Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/js/jquery.i18n.js
blob: a50e61294fab10582739b3b4e7c599c50d1b17dc (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
/* 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($){
    // PG: Maybe put everything in namespace i18n and rename the following function $.i18n.gettext?

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

    $.i18n.cgettext = function(context, string, locale){
	 var lang = locale || $.i18n.lang;
	 if (!this.i18n[lang] || !this.i18n[lang].strings){
	     return string;
	 }
	 return this.i18n[lang].strings[context][string]||string;
     };

    // You can override this in messages.<lang>.json.
    $.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;
     $._c = $.i18n.cgettext;

     $.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);