-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlocale_simple.js
277 lines (253 loc) · 5.98 KB
/
locale_simple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
*
* Locale Simple Translation JavaScript Library
*
*/
if (typeof sprintf != 'function') {
throw "locale_simple.js: require a javascript sprintf implementation";
}
if (typeof Gettext != 'function' || typeof Gettext.strargs != 'function') {
throw "locale_simple.js: require Gettext.js of http://jsgettext.berlios.de/ to be loaded, included in this distribution.";
}
if (typeof locale_simple != 'undefined') {
throw "locale_simple.js: locale_simple.js already loaded";
}
var locale_data = {};
var locale_simple = {
curr: null,
lang: null,
dir: null,
dry: 0,
nowrite: 0,
tds: {},
l_dir: function(dir) {
if (this.dir != null) {
throw "locale_simple.js: can't switch dir";
}
this.dir = dir;
},
l_dry: function(dry,nowrite) {
this.dry = dry;
this.nowrite = nowrite;
},
l_lang: function(lang) {
if (this.lang != null) {
throw "locale_simple.js: can't switch language";
}
this.lang = lang;
},
// load: function(url, callback) {
// var head = document.getElementsByTagName('head')[0];
// var script = document.createElement('script');
// script.type = 'text/javascript';
// script.src = url;
// script.onreadystatechange = callback;
// script.onload = callback;
// head.appendChild(script);
// },
ltd: function(textdomain) {
if (!(textdomain in this.tds)) {
// if (this.dir == null) {
// throw "locale_simple.js: please l_dir() before ltd()";
// }
// if (this.lang == null) {
// throw "locale_simple.js: please l_lang() before ltd()";
// }
// var loc_url = this.dir+'/'+this.lang+'/LC_MESSAGES/'+textdomain+'.json';
this.tds[textdomain] = new Gettext({
'domain': textdomain,
'locale_data': locale_data
});
}
this.curr = this.tds[textdomain];
return textdomain;
},
wd: function(td,msgctxt,msgid,msgid_plural) {
if (typeof console != 'object') {
return;
}
if (typeof console.debug != 'function') {
return;
}
if (td) { console.debug('# domain: '+td) }
if (msgctxt) { console.debug('msgctxt "'+msgctxt+'"') }
if (msgid) { console.debug('msgid "'+msgid+'"') }
if (msgid_plural) { console.debug('msgid_plural "'+msgid_plural+'"') }
console.debug('');
},
l: function() {
var A = this.argarr(arguments);
var id = A.shift();
var gt;
if (this.dry) {
gt = id;
if (!this.nowrite) {
this.wd(null,null,id,null);
}
} else {
gt = this.curr.gettext(id);
}
A.unshift(gt);
return sprintf.apply(null,A);
},
ln: function() {
var A = this.argarr(arguments);
var id = A.shift();
var idp = A.shift();
var n = A.shift();
var gt;
if (this.dry) {
if (n != 1) {
gt = idp;
} else {
gt = id;
}
if (!this.nowrite) {
this.wd(null,null,id,idp);
}
} else {
gt = this.curr.ngettext(id,idp,n);
}
A.unshift(n);
A.unshift(gt);
return sprintf.apply(null,A);
},
lp: function() {
var A = this.argarr(arguments);
var ctxt = A.shift();
var id = A.shift();
var gt;
if (this.dry) {
gt = id;
if (!this.nowrite) {
this.wd(null,ctxt,id,null);
}
} else {
gt = this.curr.pgettext(ctxt,id);
}
A.unshift(gt);
return sprintf.apply(null,A);
},
lnp: function() {
var A = this.argarr(arguments);
var ctxt = A.shift();
var id = A.shift();
var idp = A.shift();
var n = A.shift();
var gt;
if (this.dry) {
if (n != 1) {
gt = idp;
} else {
gt = id;
}
if (!this.nowrite) {
this.wd(null,ctxt,id,idp);
}
} else {
gt = this.curr.npgettext(ctxt,id,idp,n);
}
A.unshift(n);
A.unshift(gt);
return sprintf.apply(null,A);
},
ld: function() {
var A = this.argarr(arguments);
var td = A.shift();
var id = A.shift();
var gt;
if (this.dry) {
gt = id;
if (!this.nowrite) {
this.wd(td,null,id,null);
}
} else {
gt = this.curr.dgettext(td,id);
}
A.unshift(gt);
return sprintf.apply(null,A);
},
ldn: function() {
var A = this.argarr(arguments);
var td = A.shift();
var id = A.shift();
var idp = A.shift();
var n = A.shift();
var gt;
if (this.dry) {
if (n != 1) {
gt = idp;
} else {
gt = id;
}
if (!this.nowrite) {
this.wd(td,null,id,idp);
}
} else {
gt = this.curr.dngettext(td,id,idp,n);
}
A.unshift(n);
A.unshift(gt);
return sprintf.apply(null,A);
},
ldp: function() {
var A = this.argarr(arguments);
var td = A.shift();
var ctxt = A.shift();
var id = A.shift();
var gt;
if (this.dry) {
gt = id;
if (!this.nowrite) {
this.wd(td,ctxt,id,null);
}
} else {
gt = this.curr.dpgettext(td,ctxt,id);
}
A.unshift(gt);
return sprintf.apply(null,A);
},
ldnp: function(){
var A = this.argarr(arguments);
var td = A.shift();
var ctxt = A.shift();
var id = A.shift();
var idp = A.shift();
var n = A.shift();
var gt;
if (this.dry) {
if (n != 1) {
gt = idp;
} else {
gt = id;
}
if (!this.nowrite) {
this.wd(td,ctxt,id,idp);
}
} else {
gt = this.curr.dnpgettext(td,ctxt,id,idp,n);
}
A.unshift(n);
A.unshift(gt);
return sprintf.apply(null,A);
},
argarr: function(args) {
var arr = new Array();
for (var i=0, len=args.length; i<len; i++) {
arr.push(args[i]);
}
return arr;
}
};
function l_dry() { return locale_simple.l_dry.apply(locale_simple,arguments) }
function l_dir() { return locale_simple.l_dir.apply(locale_simple,arguments) }
function l_lang() { return locale_simple.l_lang.apply(locale_simple,arguments) }
function ltd() { return locale_simple.ltd.apply(locale_simple,arguments) }
function l() { return locale_simple.l.apply(locale_simple,arguments) }
function ln() { return locale_simple.ln.apply(locale_simple,arguments) }
function lp() { return locale_simple.lp.apply(locale_simple,arguments) }
function lnp() { return locale_simple.lnp.apply(locale_simple,arguments) }
function ld() { return locale_simple.ld.apply(locale_simple,arguments) }
function ldn() { return locale_simple.ldn.apply(locale_simple,arguments) }
function ldp() { return locale_simple.ldp.apply(locale_simple,arguments) }
function ldnp() { return locale_simple.ldnp.apply(locale_simple,arguments) }