4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
16 $.ajatus = $.ajatus || {};
33 $.extend($.ajatus.i10n, {
34 init: function(lang, after_init) {
35 $.ajatus.i10n.lang = lang || 'en_GB';
36 $.ajatus.i10n._after_init = after_init;
37 $.ajatus.i10n._load_lang($.ajatus.i10n.lang);
39 get_available_langs: function(force_refresh) {
40 if (typeof force_refresh == 'undefined') {
41 var force_refresh = false;
45 if ($.ajatus.i10n.available == null) {
46 $.ajatus.i10n.available = [];
53 // TODO: Somehow get all available translations from js/languages -folder
54 $.ajatus.i10n.available = [
66 return $.ajatus.i10n.available;
68 get: function() { //string, keys, pluralize_key
69 if ( arguments.length < 1
70 || typeof arguments[0] == 'undefined')
75 var string = new String();
76 var trans_key = arguments[0].toString().toLowerCase();
77 trans_key = $.trim(trans_key);
78 //$.ajatus.debug("get "+trans_key, "ajatus.i10n");
80 var replace_args = [];
81 if ( arguments.length > 1
82 && typeof(arguments[1]) == 'object')
84 replace_args = arguments[1];
87 var auto_plural = false;
88 if (arguments.length < 3) {
92 if (typeof($.ajatus.i10n.dict[trans_key]) != 'undefined') {
93 string = String($.ajatus.i10n.dict[trans_key]);
95 string = arguments[0].toString();
98 if (replace_args.length > 0) {
99 string = $.ajatus.i10n.format_result(string, replace_args, auto_plural);
102 if ( arguments.length > 2
103 && typeof(arguments[2]) == 'string')
105 string = $.ajatus.i10n.plural(string, arguments[2]);
108 var curr_s = "%_CURR_S";
109 var curr_n = "%_CURR_N";
110 var s_re = new RegExp(curr_s, "i");
111 var n_re = new RegExp(curr_n, "i");
112 if (string.match(s_re)) {
113 var re = new RegExp(curr_s, "gi");
114 string = string.replace(re, $.ajatus.i10n.get_currency().symbol);
116 if (string.match(n_re)) {
117 var re = new RegExp(curr_n, "gi");
118 string = string.replace(re, $.ajatus.i10n.get_currency().symbol);
120 // $.ajatus.debug("return "+string, "ajatus.i10n");
121 if ($.browser.safari) {
122 return $.ajatus.utils.utf8.decode(string);
127 get_currency: function() {
128 if (arguments.length > 0) {
129 switch (arguments[0]) {
131 return $.ajatus.i10n.currency.name;
134 return $.ajatus.i10n.currency.name;
137 return $.ajatus.i10n.currency;
141 return $.ajatus.i10n.currency;
143 format_result: function(string, args, auto_plural) {
144 if (typeof auto_plural == 'undefined') {
145 var auto_plural = true;
147 $.each(args, function(i,value){
148 var key = $.ajatus.i10n._resolve_replace_type(value);
152 if (typeof value != 'number') {
155 var re = new RegExp(/\b[%]?d.(\w+)\b/);
156 var m = re.exec(string);
160 var sing = m[(m.length-1)];
161 var plural = $.ajatus.i10n.inflector.pluralize(sing);
162 string = string.toString().replace(sing, plural);
165 string = string.toString().replace(key, value);
169 plural: function(string, key) {
170 if (typeof key == 'undefined') {
174 var plural_key = $.ajatus.i10n.inflector.pluralize(key);
176 var result = string.toString().replace(key, plural_key);
180 _resolve_replace_type: function(value) {
181 if (typeof value == 'string') {
184 if (typeof value == 'number') {
190 _load_lang: function(lang) {
191 if ( typeof lang == 'undefined'
200 $.ajatus.events.lock_pool.increase();
202 var lang_url = $.ajatus.preferences.client.application_url + 'js/languages/'+lang+'.js';
203 $.ajatus.utils.load_script(lang_url, "$.ajatus.i10n._lang_loaded", [lang]);
205 _lang_loaded: function(lang) {
209 $.ajatus.i10n.initialized = true;
210 $.ajatus.events.lock_pool.decrease();
212 $.ajatus.i10n._after_init.apply($.ajatus.i10n._after_init, []);
216 $.ajatus.i10n.inflections = {};
217 $.ajatus.i10n.inflector = {
220 $.extend($.ajatus.i10n.inflector, {
221 ordinalize: function(number) {
222 $.ajatus.i10n.inflector._get_inflector();
223 if ($.ajatus.i10n.inflector.inflection == false) {
227 if (typeof ($.ajatus.i10n.inflector.inflection['ordinalize']) == 'function') {
228 return $.ajatus.i10n.inflector.inflection.ordinalize(number);
232 pluralize: function(word) {
233 $.ajatus.i10n.inflector._get_inflector();
234 if ($.ajatus.i10n.inflector.inflection == false) {
239 $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
240 if (word.toLowerCase() == uncountable) {
241 result = uncountable;
247 $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
248 var singular = irr[0];
250 if ((word.toLowerCase() == singular) || (word == plural)) {
257 $.each($.ajatus.i10n.inflector.inflection.plural, function(i,plur){
259 var replace_string = plur[1];
260 if (regex.test(word)) {
261 result = word.replace(regex, replace_string);
270 singularize: function(word) {
271 $.ajatus.i10n.inflector._get_inflector();
272 if ($.ajatus.i10n.inflector.inflection == false) {
278 $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
279 if (word.toLowerCase() == uncountable) {
280 result = uncountable;
286 $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
287 var singular = irr[0];
289 if ((word.toLowerCase() == singular) || (word == plural)) {
296 $.each($.ajatus.i10n.inflector.inflection.singular, function(i,sing){
298 var replace_string = sing[1];
299 if (regex.test(word)) {
300 result = word.replace(regex, replace_string);
309 _get_inflector: function(lng) {
310 var lang = lng || $.ajatus.i10n.lang;
312 if ($.ajatus.i10n.inflector.inflection == null) {
313 if (typeof($.ajatus.i10n.inflections[lang]) == 'undefined') {
314 $.ajatus.i10n.inflector.inflection = false;
316 $.ajatus.i10n.inflector.inflection = $.ajatus.i10n.inflections[lang];