Fixed dynamic style loading for Safari
[ajatus.git] / js / ajatus.i10n.js
blob6e086ae66f10d73b3359fae6cac901e5be590c7b
1 /*
2  * This file is part of
3  *
4  * Ajatus - Distributed CRM
5  * @requires jQuery v1.2.1
6  * 
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
12  * 
13  */
15 (function($){
16     $.ajatus = $.ajatus || {};
17     
18     $.ajatus.i10n = {
19         lang: 'en_GB',
20         dict: {},
21         currency: {
22             name: '',
23             symbol: ''
24         },
25         datetime: {
26             date: 'MDY/',
27             time: 'HMS:'
28         },
29         initialized: false,
30         available: null,
31         _after_init: null
32     };
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);
38         },
39         get_available_langs: function(force_refresh) {
40             if (typeof force_refresh == 'undefined') {
41                 var force_refresh = false;
42             }            
43             var refresh = false;
44             
45             if ($.ajatus.i10n.available == null) {
46                 $.ajatus.i10n.available = [];
47                 refresh = true;
48             }
49             
50             if (   refresh
51                 || force_refresh)
52             {
53                 // TODO: Somehow get all available translations from js/languages -folder
54                 $.ajatus.i10n.available = [
55                     'de_DE',
56                     'en_GB',
57                     'fi_FI',
58                     'hu_HU',
59                     'nl_NL',
60                     'pl_PL',
61                     'pt_BR',
62                     'sv_SE'
63                 ];
64             }
66             return $.ajatus.i10n.available;
67         },
68         get: function() { //string, keys, pluralize_key            
69             if (   arguments.length < 1
70                 || typeof arguments[0] == 'undefined')
71             {
72                 return;
73             }
74             
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");
79             
80             var replace_args = [];
81             if (   arguments.length > 1
82                 && typeof(arguments[1]) == 'object')
83             {
84                 replace_args = arguments[1];
85             }            
87             var auto_plural = false;
88             if (arguments.length < 3) {
89                 auto_plural = true;
90             }
91             
92             if (typeof($.ajatus.i10n.dict[trans_key]) != 'undefined') {                
93                 string = String($.ajatus.i10n.dict[trans_key]);
94             } else {
95                 string = arguments[0].toString();
96             }
97             
98             if (replace_args.length > 0) {
99                 string = $.ajatus.i10n.format_result(string, replace_args, auto_plural);
100             }
101             
102             if (   arguments.length > 2
103                 && typeof(arguments[2]) == 'string')
104             {
105                 string = $.ajatus.i10n.plural(string, arguments[2]);
106             }
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);
115             }
116             if (string.match(n_re)) {
117                 var re = new RegExp(curr_n, "gi");
118                 string = string.replace(re, $.ajatus.i10n.get_currency().symbol);
119             }
120             // $.ajatus.debug("return "+string, "ajatus.i10n");
121             if ($.browser.safari) {
122                 return $.ajatus.utils.utf8.decode(string);
123             }
124             
125             return string;
126         },
127         get_currency: function() {
128             if (arguments.length > 0) {
129                 switch (arguments[0]) {
130                     case 'name':
131                         return $.ajatus.i10n.currency.name;
132                     break;
133                     case 'symbol':
134                         return $.ajatus.i10n.currency.name;
135                     break;
136                     case 'both':
137                         return $.ajatus.i10n.currency;
138                     break;
139                 }
140             }
141             return $.ajatus.i10n.currency;
142         },
143         format_result: function(string, args, auto_plural) {
144             if (typeof auto_plural == 'undefined') {
145                 var auto_plural = true;
146             }
147             $.each(args, function(i,value){
148                 var key = $.ajatus.i10n._resolve_replace_type(value);
149                 if (   key == '%d'
150                     && auto_plural)
151                 {
152                     if (typeof value != 'number') {
153                         value = 'null';
154                     }
155                     var re = new RegExp(/\b[%]?d.(\w+)\b/);
156                     var m = re.exec(string);
157                     if (   m != null
158                         && value > 1)
159                     {
160                         var sing = m[(m.length-1)];
161                         var plural = $.ajatus.i10n.inflector.pluralize(sing);
162                         string = string.toString().replace(sing, plural);
163                     }
164                 }
165                 string = string.toString().replace(key, value);
166             });
167             return string;
168         },
169         plural: function(string, key) {
170             if (typeof key == 'undefined') {
171                 var key = string;
172             }
173             
174             var plural_key = $.ajatus.i10n.inflector.pluralize(key);
175                         
176             var result = string.toString().replace(key, plural_key);
177             
178             return result;
179         },
180         _resolve_replace_type: function(value) {
181             if (typeof value == 'string') {
182                 return '%s';
183             }
184             if (typeof value == 'number') {
185                 return '%d';
186             }
187             
188             return '%s';
189         },
190         _load_lang: function(lang) {
191             if (   typeof lang == 'undefined'
192                 || lang == '')
193             {
194                 return;
195             }
197             $.ajaxSetup({
198                 async: false
199             });
200             $.ajatus.events.lock_pool.increase();
201             
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]);
204         },
205         _lang_loaded: function(lang) {
206             $.ajaxSetup({
207                 async: true
208             });
209             $.ajatus.i10n.initialized = true;
210             $.ajatus.events.lock_pool.decrease();
211             
212             $.ajatus.i10n._after_init.apply($.ajatus.i10n._after_init, []);
213         }
214     });
215     
216     $.ajatus.i10n.inflections = {};
217     $.ajatus.i10n.inflector = {
218         inflection: null
219     };
220     $.extend($.ajatus.i10n.inflector, {        
221         ordinalize: function(number) {
222             $.ajatus.i10n.inflector._get_inflector();
223             if ($.ajatus.i10n.inflector.inflection == false) {
224                 return number;
225             }
226                         
227             if (typeof ($.ajatus.i10n.inflector.inflection['ordinalize']) == 'function') {
228                 return $.ajatus.i10n.inflector.inflection.ordinalize(number);
229             }
230             return number;
231         },
232         pluralize: function(word) {
233             $.ajatus.i10n.inflector._get_inflector();
234             if ($.ajatus.i10n.inflector.inflection == false) {
235                 return word;
236             }
237                         
238             var result = '';
239             $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
240                 if (word.toLowerCase() == uncountable) {
241                     result = uncountable;
242                 }
243             });
244             if (result != '') {
245                 return result;
246             }
247             $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
248                 var singular = irr[0];
249                 var plural = irr[1];
250                 if ((word.toLowerCase() == singular) || (word == plural)) {
251                     result = plural;
252                 }
253             });
254             if (result != '') {
255                 return result;
256             }
257             $.each($.ajatus.i10n.inflector.inflection.plural, function(i,plur){
258                 var regex = plur[0];
259                 var replace_string = plur[1];
260                 if (regex.test(word)) {
261                     result = word.replace(regex, replace_string);
262                 }
263             });
264             if (result != '') {
265                 return result;
266             }
267             
268             return word;
269         },
270         singularize: function(word) {
271             $.ajatus.i10n.inflector._get_inflector();
272             if ($.ajatus.i10n.inflector.inflection == false) {
273                 return word;
274             }
275             
276             var result = '';
277             
278             $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
279                 if (word.toLowerCase() == uncountable) {
280                     result = uncountable;
281                 }
282             });
283             if (result != '') {
284                 return result;
285             }
286             $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
287                 var singular = irr[0];
288                 var plural = irr[1];
289                 if ((word.toLowerCase() == singular) || (word == plural)) {
290                     result = plural;
291                 }
292             });
293             if (result != '') {
294                 return result;
295             }
296             $.each($.ajatus.i10n.inflector.inflection.singular, function(i,sing){
297                 var regex = sing[0];
298                 var replace_string = sing[1];
299                 if (regex.test(word)) {
300                     result = word.replace(regex, replace_string);
301                 }
302             });
303             if (result != '') {
304                 return result;
305             }
306             
307             return word;
308         },
309         _get_inflector: function(lng) {
310             var lang = lng || $.ajatus.i10n.lang;
311             
312             if ($.ajatus.i10n.inflector.inflection == null) {
313                 if (typeof($.ajatus.i10n.inflections[lang]) == 'undefined') {
314                     $.ajatus.i10n.inflector.inflection = false;
315                 } else {
316                     $.ajatus.i10n.inflector.inflection = $.ajatus.i10n.inflections[lang];            
317                 }                
318             }
319         }
320     });
321     
322 })(jQuery);