Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[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         ns._locales[ns._localeLang] = {};
10
11         jQuery.ajax({
12           url: "js/locale/"+ ns._localeLang +".js",
13           async: false,
14           dataType: "json",
15           success: function(res) {
16             ns._locales[ns._localeLang] = res;
17           },
18         });
19       }
20
21       text = ns._locales[ns._localeLang][text] || text;
22     }
23
24     if( Object.prototype.toString.call( params ) === '[object Array]' ) {
25       var len = params.length;
26
27       for(var i=0; i<len; ++i) {
28         var key = i + 1;
29         var value = params[i];
30         text = text.split("#"+ key).join(value);
31       }
32     }
33     else if( typeof params == 'object' ) {
34       for(var key in params) {
35         var value = params[key];
36         text = text.split("#{"+ key +"}").join(value);
37       }
38     }
39
40     return text;
41   };
42
43   ns.initLocale = function(localeLang) {
44     ns._localeLang = localeLang;
45   };
46
47 });