-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
308 lines (278 loc) · 8.27 KB
/
index.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
class I18n {
constructor(lang, filePath) {
this._lang = lang;
this._locale = require(filePath);
}
//Get the rule for pluralization
//http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
get_rule(count, language) {
switch (language) {
//nplurals=2; plural=(n > 1);
case 'ach':
case 'ak':
case 'am':
case 'arn':
case 'br':
case 'fil':
case 'fr':
case 'gun':
case 'ln':
case 'mfe':
case 'mg':
case 'mi':
case 'oc':
case 'pt_BR':
case 'tg':
case 'ti':
case 'tr':
case 'uz':
case 'wa':
return (count > 1) ? 1 : 0;
break;
//nplurals=2; plural=(n != 1);
case 'af':
case 'an':
case 'anp':
case 'as':
case 'ast':
case 'az':
case 'bg':
case 'bn':
case 'brx':
case 'ca':
case 'da':
case 'doi':
case 'de':
case 'el':
case 'en':
case 'eo':
case 'es':
case 'es_AR':
case 'et':
case 'eu':
case 'ff':
case 'fi':
case 'fo':
case 'fur':
case 'fy':
case 'gl':
case 'gu':
case 'ha':
case 'he':
case 'hi':
case 'hne':
case 'hu':
case 'hy':
case 'ia':
case 'it':
case 'kl':
case 'kn':
case 'ku':
case 'lb':
case 'mai':
case 'ml':
case 'mn':
case 'mni':
case 'mr':
case 'nah':
case 'nap':
case 'nb':
case 'ne':
case 'nl':
case 'nn':
case 'no':
case 'nso':
case 'or':
case 'pa':
case 'pap':
case 'pms':
case 'ps':
case 'pt':
case 'rm':
case 'rw':
case 'sat':
case 'sco':
case 'sd':
case 'se':
case 'si':
case 'so':
case 'son':
case 'sq':
case 'sv':
case 'sw':
case 'ta':
case 'te':
case 'tk':
case 'ur':
case 'yo':
return (count != 1) ? 1 : 0;
break;
//nplurals=1; plural=0;
case 'ay':
case 'bo':
case 'cgg':
case 'dz':
case 'fa':
case 'id':
case 'ja':
case 'jbo':
case 'ka':
case 'kk':
case 'km':
case 'ko':
case 'ky':
case 'lo':
case 'ms':
case 'my':
case 'sah':
case 'su':
case 'th':
case 'tt':
case 'ug':
case 'vi':
case 'wo':
case 'zh':
case 'jv':
return 0;
break;
//nplurals=2; plural=(n%10!=1 || n%100==11);
case 'is':
return (count % 10 != 1 || count % 100 == 11) ? 1 : 0;
break;
//nplurals=4; plural=(n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3;
case 'kw':
return (count == 1) ? 0 : (count == 2) ? 1 : (count == 3) ? 2 : 3;
break;
//nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
case 'uk':
case 'sr':
case 'ru':
case 'hr':
case 'bs':
case 'be':
return count % 10 == 1 && count % 100 != 11 ? 0 : count % 10 >= 2 && count % 10 <= 4 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
break;
//nplurals=3; plural=(n==0 ? 0 : n==1 ? 1 : 2);
case 'mnk':
return count == 0 ? 0 : count == 1 ? 1 : 2;
break;
//nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;
case 'sk':
return (count == 1) ? 0 : (count >= 2 && count <= 4) ? 1 : 2;
break;
//nplurals=3; plural=(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);
case 'ro':
return count == 1 ? 0 : (count == 0 || (count % 100 > 0 && count % 100 < 20)) ? 1 : 2;
break;
//nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5);
case 'ar':
return count == 0 ? 0 : count == 1 ? 1 : count == 2 ? 2 : count % 100 >= 3 && count % 100 <= 10 ? 3 : count % 100 >= 11 ? 4 : 5;
break;
//nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;
case 'cs':
return count == 1 ? 0 : (count >= 2 && count <= 4) ? 1 : 2;
break;
//countplurals=3; plural=(n==1) ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
case 'csb':
return (count == 1) ? 0 : count % 10 >= 2 && count % 10 <= 4 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
break;
//nplurals=4; plural=(n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3;
case 'cy':
return (count == 1) ? 0 : (count == 2) ? 1 : (count != 8 && count != 11) ? 2 : 3;
break;
//nplurals=5; plural=n==1 ? 0 : n==2 ? 1 : (n>2 && n<7) ? 2 :(n>6 && n<11) ? 3 : 4;
case 'ga':
return count == 1 ? 0 : count == 2 ? 1 : (count > 2 && count < 7) ? 2 : (count > 6 && count < 11) ? 3 : 4;
break;
//nplurals=4; plural=(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3;
case 'gd':
return (count == 1 || count == 11) ? 0 : (count == 2 || count == 12) ? 1 : (count > 2 && count < 20) ? 2 : 3;
break;
//nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);
case 'it':
return count % 10 == 1 && count % 100 != 11 ? 0 : count % 10 >= 2 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
break;
//nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2);
case 'lv':
return count % 10 == 1 && count % 100 != 11 ? 0 : count != 0 ? 1 : 2;
break;
//nplurals=2; plural= n==1 || n%10==1 ? 0 : 1;
case 'mk':
return count = 2; plural = count == 1 || count % 10 == 1 ? 0 : 1;
break;
//nplurals=4; plural=(n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3);
case 'mt':
return count == 1 ? 0 : count == 0 || (count % 100 > 1 && n % 100 < 11) ? 1 : (count % 100 > 10 && count % 100 < 20) ? 2 : 3;
break;
//nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
case 'pl':
return count == 1 ? 0 : count % 10 >= 2 && count % 10 <= 4 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
break;
//nplurals=4; plural=(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0);
case 'sl':
return count % 100 == 1 ? 1 : count % 100 == 2 ? 2 : count % 100 == 3 || count % 100 == 4 ? 3 : 0;
break;
default:
return 0;
}
}
__(string, values, lang) {
//if lang is passed then use it, else use the default lang
if (!lang) {
lang = this._lang;
}
//return translation of the original sting if did not find the translation
var translation = string;
//get the corresponding translation from the file
if (typeof this._locale[string] != "undefined" && typeof this._locale[string][lang] != "undefined") {
translation = this._locale[string][lang];
}
//If the string have place to render values withen
if ((/{{.+?}}/g).test(translation)) {
//get all the parts needed to be replaced
var matches = translation.match(/{{.+?}}/g);
//loop on each match
for (var index in matches) {
//get the match {{example}}
var match = matches[index];
//get the word in the match example
var match_word = (match.replace('}}', '')).replace('{{', '');
//translate the word if was passed in the values var
if (values && typeof values[match_word] != "undefined") {
translation = translation.replace(match, values[match_word]);
continue;//move to the next word in the loop
} else if (typeof this._locale[match_word] != "undefined" && typeof this._locale[match_word][lang] != "undefined") {
//If the translation is there in the file then translate it directly
translation = translation.replace(match, this._locale[match_word][lang]);
continue;//move to the next word in the loop
}
//if the matched word have a count
if ((/\|\|.+/g).test(match_word)) {
var temp_array = match_word.split("||");
//update the matched word
match_word = temp_array[0];
//get the variable of the count for the word
var item_count_variable = temp_array[1];
//get the value form values passed to this function
//TODO through error if not found in values
var item_count = values[item_count_variable];
//will get the rule or for pluralization based on the lang
var rule = this.get_rule(item_count, lang);
if (typeof this._locale[match_word][lang] == "object") {
translation = translation.replace(match, this._locale[match_word][lang][rule]);
} else {
translation = translation.replace(match, this._locale[match_word][lang]);
}
} else {
if (typeof values == "object") {
translation = translation.replace(match, values[match_word]);
} else {
translation = translation.replace(match, this._locale[match_word][lang]);
}
}
}//end of for
}//END OF IF
return translation;
}//END OF function __()
}
module.exports = I18n;