Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/js/jquery.i18n.js
blob: aad8f270e8d6f30b09f8b5194d34d7c221c13ae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright Bryan W Berry, 2009,
 * under the MIT license http://www.opensource.org/licenses/mit-license.php
 *
 * this library is heavily influenced by the GNU LIBC library
 *  http://www.gnu.org/software/libc/manual/html_node/Locales.html
 */

(function($){
    $.i18n = {};

    
    $.i18n.gettext = function(string) {
	var lang = $.i18n.lang;
	if (!$.i18n[lang] || !$.i18n[lang].strings) {
	    return string;
	}
	return $.i18n[lang].strings[string]||string;
    };
     

    
    $.i18n.pgettext = function(context, string) {
	var lang = $.i18n.lang;
	if (!$.i18n[lang] ||
            !$.i18n[lang].contextualized_strings ||
            !$.i18n[lang].contextualized_strings[context]) {
	    return string;
	}
	return $.i18n[lang].contextualized_strings[context][string]||string;
    }; 

    // You can override this in messages.<lang>.json.
    // We should maybe extract this from the .mo file.
    $.i18n.choose_pluralized_msg = function (choices, n) {
        return n == 1 ? choices[0] : choices[1];
    };

    $.i18n.ngettext = function (msgid1, msgid2, n) {
        var lang = $.i18n.lang;
        if (!$.i18n[lang] || !$.i18n[lang].strings) {
            return $.i18n.choose_pluralized_msg([msgid1, msgid2], n);
        }
        // Is using msgid1 as the key ok?
        return $.i18n.choose_pluralized_msg($.i18n[lang].strings[msgid1]
                                            || [msgid1, msgid2],
                                            n);
    };

    $._ = $.i18n.gettext;

    $.i18n.setLocale = function (locale){
	$.i18n.lang = locale;
    };

    $.i18n.getLocale = function (){
	return $.i18n.lang;
    };


    /**
      * Converts a number to numerals in the specified locale. Currently only
      * supports devanagari numerals for Indic languages like Nepali and Hindi
      * @param {Number} Number to be converted
      * @param {locale} locale that number should be converted to
      * @returns {String} Unicode string for localized numeral
      */
    $.i18n._n = function(num, locale){

	locale = locale || $.i18n.lang;

	if (!this.i18n[locale] || !this.i18n[locale].numeralBase ){
	    return num;
	}


	//48 is the base for western numerals
	var numBase = $.i18n[$.i18n.lang].numeralBase || 48;
	var prefix =  $.i18n[$.i18n.lang].numeralPrefix || "u00";

	var convertDigit = function(digit){
	    return '\\' + prefix +
		(numBase + parseInt(digit)).toString(16);
	};

	var charArray = num.toString().split("").map(convertDigit);
	return eval('"' + charArray.join('') + '"');
    };

    $._n = $.i18n._n;

    /* ToDo
      * implement sprintf
      * conversion functions for monetary and numeric
      * sorting functions (collation) for different locales
      */

})(jQuery);