Merge branch 'master' into dev
[kivitendo-erp.git] / js / kivi.js
1 namespace("kivi", function(ns) {
2
3   ns._localeLang = false;
4   ns._locales = {};
5
6   ns.t8 = function(text, params) {
7     if( ns._localeLang ) {
8       if( !ns._locales[ns._localeLang] ) {
9         jQuery.ajax({
10           url: "locale/"+ ns._localeLang +"/js.js",
11           async: false,
12           dataType: "json",
13           success: function(res) {
14             ns._locales[ns._localeLang] = res;
15           },
16           error: function(xhr, textStatus, errorThrown) {
17             alert(textStatus +": "+ errorThrown);
18           },
19         });
20       }
21
22       text = ns._locales[ns._localeLang][text] || text;
23     }
24
25     if( Object.prototype.toString.call( params ) === '[object Array]' ) {
26       var len = params.length;
27
28       for(var i=0; i<len; ++i) {
29         var key = i + 1;
30         var value = params[i];
31         text = text.split("#"+ key).join(value);
32       }
33     }
34     else if( typeof params == 'object' ) {
35       for(var key in params) {
36         var value = params[key];
37         text = text.split("#{"+ key +"}").join(value);
38       }
39     }
40
41     return text;
42   };
43
44   ns.initLocale = function(localeLang) {
45     ns._localeLang = localeLang;
46   };
47
48 });