diff options
author | Bryan Berry <bryan@olenepal.org> | 2010-03-15 08:43:52 (GMT) |
---|---|---|
committer | Bryan Berry <bryan@olenepal.org> | 2010-03-15 08:43:52 (GMT) |
commit | d03552a31d2b63b42084ae506fc94e6149a5440a (patch) | |
tree | 76608ccbe04fb941c9e4b69a205c957d29ac4b95 /js/jquery.i18n.js |
initial commit
Diffstat (limited to 'js/jquery.i18n.js')
-rwxr-xr-x | js/jquery.i18n.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/js/jquery.i18n.js b/js/jquery.i18n.js new file mode 100755 index 0000000..aad8f27 --- /dev/null +++ b/js/jquery.i18n.js @@ -0,0 +1,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); + + |