0e9aeacd
root
localization l20n
|
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
|
(function () { 'use strict';
const Service = bridge.service;
const channel = new BroadcastChannel('l20n-channel');
function broadcast(type, data) {
return this.service.broadcast(type, data);
}
function L10nError(message, id, lang) {
this.name = 'L10nError';
this.message = message;
this.id = id;
this.lang = lang;
}
L10nError.prototype = Object.create(Error.prototype);
L10nError.prototype.constructor = L10nError;
function load(type, url) {
return new Promise(function(resolve, reject) {
const xhr = new XMLHttpRequest();
if (xhr.overrideMimeType) {
xhr.overrideMimeType(type);
}
xhr.open('GET', url, true);
if (type === 'application/json') {
xhr.responseType = 'json';
}
xhr.addEventListener('load', function io_onload(e) {
if (e.target.status === 200 || e.target.status === 0) {
// Sinon.JS's FakeXHR doesn't have the response property
resolve(e.target.response || e.target.responseText);
} else {
reject(new L10nError('Not found: ' + url));
}
});
xhr.addEventListener('error', reject);
xhr.addEventListener('timeout', reject);
// the app: protocol throws on 404, see https://bugzil.la/827243
try {
xhr.send(null);
} catch (e) {
if (e.name === 'NS_ERROR_FILE_NOT_FOUND') {
// the app: protocol throws on 404, see https://bugzil.la/827243
reject(new L10nError('Not found: ' + url));
} else {
throw e;
}
}
});
}
const io = {
extra: function(code, ver, path, type) {
return navigator.mozApps.getLocalizationResource(
code, ver, path, type);
},
app: function(code, ver, path, type) {
switch (type) {
case 'text':
return load('text/plain', path);
case 'json':
return load('application/json', path);
default:
throw new L10nError('Unknown file type: ' + type);
}
},
};
|
a1a3bc73
Luigi Serra
graphs updates
|
75
76
|
function fetchResource(res, { code, src, ver }) {
const url = res.replace('{locale}', code);
|
0e9aeacd
root
localization l20n
|
77
|
const type = res.endsWith('.json') ? 'json' : 'text';
|
a1a3bc73
Luigi Serra
graphs updates
|
78
|
return io[src](code, ver, url, type);
|
0e9aeacd
root
localization l20n
|
79
80
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
81
82
|
const KNOWN_MACROS = ['plural'];
const MAX_PLACEABLE_LENGTH = 2500;
|
0e9aeacd
root
localization l20n
|
83
|
|
a1a3bc73
Luigi Serra
graphs updates
|
84
85
86
|
// Unicode bidi isolation characters
const FSI = '\u2068';
const PDI = '\u2069';
|
0e9aeacd
root
localization l20n
|
87
|
|
a1a3bc73
Luigi Serra
graphs updates
|
88
|
const resolutionChain = new WeakSet();
|
0e9aeacd
root
localization l20n
|
89
|
|
a1a3bc73
Luigi Serra
graphs updates
|
90
91
92
93
|
function format(ctx, lang, args, entity) {
if (typeof entity === 'string') {
return [{}, entity];
}
|
0e9aeacd
root
localization l20n
|
94
|
|
a1a3bc73
Luigi Serra
graphs updates
|
95
96
97
|
if (resolutionChain.has(entity)) {
throw new L10nError('Cyclic reference detected');
}
|
0e9aeacd
root
localization l20n
|
98
|
|
a1a3bc73
Luigi Serra
graphs updates
|
99
|
resolutionChain.add(entity);
|
0e9aeacd
root
localization l20n
|
100
|
|
a1a3bc73
Luigi Serra
graphs updates
|
101
102
103
104
105
106
107
108
109
110
111
112
|
let rv;
// if format fails, we want the exception to bubble up and stop the whole
// resolving process; however, we still need to remove the entity from the
// resolution chain
try {
rv = resolveValue(
{}, ctx, lang, args, entity.value, entity.index);
} finally {
resolutionChain.delete(entity);
}
return rv;
}
|
0e9aeacd
root
localization l20n
|
113
|
|
a1a3bc73
Luigi Serra
graphs updates
|
114
115
116
117
|
function resolveIdentifier(ctx, lang, args, id) {
if (KNOWN_MACROS.indexOf(id) > -1) {
return [{}, ctx._getMacro(lang, id)];
}
|
0e9aeacd
root
localization l20n
|
118
|
|
a1a3bc73
Luigi Serra
graphs updates
|
119
120
121
122
|
if (args && args.hasOwnProperty(id)) {
if (typeof args[id] === 'string' || (typeof args[id] === 'number' &&
!isNaN(args[id]))) {
return [{}, args[id]];
|
0e9aeacd
root
localization l20n
|
123
|
} else {
|
a1a3bc73
Luigi Serra
graphs updates
|
124
|
throw new L10nError('Arg must be a string or a number: ' + id);
|
0e9aeacd
root
localization l20n
|
125
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
126
|
}
|
0e9aeacd
root
localization l20n
|
127
|
|
a1a3bc73
Luigi Serra
graphs updates
|
128
129
130
131
132
|
// XXX: special case for Node.js where still:
// '__proto__' in Object.create(null) => true
if (id === '__proto__') {
throw new L10nError('Illegal id: ' + id);
}
|
0e9aeacd
root
localization l20n
|
133
|
|
a1a3bc73
Luigi Serra
graphs updates
|
134
|
const entity = ctx._getEntity(lang, id);
|
0e9aeacd
root
localization l20n
|
135
|
|
a1a3bc73
Luigi Serra
graphs updates
|
136
137
138
|
if (entity) {
return format(ctx, lang, args, entity);
}
|
0e9aeacd
root
localization l20n
|
139
|
|
a1a3bc73
Luigi Serra
graphs updates
|
140
141
|
throw new L10nError('Unknown reference: ' + id);
}
|
0e9aeacd
root
localization l20n
|
142
|
|
a1a3bc73
Luigi Serra
graphs updates
|
143
144
|
function subPlaceable(locals, ctx, lang, args, id) {
let newLocals, value;
|
0e9aeacd
root
localization l20n
|
145
|
|
a1a3bc73
Luigi Serra
graphs updates
|
146
147
148
149
150
|
try {
[newLocals, value] = resolveIdentifier(ctx, lang, args, id);
} catch (err) {
return [{ error: err }, FSI + '{{ ' + id + ' }}' + PDI];
}
|
0e9aeacd
root
localization l20n
|
151
|
|
a1a3bc73
Luigi Serra
graphs updates
|
152
153
154
155
156
157
158
159
160
161
162
|
if (typeof value === 'number') {
const formatter = ctx._getNumberFormatter(lang);
return [newLocals, formatter.format(value)];
}
if (typeof value === 'string') {
// prevent Billion Laughs attacks
if (value.length >= MAX_PLACEABLE_LENGTH) {
throw new L10nError('Too many characters in placeable (' +
value.length + ', max allowed is ' +
MAX_PLACEABLE_LENGTH + ')');
|
0e9aeacd
root
localization l20n
|
163
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
164
165
|
return [newLocals, FSI + value + PDI];
}
|
0e9aeacd
root
localization l20n
|
166
|
|
a1a3bc73
Luigi Serra
graphs updates
|
167
168
|
return [{}, FSI + '{{ ' + id + ' }}' + PDI];
}
|
0e9aeacd
root
localization l20n
|
169
|
|
a1a3bc73
Luigi Serra
graphs updates
|
170
171
172
173
|
function interpolate(locals, ctx, lang, args, arr) {
return arr.reduce(function([localsSeq, valueSeq], cur) {
if (typeof cur === 'string') {
return [localsSeq, valueSeq + cur];
|
0e9aeacd
root
localization l20n
|
174
|
} else {
|
a1a3bc73
Luigi Serra
graphs updates
|
175
176
177
|
const [, value] = subPlaceable(locals, ctx, lang, args, cur.name);
// wrap the substitution in bidi isolate characters
return [localsSeq, valueSeq + value];
|
0e9aeacd
root
localization l20n
|
178
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
179
180
|
}, [locals, '']);
}
|
0e9aeacd
root
localization l20n
|
181
|
|
a1a3bc73
Luigi Serra
graphs updates
|
182
183
184
185
186
187
188
189
190
191
|
function resolveSelector(ctx, lang, args, expr, index) {
//XXX: Dehardcode!!!
let selectorName;
if (index[0].type === 'call' && index[0].expr.type === 'prop' &&
index[0].expr.expr.name === 'cldr') {
selectorName = 'plural';
} else {
selectorName = index[0].name;
}
const selector = resolveIdentifier(ctx, lang, args, selectorName)[1];
|
0e9aeacd
root
localization l20n
|
192
|
|
a1a3bc73
Luigi Serra
graphs updates
|
193
194
195
196
|
if (typeof selector !== 'function') {
// selector is a simple reference to an entity or args
return selector;
}
|
0e9aeacd
root
localization l20n
|
197
|
|
a1a3bc73
Luigi Serra
graphs updates
|
198
199
200
201
202
203
204
|
const argValue = index[0].args ?
resolveIdentifier(ctx, lang, args, index[0].args[0].name)[1] : undefined;
if (selectorName === 'plural') {
// special cases for zero, one, two if they are defined on the hash
if (argValue === 0 && 'zero' in expr) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
205
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
206
207
208
209
210
211
212
|
if (argValue === 1 && 'one' in expr) {
return 'one';
}
if (argValue === 2 && 'two' in expr) {
return 'two';
}
}
|
0e9aeacd
root
localization l20n
|
213
|
|
a1a3bc73
Luigi Serra
graphs updates
|
214
215
|
return selector(argValue);
}
|
0e9aeacd
root
localization l20n
|
216
|
|
a1a3bc73
Luigi Serra
graphs updates
|
217
218
219
220
|
function resolveValue(locals, ctx, lang, args, expr, index) {
if (!expr) {
return [locals, expr];
}
|
0e9aeacd
root
localization l20n
|
221
|
|
a1a3bc73
Luigi Serra
graphs updates
|
222
223
224
225
226
|
if (typeof expr === 'string' ||
typeof expr === 'boolean' ||
typeof expr === 'number') {
return [locals, expr];
}
|
0e9aeacd
root
localization l20n
|
227
|
|
a1a3bc73
Luigi Serra
graphs updates
|
228
229
230
|
if (Array.isArray(expr)) {
return interpolate(locals, ctx, lang, args, expr);
}
|
0e9aeacd
root
localization l20n
|
231
|
|
a1a3bc73
Luigi Serra
graphs updates
|
232
233
234
235
236
237
238
239
|
// otherwise, it's a dict
if (index) {
// try to use the index in order to select the right dict member
const selector = resolveSelector(ctx, lang, args, expr, index);
if (selector in expr) {
return resolveValue(locals, ctx, lang, args, expr[selector]);
}
}
|
0e9aeacd
root
localization l20n
|
240
|
|
a1a3bc73
Luigi Serra
graphs updates
|
241
242
243
244
245
246
|
// if there was no index or no selector was found, try the default
// XXX 'other' is an artifact from Gaia
const defaultKey = expr.__default || 'other';
if (defaultKey in expr) {
return resolveValue(locals, ctx, lang, args, expr[defaultKey]);
}
|
0e9aeacd
root
localization l20n
|
247
|
|
a1a3bc73
Luigi Serra
graphs updates
|
248
249
|
throw new L10nError('Unresolvable value');
}
|
0e9aeacd
root
localization l20n
|
250
|
|
a1a3bc73
Luigi Serra
graphs updates
|
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
const locales2rules = {
'af': 3,
'ak': 4,
'am': 4,
'ar': 1,
'asa': 3,
'az': 0,
'be': 11,
'bem': 3,
'bez': 3,
'bg': 3,
'bh': 4,
'bm': 0,
'bn': 3,
'bo': 0,
'br': 20,
'brx': 3,
'bs': 11,
'ca': 3,
'cgg': 3,
'chr': 3,
'cs': 12,
'cy': 17,
'da': 3,
'de': 3,
'dv': 3,
'dz': 0,
'ee': 3,
'el': 3,
'en': 3,
'eo': 3,
'es': 3,
'et': 3,
'eu': 3,
'fa': 0,
'ff': 5,
'fi': 3,
'fil': 4,
'fo': 3,
'fr': 5,
'fur': 3,
'fy': 3,
'ga': 8,
'gd': 24,
'gl': 3,
'gsw': 3,
'gu': 3,
'guw': 4,
'gv': 23,
'ha': 3,
'haw': 3,
'he': 2,
'hi': 4,
'hr': 11,
'hu': 0,
'id': 0,
'ig': 0,
'ii': 0,
'is': 3,
'it': 3,
'iu': 7,
'ja': 0,
'jmc': 3,
'jv': 0,
'ka': 0,
'kab': 5,
'kaj': 3,
'kcg': 3,
'kde': 0,
'kea': 0,
'kk': 3,
'kl': 3,
'km': 0,
'kn': 0,
'ko': 0,
'ksb': 3,
'ksh': 21,
'ku': 3,
'kw': 7,
'lag': 18,
'lb': 3,
'lg': 3,
'ln': 4,
'lo': 0,
'lt': 10,
'lv': 6,
'mas': 3,
'mg': 4,
'mk': 16,
'ml': 3,
'mn': 3,
'mo': 9,
'mr': 3,
'ms': 0,
'mt': 15,
'my': 0,
'nah': 3,
'naq': 7,
'nb': 3,
'nd': 3,
'ne': 3,
'nl': 3,
'nn': 3,
'no': 3,
'nr': 3,
'nso': 4,
'ny': 3,
'nyn': 3,
'om': 3,
'or': 3,
'pa': 3,
'pap': 3,
'pl': 13,
'ps': 3,
'pt': 3,
'rm': 3,
'ro': 9,
'rof': 3,
'ru': 11,
'rwk': 3,
'sah': 0,
'saq': 3,
'se': 7,
'seh': 3,
'ses': 0,
'sg': 0,
'sh': 11,
'shi': 19,
'sk': 12,
'sl': 14,
'sma': 7,
'smi': 7,
'smj': 7,
'smn': 7,
'sms': 7,
'sn': 3,
'so': 3,
'sq': 3,
'sr': 11,
'ss': 3,
'ssy': 3,
'st': 3,
'sv': 3,
'sw': 3,
'syr': 3,
'ta': 3,
'te': 3,
'teo': 3,
'th': 0,
'ti': 4,
'tig': 3,
'tk': 3,
'tl': 4,
'tn': 3,
'to': 0,
'tr': 0,
'ts': 3,
'tzm': 22,
'uk': 11,
'ur': 3,
've': 3,
'vi': 0,
'vun': 3,
'wa': 4,
'wae': 3,
'wo': 0,
'xh': 3,
'xog': 3,
'yo': 0,
'zh': 0,
'zu': 3
};
// utility functions for plural rules methods
function isIn(n, list) {
return list.indexOf(n) !== -1;
}
function isBetween(n, start, end) {
return typeof n === typeof start && start <= n && n <= end;
}
// list of all plural rules methods:
// map an integer to the plural form name to use
const pluralRules = {
'0': function() {
return 'other';
},
'1': function(n) {
if ((isBetween((n % 100), 3, 10))) {
return 'few';
}
if (n === 0) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
444
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
445
446
|
if ((isBetween((n % 100), 11, 99))) {
return 'many';
|
0e9aeacd
root
localization l20n
|
447
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
448
449
|
if (n === 2) {
return 'two';
|
0e9aeacd
root
localization l20n
|
450
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
451
452
|
if (n === 1) {
return 'one';
|
0e9aeacd
root
localization l20n
|
453
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
454
|
return 'other';
|
0e9aeacd
root
localization l20n
|
455
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
456
457
458
|
'2': function(n) {
if (n !== 0 && (n % 10) === 0) {
return 'many';
|
0e9aeacd
root
localization l20n
|
459
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
460
461
|
if (n === 2) {
return 'two';
|
0e9aeacd
root
localization l20n
|
462
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
463
464
|
if (n === 1) {
return 'one';
|
0e9aeacd
root
localization l20n
|
465
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
466
467
468
469
470
|
return 'other';
},
'3': function(n) {
if (n === 1) {
return 'one';
|
0e9aeacd
root
localization l20n
|
471
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
472
|
return 'other';
|
0e9aeacd
root
localization l20n
|
473
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
474
475
476
|
'4': function(n) {
if ((isBetween(n, 0, 1))) {
return 'one';
|
0e9aeacd
root
localization l20n
|
477
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
478
479
480
481
482
|
return 'other';
},
'5': function(n) {
if ((isBetween(n, 0, 2)) && n !== 2) {
return 'one';
|
0e9aeacd
root
localization l20n
|
483
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
484
|
return 'other';
|
0e9aeacd
root
localization l20n
|
485
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
486
487
488
|
'6': function(n) {
if (n === 0) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
489
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
490
491
|
if ((n % 10) === 1 && (n % 100) !== 11) {
return 'one';
|
0e9aeacd
root
localization l20n
|
492
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
493
|
return 'other';
|
0e9aeacd
root
localization l20n
|
494
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
495
496
497
|
'7': function(n) {
if (n === 2) {
return 'two';
|
0e9aeacd
root
localization l20n
|
498
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
499
500
501
502
|
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
503
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
504
505
506
|
'8': function(n) {
if ((isBetween(n, 3, 6))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
507
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
508
509
510
511
512
513
514
515
516
517
|
if ((isBetween(n, 7, 10))) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
518
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
519
520
521
|
'9': function(n) {
if (n === 0 || n !== 1 && (isBetween((n % 100), 1, 19))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
522
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
523
524
525
526
|
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
527
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
528
529
530
531
532
533
534
535
|
'10': function(n) {
if ((isBetween((n % 10), 2, 9)) && !(isBetween((n % 100), 11, 19))) {
return 'few';
}
if ((n % 10) === 1 && !(isBetween((n % 100), 11, 19))) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
536
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
537
538
539
540
541
542
543
544
545
546
547
|
'11': function(n) {
if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14))) {
return 'few';
}
if ((n % 10) === 0 ||
(isBetween((n % 10), 5, 9)) ||
(isBetween((n % 100), 11, 14))) {
return 'many';
}
if ((n % 10) === 1 && (n % 100) !== 11) {
return 'one';
|
0e9aeacd
root
localization l20n
|
548
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
549
|
return 'other';
|
0e9aeacd
root
localization l20n
|
550
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
551
552
553
|
'12': function(n) {
if ((isBetween(n, 2, 4))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
554
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
555
556
|
if (n === 1) {
return 'one';
|
0e9aeacd
root
localization l20n
|
557
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
558
|
return 'other';
|
0e9aeacd
root
localization l20n
|
559
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
560
561
562
|
'13': function(n) {
if ((isBetween((n % 10), 2, 4)) && !(isBetween((n % 100), 12, 14))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
563
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
564
565
566
567
|
if (n !== 1 && (isBetween((n % 10), 0, 1)) ||
(isBetween((n % 10), 5, 9)) ||
(isBetween((n % 100), 12, 14))) {
return 'many';
|
0e9aeacd
root
localization l20n
|
568
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
569
570
571
572
|
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
573
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
574
575
576
|
'14': function(n) {
if ((isBetween((n % 100), 3, 4))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
577
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
578
579
580
581
582
583
584
|
if ((n % 100) === 2) {
return 'two';
}
if ((n % 100) === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
585
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
586
587
588
589
590
591
592
593
594
595
596
|
'15': function(n) {
if (n === 0 || (isBetween((n % 100), 2, 10))) {
return 'few';
}
if ((isBetween((n % 100), 11, 19))) {
return 'many';
}
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
597
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
598
599
600
|
'16': function(n) {
if ((n % 10) === 1 && n !== 11) {
return 'one';
|
0e9aeacd
root
localization l20n
|
601
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
602
603
604
605
606
|
return 'other';
},
'17': function(n) {
if (n === 3) {
return 'few';
|
0e9aeacd
root
localization l20n
|
607
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
608
609
|
if (n === 0) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
610
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
611
612
613
614
615
616
617
618
619
620
|
if (n === 6) {
return 'many';
}
if (n === 2) {
return 'two';
}
if (n === 1) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
621
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
622
623
624
|
'18': function(n) {
if (n === 0) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
625
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
626
627
|
if ((isBetween(n, 0, 2)) && n !== 0 && n !== 2) {
return 'one';
|
0e9aeacd
root
localization l20n
|
628
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
629
630
631
632
633
|
return 'other';
},
'19': function(n) {
if ((isBetween(n, 2, 10))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
634
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
635
636
637
638
|
if ((isBetween(n, 0, 1))) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
639
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
640
641
642
643
644
645
646
|
'20': function(n) {
if ((isBetween((n % 10), 3, 4) || ((n % 10) === 9)) && !(
isBetween((n % 100), 10, 19) ||
isBetween((n % 100), 70, 79) ||
isBetween((n % 100), 90, 99)
)) {
return 'few';
|
0e9aeacd
root
localization l20n
|
647
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
648
649
|
if ((n % 1000000) === 0 && n !== 0) {
return 'many';
|
0e9aeacd
root
localization l20n
|
650
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
651
652
|
if ((n % 10) === 2 && !isIn((n % 100), [12, 72, 92])) {
return 'two';
|
0e9aeacd
root
localization l20n
|
653
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
654
655
|
if ((n % 10) === 1 && !isIn((n % 100), [11, 71, 91])) {
return 'one';
|
0e9aeacd
root
localization l20n
|
656
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
657
|
return 'other';
|
0e9aeacd
root
localization l20n
|
658
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
659
660
661
|
'21': function(n) {
if (n === 0) {
return 'zero';
|
0e9aeacd
root
localization l20n
|
662
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
663
664
|
if (n === 1) {
return 'one';
|
0e9aeacd
root
localization l20n
|
665
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
666
|
return 'other';
|
0e9aeacd
root
localization l20n
|
667
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
668
669
670
|
'22': function(n) {
if ((isBetween(n, 0, 1)) || (isBetween(n, 11, 99))) {
return 'one';
|
0e9aeacd
root
localization l20n
|
671
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
672
|
return 'other';
|
0e9aeacd
root
localization l20n
|
673
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
674
675
676
|
'23': function(n) {
if ((isBetween((n % 10), 1, 2)) || (n % 20) === 0) {
return 'one';
|
0e9aeacd
root
localization l20n
|
677
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
678
|
return 'other';
|
0e9aeacd
root
localization l20n
|
679
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
680
681
682
|
'24': function(n) {
if ((isBetween(n, 3, 10) || isBetween(n, 13, 19))) {
return 'few';
|
0e9aeacd
root
localization l20n
|
683
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
684
685
|
if (isIn(n, [2, 12])) {
return 'two';
|
0e9aeacd
root
localization l20n
|
686
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
687
688
689
690
|
if (isIn(n, [1, 11])) {
return 'one';
}
return 'other';
|
0e9aeacd
root
localization l20n
|
691
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
692
|
};
|
0e9aeacd
root
localization l20n
|
693
|
|
a1a3bc73
Luigi Serra
graphs updates
|
694
695
696
697
698
|
function getPluralRule(code) {
// return a function that gives the plural form name for a given integer
const index = locales2rules[code.replace(/-.*$/, '')];
if (!(index in pluralRules)) {
return function() { return 'other'; };
|
0e9aeacd
root
localization l20n
|
699
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
700
|
return pluralRules[index];
|
0e9aeacd
root
localization l20n
|
701
702
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
703
704
705
706
707
708
709
710
711
|
// Safari 9 and iOS 9 does not support Intl
const L20nIntl = typeof Intl !== 'undefined' ?
Intl : {
NumberFormat: function() {
return {
format: function(v) {
return v;
}
};
|
0e9aeacd
root
localization l20n
|
712
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
713
|
};
|
0e9aeacd
root
localization l20n
|
714
|
|
a1a3bc73
Luigi Serra
graphs updates
|
715
716
717
718
719
720
|
class Context {
constructor(env, langs, resIds) {
this.langs = langs;
this.resIds = resIds;
this.env = env;
this.emit = (type, evt) => env.emit(type, evt, this);
|
0e9aeacd
root
localization l20n
|
721
722
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
723
724
725
726
727
728
729
730
|
_formatTuple(lang, args, entity, id, key) {
try {
return format(this, lang, args, entity);
} catch (err) {
err.id = key ? id + '::' + key : id;
err.lang = lang;
this.emit('resolveerror', err);
return [{ error: err }, err.id];
|
0e9aeacd
root
localization l20n
|
731
|
}
|
0e9aeacd
root
localization l20n
|
732
733
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
734
735
|
_formatEntity(lang, args, entity, id) {
const [, value] = this._formatTuple(lang, args, entity, id);
|
0e9aeacd
root
localization l20n
|
736
|
|
a1a3bc73
Luigi Serra
graphs updates
|
737
738
739
740
|
const formatted = {
value,
attrs: null,
};
|
0e9aeacd
root
localization l20n
|
741
|
|
a1a3bc73
Luigi Serra
graphs updates
|
742
743
744
745
746
747
748
749
750
|
if (entity.attrs) {
formatted.attrs = Object.create(null);
for (let key in entity.attrs) {
/* jshint -W089 */
const [, attrValue] = this._formatTuple(
lang, args, entity.attrs[key], id, key);
formatted.attrs[key] = attrValue;
}
}
|
0e9aeacd
root
localization l20n
|
751
|
|
a1a3bc73
Luigi Serra
graphs updates
|
752
|
return formatted;
|
0e9aeacd
root
localization l20n
|
753
754
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
755
756
|
_formatValue(lang, args, entity, id) {
return this._formatTuple(lang, args, entity, id)[1];
|
0e9aeacd
root
localization l20n
|
757
758
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
759
760
761
|
fetch(langs = this.langs) {
if (langs.length === 0) {
return Promise.resolve(langs);
|
0e9aeacd
root
localization l20n
|
762
|
}
|
0e9aeacd
root
localization l20n
|
763
|
|
a1a3bc73
Luigi Serra
graphs updates
|
764
765
766
767
|
return Promise.all(
this.resIds.map(
resId => this.env._getResource(langs[0], resId))
).then(() => langs);
|
0e9aeacd
root
localization l20n
|
768
769
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
770
771
|
_resolve(langs, keys, formatter, prevResolved) {
const lang = langs[0];
|
0e9aeacd
root
localization l20n
|
772
|
|
a1a3bc73
Luigi Serra
graphs updates
|
773
774
775
|
if (!lang) {
return reportMissing.call(this, keys, formatter, prevResolved);
}
|
0e9aeacd
root
localization l20n
|
776
|
|
a1a3bc73
Luigi Serra
graphs updates
|
777
|
let hasUnresolved = false;
|
0e9aeacd
root
localization l20n
|
778
|
|
a1a3bc73
Luigi Serra
graphs updates
|
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
const resolved = keys.map((key, i) => {
if (prevResolved && prevResolved[i] !== undefined) {
return prevResolved[i];
}
const [id, args] = Array.isArray(key) ?
key : [key, undefined];
const entity = this._getEntity(lang, id);
if (entity) {
return formatter.call(this, lang, args, entity, id);
}
this.emit('notfounderror',
new L10nError('"' + id + '"' + ' not found in ' + lang.code,
id, lang));
hasUnresolved = true;
});
if (!hasUnresolved) {
return resolved;
|
0e9aeacd
root
localization l20n
|
799
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
|
return this.fetch(langs.slice(1)).then(
nextLangs => this._resolve(nextLangs, keys, formatter, resolved));
}
formatEntities(...keys) {
return this.fetch().then(
langs => this._resolve(langs, keys, this._formatEntity));
}
formatValues(...keys) {
return this.fetch().then(
langs => this._resolve(langs, keys, this._formatValue));
}
_getEntity(lang, id) {
const cache = this.env.resCache;
// Look for `id` in every resource in order.
for (let i = 0, resId; resId = this.resIds[i]; i++) {
const resource = cache.get(resId + lang.code + lang.src);
if (resource instanceof L10nError) {
continue;
}
if (id in resource) {
return resource[id];
}
|
0e9aeacd
root
localization l20n
|
827
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
828
829
830
831
832
833
|
return undefined;
}
_getNumberFormatter(lang) {
if (!this.env.numberFormatters) {
this.env.numberFormatters = new Map();
|
0e9aeacd
root
localization l20n
|
834
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
835
836
837
838
|
if (!this.env.numberFormatters.has(lang)) {
const formatter = L20nIntl.NumberFormat(lang);
this.env.numberFormatters.set(lang, formatter);
return formatter;
|
0e9aeacd
root
localization l20n
|
839
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
840
841
842
843
844
845
846
847
848
849
850
|
return this.env.numberFormatters.get(lang);
}
// XXX in the future macros will be stored in localization resources together
// with regular entities and this method will not be needed anymore
_getMacro(lang, id) {
switch(id) {
case 'plural':
return getPluralRule(lang.code);
default:
return undefined;
|
0e9aeacd
root
localization l20n
|
851
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
852
853
854
855
856
857
858
859
860
861
|
}
}
function reportMissing(keys, formatter, resolved) {
const missingIds = new Set();
keys.forEach((key, i) => {
if (resolved && resolved[i] !== undefined) {
return;
|
0e9aeacd
root
localization l20n
|
862
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
const id = Array.isArray(key) ? key[0] : key;
missingIds.add(id);
resolved[i] = formatter === this._formatValue ?
id : {value: id, attrs: null};
});
this.emit('notfounderror', new L10nError(
'"' + Array.from(missingIds).join(', ') + '"' +
' not found in any language', missingIds));
return resolved;
}
var MAX_PLACEABLES = 100;
var PropertiesParser = {
patterns: null,
entryIds: null,
emit: null,
init: function() {
this.patterns = {
comment: /^\s*#|^\s*$/,
entity: /^([^=\s]+)\s*=\s*(.*)$/,
multiline: /[^\\]\\$/,
index: /\{\[\s*(\w+)(?:\(([^\)]*)\))?\s*\]\}/i,
unicode: /\\u([0-9a-fA-F]{1,4})/g,
entries: /[^\r\n]+/g,
controlChars: /\\([\\\n\r\t\b\f\{\}\"\'])/g,
placeables: /\{\{\s*([^\s]*?)\s*\}\}/,
};
|
0e9aeacd
root
localization l20n
|
894
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
895
896
897
898
|
parse: function(emit, source) {
if (!this.patterns) {
this.init();
|
0e9aeacd
root
localization l20n
|
899
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
900
901
902
903
904
905
906
|
this.emit = emit;
var entries = {};
var lines = source.match(this.patterns.entries);
if (!lines) {
return entries;
|
0e9aeacd
root
localization l20n
|
907
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (this.patterns.comment.test(line)) {
continue;
}
while (this.patterns.multiline.test(line) && i < lines.length) {
line = line.slice(0, -1) + lines[++i].trim();
}
var entityMatch = line.match(this.patterns.entity);
if (entityMatch) {
try {
this.parseEntity(entityMatch[1], entityMatch[2], entries);
} catch (e) {
if (!this.emit) {
throw e;
}
}
}
|
0e9aeacd
root
localization l20n
|
929
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
930
|
return entries;
|
0e9aeacd
root
localization l20n
|
931
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
932
933
934
935
936
937
938
939
940
941
942
|
parseEntity: function(id, value, entries) {
var name, key;
var pos = id.indexOf('[');
if (pos !== -1) {
name = id.substr(0, pos);
key = id.substring(pos + 1, id.length - 1);
} else {
name = id;
key = null;
|
0e9aeacd
root
localization l20n
|
943
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
944
945
946
947
948
949
|
var nameElements = name.split('.');
if (nameElements.length > 2) {
throw this.error('Error in ID: "' + name + '".' +
' Nested attributes are not supported.');
|
0e9aeacd
root
localization l20n
|
950
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
951
952
953
954
955
956
957
958
959
960
961
|
var attr;
if (nameElements.length > 1) {
name = nameElements[0];
attr = nameElements[1];
if (attr[0] === '$') {
throw this.error('Attribute can\'t start with "$"');
}
} else {
attr = null;
|
0e9aeacd
root
localization l20n
|
962
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
963
964
|
this.setEntityValue(name, attr, key, this.unescapeString(value), entries);
|
0e9aeacd
root
localization l20n
|
965
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
966
967
968
969
970
971
972
973
974
975
976
977
978
|
setEntityValue: function(id, attr, key, rawValue, entries) {
var value = rawValue.indexOf('{{') > -1 ?
this.parseString(rawValue) : rawValue;
var isSimpleValue = typeof value === 'string';
var root = entries;
var isSimpleNode = typeof entries[id] === 'string';
if (!entries[id] && (attr || key || !isSimpleValue)) {
entries[id] = Object.create(null);
isSimpleNode = false;
|
0e9aeacd
root
localization l20n
|
979
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
|
if (attr) {
if (isSimpleNode) {
const val = entries[id];
entries[id] = Object.create(null);
entries[id].value = val;
}
if (!entries[id].attrs) {
entries[id].attrs = Object.create(null);
}
if (!entries[id].attrs && !isSimpleValue) {
entries[id].attrs[attr] = Object.create(null);
}
root = entries[id].attrs;
id = attr;
|
0e9aeacd
root
localization l20n
|
995
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
|
if (key) {
isSimpleNode = false;
if (typeof root[id] === 'string') {
const val = root[id];
root[id] = Object.create(null);
root[id].index = this.parseIndex(val);
root[id].value = Object.create(null);
}
root = root[id].value;
id = key;
isSimpleValue = true;
|
0e9aeacd
root
localization l20n
|
1008
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
if (isSimpleValue) {
if (id in root) {
throw this.error('Duplicated id: ' + id);
}
root[id] = value;
} else {
if (!root[id]) {
root[id] = Object.create(null);
}
root[id].value = value;
|
0e9aeacd
root
localization l20n
|
1020
|
}
|
0e9aeacd
root
localization l20n
|
1021
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
|
parseString: function(str) {
var chunks = str.split(this.patterns.placeables);
var complexStr = [];
var len = chunks.length;
var placeablesCount = (len - 1) / 2;
if (placeablesCount >= MAX_PLACEABLES) {
throw this.error('Too many placeables (' + placeablesCount +
', max allowed is ' + MAX_PLACEABLES + ')');
|
0e9aeacd
root
localization l20n
|
1033
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
for (var i = 0; i < chunks.length; i++) {
if (chunks[i].length === 0) {
continue;
}
if (i % 2 === 1) {
complexStr.push({type: 'idOrVar', name: chunks[i]});
} else {
complexStr.push(chunks[i]);
}
|
0e9aeacd
root
localization l20n
|
1044
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1045
|
return complexStr;
|
0e9aeacd
root
localization l20n
|
1046
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1047
1048
1049
1050
|
unescapeString: function(str) {
if (str.lastIndexOf('\\') !== -1) {
str = str.replace(this.patterns.controlChars, '$1');
|
0e9aeacd
root
localization l20n
|
1051
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1052
1053
1054
|
return str.replace(this.patterns.unicode, function(match, token) {
return String.fromCodePoint(parseInt(token, 16));
});
|
0e9aeacd
root
localization l20n
|
1055
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1056
1057
1058
1059
1060
|
parseIndex: function(str) {
var match = str.match(this.patterns.index);
if (!match) {
throw new L10nError('Malformed index');
|
0e9aeacd
root
localization l20n
|
1061
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
|
if (match[2]) {
return [{
type: 'call',
expr: {
type: 'prop',
expr: {
type: 'glob',
name: 'cldr'
},
prop: 'plural',
cmpt: false
}, args: [{
type: 'idOrVar',
name: match[2]
}]
}];
} else {
return [{type: 'idOrVar', name: match[1]}];
|
0e9aeacd
root
localization l20n
|
1080
|
}
|
0e9aeacd
root
localization l20n
|
1081
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1082
1083
1084
1085
1086
|
error: function(msg, type = 'parsererror') {
const err = new L10nError(msg);
if (this.emit) {
this.emit(type, err);
|
0e9aeacd
root
localization l20n
|
1087
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
return err;
}
};
const MAX_PLACEABLES$1 = 100;
var L20nParser = {
parse: function(emit, string) {
this._source = string;
this._index = 0;
this._length = string.length;
this.entries = Object.create(null);
this.emit = emit;
return this.getResource();
|
0e9aeacd
root
localization l20n
|
1103
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
|
getResource: function() {
this.getWS();
while (this._index < this._length) {
try {
this.getEntry();
} catch (e) {
if (e instanceof L10nError) {
// we want to recover, but we don't need it in entries
this.getJunkEntry();
if (!this.emit) {
throw e;
}
} else {
throw e;
}
}
if (this._index < this._length) {
this.getWS();
}
|
0e9aeacd
root
localization l20n
|
1125
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1126
1127
|
return this.entries;
|
0e9aeacd
root
localization l20n
|
1128
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
getEntry: function() {
if (this._source[this._index] === '<') {
++this._index;
const id = this.getIdentifier();
if (this._source[this._index] === '[') {
++this._index;
return this.getEntity(id, this.getItemList(this.getExpression, ']'));
}
return this.getEntity(id);
|
0e9aeacd
root
localization l20n
|
1139
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1140
1141
1142
|
if (this._source.startsWith('/*', this._index)) {
return this.getComment();
|
0e9aeacd
root
localization l20n
|
1143
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1144
1145
|
throw this.error('Invalid entry');
|
0e9aeacd
root
localization l20n
|
1146
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1147
1148
1149
1150
|
getEntity: function(id, index) {
if (!this.getRequiredWS()) {
throw this.error('Expected white space');
|
0e9aeacd
root
localization l20n
|
1151
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
|
const ch = this._source[this._index];
const hasIndex = index !== undefined;
const value = this.getValue(ch, hasIndex, hasIndex);
let attrs;
if (value === undefined) {
if (ch === '>') {
throw this.error('Expected ">"');
}
attrs = this.getAttributes();
} else {
const ws1 = this.getRequiredWS();
if (this._source[this._index] !== '>') {
if (!ws1) {
throw this.error('Expected ">"');
}
attrs = this.getAttributes();
}
|
0e9aeacd
root
localization l20n
|
1171
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1172
1173
1174
1175
1176
1177
|
// skip '>'
++this._index;
if (id in this.entries) {
throw this.error('Duplicate entry ID "' + id, 'duplicateerror');
|
0e9aeacd
root
localization l20n
|
1178
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1179
1180
1181
1182
1183
1184
1185
1186
|
if (!attrs && !index && typeof value === 'string') {
this.entries[id] = value;
} else {
this.entries[id] = {
value,
attrs,
index
};
|
0e9aeacd
root
localization l20n
|
1187
|
}
|
0e9aeacd
root
localization l20n
|
1188
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
getValue: function(
ch = this._source[this._index], index = false, required = true) {
switch (ch) {
case '\'':
case '"':
return this.getString(ch, 1);
case '{':
return this.getHash(index);
|
0e9aeacd
root
localization l20n
|
1198
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1199
1200
1201
|
if (required) {
throw this.error('Unknown value type');
|
0e9aeacd
root
localization l20n
|
1202
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1203
1204
1205
1206
1207
1208
1209
1210
1211
|
return;
},
getWS: function() {
let cc = this._source.charCodeAt(this._index);
// space, \n, \t, \r
while (cc === 32 || cc === 10 || cc === 9 || cc === 13) {
cc = this._source.charCodeAt(++this._index);
|
0e9aeacd
root
localization l20n
|
1212
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1213
1214
1215
1216
1217
1218
1219
1220
|
},
getRequiredWS: function() {
const pos = this._index;
let cc = this._source.charCodeAt(pos);
// space, \n, \t, \r
while (cc === 32 || cc === 10 || cc === 9 || cc === 13) {
cc = this._source.charCodeAt(++this._index);
|
0e9aeacd
root
localization l20n
|
1221
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1222
|
return this._index !== pos;
|
0e9aeacd
root
localization l20n
|
1223
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
|
getIdentifier: function() {
const start = this._index;
let cc = this._source.charCodeAt(this._index);
if ((cc >= 97 && cc <= 122) || // a-z
(cc >= 65 && cc <= 90) || // A-Z
cc === 95) { // _
cc = this._source.charCodeAt(++this._index);
} else {
throw this.error('Identifier has to start with [a-zA-Z_]');
|
0e9aeacd
root
localization l20n
|
1235
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1236
1237
1238
1239
1240
1241
|
while ((cc >= 97 && cc <= 122) || // a-z
(cc >= 65 && cc <= 90) || // A-Z
(cc >= 48 && cc <= 57) || // 0-9
cc === 95) { // _
cc = this._source.charCodeAt(++this._index);
|
0e9aeacd
root
localization l20n
|
1242
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1243
1244
|
return this._source.slice(start, this._index);
|
0e9aeacd
root
localization l20n
|
1245
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
|
getUnicodeChar: function() {
for (let i = 0; i < 4; i++) {
let cc = this._source.charCodeAt(++this._index);
if ((cc > 96 && cc < 103) || // a-f
(cc > 64 && cc < 71) || // A-F
(cc > 47 && cc < 58)) { // 0-9
continue;
}
throw this.error('Illegal unicode escape sequence');
|
0e9aeacd
root
localization l20n
|
1256
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1257
1258
1259
|
this._index++;
return String.fromCharCode(
parseInt(this._source.slice(this._index - 4, this._index), 16));
|
0e9aeacd
root
localization l20n
|
1260
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
|
stringRe: /"|'|{{|\\/g,
getString: function(opchar, opcharLen) {
const body = [];
let placeables = 0;
this._index += opcharLen;
const start = this._index;
let bufStart = start;
let buf = '';
while (true) {
this.stringRe.lastIndex = this._index;
const match = this.stringRe.exec(this._source);
if (!match) {
throw this.error('Unclosed string literal');
}
if (match[0] === '"' || match[0] === '\'') {
if (match[0] !== opchar) {
this._index += opcharLen;
continue;
}
this._index = match.index + opcharLen;
break;
}
if (match[0] === '{{') {
if (placeables > MAX_PLACEABLES$1 - 1) {
throw this.error('Too many placeables, maximum allowed is ' +
MAX_PLACEABLES$1);
}
placeables++;
if (match.index > bufStart || buf.length > 0) {
body.push(buf + this._source.slice(bufStart, match.index));
buf = '';
}
this._index = match.index + 2;
this.getWS();
body.push(this.getExpression());
this.getWS();
this._index += 2;
bufStart = this._index;
continue;
}
if (match[0] === '\\') {
this._index = match.index + 1;
const ch2 = this._source[this._index];
if (ch2 === 'u') {
buf += this._source.slice(bufStart, match.index) +
this.getUnicodeChar();
} else if (ch2 === opchar || ch2 === '\\') {
buf += this._source.slice(bufStart, match.index) + ch2;
this._index++;
} else if (this._source.startsWith('{{', this._index)) {
buf += this._source.slice(bufStart, match.index) + '{{';
this._index += 2;
} else {
throw this.error('Illegal escape sequence');
}
bufStart = this._index;
}
|
0e9aeacd
root
localization l20n
|
1326
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1327
1328
1329
|
if (body.length === 0) {
return buf + this._source.slice(bufStart, this._index - opcharLen);
|
0e9aeacd
root
localization l20n
|
1330
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1331
1332
1333
|
if (this._index - opcharLen > bufStart || buf.length > 0) {
body.push(buf + this._source.slice(bufStart, this._index - opcharLen));
|
0e9aeacd
root
localization l20n
|
1334
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
|
return body;
},
getAttributes: function() {
const attrs = Object.create(null);
while (true) {
this.getAttribute(attrs);
const ws1 = this.getRequiredWS();
const ch = this._source.charAt(this._index);
if (ch === '>') {
break;
} else if (!ws1) {
throw this.error('Expected ">"');
}
|
0e9aeacd
root
localization l20n
|
1351
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1352
|
return attrs;
|
0e9aeacd
root
localization l20n
|
1353
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1354
1355
1356
1357
1358
1359
1360
1361
1362
|
getAttribute: function(attrs) {
const key = this.getIdentifier();
let index;
if (this._source[this._index]=== '[') {
++this._index;
this.getWS();
index = this.getItemList(this.getExpression, ']');
|
0e9aeacd
root
localization l20n
|
1363
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1364
1365
1366
|
this.getWS();
if (this._source[this._index] !== ':') {
throw this.error('Expected ":"');
|
0e9aeacd
root
localization l20n
|
1367
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1368
1369
1370
1371
1372
1373
1374
|
++this._index;
this.getWS();
const hasIndex = index !== undefined;
const value = this.getValue(undefined, hasIndex);
if (key in attrs) {
throw this.error('Duplicate attribute "' + key, 'duplicateerror');
|
0e9aeacd
root
localization l20n
|
1375
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1376
1377
1378
1379
1380
1381
1382
1383
|
if (!index && typeof value === 'string') {
attrs[key] = value;
} else {
attrs[key] = {
value,
index
};
|
0e9aeacd
root
localization l20n
|
1384
|
}
|
0e9aeacd
root
localization l20n
|
1385
|
},
|
a1a3bc73
Luigi Serra
graphs updates
|
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
|
getHash: function(index) {
const items = Object.create(null);
++this._index;
this.getWS();
let defKey;
while (true) {
const [key, value, def] = this.getHashItem();
items[key] = value;
if (def) {
if (defKey) {
throw this.error('Default item redefinition forbidden');
}
defKey = key;
}
this.getWS();
const comma = this._source[this._index] === ',';
if (comma) {
++this._index;
this.getWS();
}
if (this._source[this._index] === '}') {
++this._index;
break;
}
if (!comma) {
throw this.error('Expected "}"');
}
|
0e9aeacd
root
localization l20n
|
1419
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1420
1421
1422
1423
1424
|
if (defKey) {
items.__default = defKey;
} else if (!index) {
throw this.error('Unresolvable Hash Value');
|
0e9aeacd
root
localization l20n
|
1425
|
}
|
0e9aeacd
root
localization l20n
|
1426
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1427
1428
|
return items;
},
|
0e9aeacd
root
localization l20n
|
1429
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1430
1431
1432
1433
1434
1435
|
getHashItem: function() {
let defItem = false;
if (this._source[this._index] === '*') {
++this._index;
defItem = true;
}
|
0e9aeacd
root
localization l20n
|
1436
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1437
1438
1439
1440
|
const key = this.getIdentifier();
this.getWS();
if (this._source[this._index] !== ':') {
throw this.error('Expected ":"');
|
0e9aeacd
root
localization l20n
|
1441
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1442
1443
|
++this._index;
this.getWS();
|
0e9aeacd
root
localization l20n
|
1444
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1445
1446
|
return [key, this.getValue(), defItem];
},
|
0e9aeacd
root
localization l20n
|
1447
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1448
1449
1450
1451
|
getComment: function() {
this._index += 2;
const start = this._index;
const end = this._source.indexOf('*/', start);
|
0e9aeacd
root
localization l20n
|
1452
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1453
1454
|
if (end === -1) {
throw this.error('Comment without a closing tag');
|
0e9aeacd
root
localization l20n
|
1455
1456
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1457
1458
|
this._index = end + 2;
},
|
0e9aeacd
root
localization l20n
|
1459
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1460
1461
|
getExpression: function () {
let exp = this.getPrimaryExpression();
|
0e9aeacd
root
localization l20n
|
1462
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
|
while (true) {
let ch = this._source[this._index];
if (ch === '.' || ch === '[') {
++this._index;
exp = this.getPropertyExpression(exp, ch === '[');
} else if (ch === '(') {
++this._index;
exp = this.getCallExpression(exp);
} else {
break;
}
|
0e9aeacd
root
localization l20n
|
1474
1475
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1476
1477
|
return exp;
},
|
0e9aeacd
root
localization l20n
|
1478
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1479
1480
|
getPropertyExpression: function(idref, computed) {
let exp;
|
0e9aeacd
root
localization l20n
|
1481
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
|
if (computed) {
this.getWS();
exp = this.getExpression();
this.getWS();
if (this._source[this._index] !== ']') {
throw this.error('Expected "]"');
}
++this._index;
} else {
exp = this.getIdentifier();
|
0e9aeacd
root
localization l20n
|
1492
1493
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1494
1495
1496
1497
1498
1499
1500
|
return {
type: 'prop',
expr: idref,
prop: exp,
cmpt: computed
};
},
|
0e9aeacd
root
localization l20n
|
1501
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1502
1503
|
getCallExpression: function(callee) {
this.getWS();
|
0e9aeacd
root
localization l20n
|
1504
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1505
1506
1507
1508
1509
1510
|
return {
type: 'call',
expr: callee,
args: this.getItemList(this.getExpression, ')')
};
},
|
0e9aeacd
root
localization l20n
|
1511
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1512
1513
|
getPrimaryExpression: function() {
const ch = this._source[this._index];
|
0e9aeacd
root
localization l20n
|
1514
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
|
switch (ch) {
case '$':
++this._index;
return {
type: 'var',
name: this.getIdentifier()
};
case '@':
++this._index;
return {
type: 'glob',
name: this.getIdentifier()
};
default:
return {
type: 'id',
name: this.getIdentifier()
};
|
0e9aeacd
root
localization l20n
|
1533
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1534
|
},
|
0e9aeacd
root
localization l20n
|
1535
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1536
1537
1538
|
getItemList: function(callback, closeChar) {
const items = [];
let closed = false;
|
0e9aeacd
root
localization l20n
|
1539
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1540
|
this.getWS();
|
0e9aeacd
root
localization l20n
|
1541
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1542
1543
1544
1545
|
if (this._source[this._index] === closeChar) {
++this._index;
closed = true;
}
|
0e9aeacd
root
localization l20n
|
1546
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
|
while (!closed) {
items.push(callback.call(this));
this.getWS();
let ch = this._source.charAt(this._index);
switch (ch) {
case ',':
++this._index;
this.getWS();
break;
case closeChar:
++this._index;
closed = true;
break;
default:
throw this.error('Expected "," or "' + closeChar + '"');
|
0e9aeacd
root
localization l20n
|
1562
1563
|
}
}
|
0e9aeacd
root
localization l20n
|
1564
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1565
1566
|
return items;
},
|
0e9aeacd
root
localization l20n
|
1567
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
|
getJunkEntry: function() {
const pos = this._index;
let nextEntity = this._source.indexOf('<', pos);
let nextComment = this._source.indexOf('/*', pos);
if (nextEntity === -1) {
nextEntity = this._length;
}
if (nextComment === -1) {
nextComment = this._length;
|
0e9aeacd
root
localization l20n
|
1579
|
}
|
0e9aeacd
root
localization l20n
|
1580
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1581
|
let nextEntry = Math.min(nextEntity, nextComment);
|
0e9aeacd
root
localization l20n
|
1582
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1583
1584
|
this._index = nextEntry;
},
|
0e9aeacd
root
localization l20n
|
1585
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1586
1587
|
error: function(message, type = 'parsererror') {
const pos = this._index;
|
0e9aeacd
root
localization l20n
|
1588
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1589
1590
1591
1592
|
let start = this._source.lastIndexOf('<', pos - 1);
const lastClose = this._source.lastIndexOf('>', pos - 1);
start = lastClose > start ? lastClose + 1 : start;
const context = this._source.slice(start, pos + 10);
|
0e9aeacd
root
localization l20n
|
1593
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1594
1595
1596
1597
1598
1599
1600
1601
|
const msg = message + ' at pos ' + pos + ': `' + context + '`';
const err = new L10nError(msg);
if (this.emit) {
this.emit(type, err);
}
return err;
},
};
|
0e9aeacd
root
localization l20n
|
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
|
// Walk an entry node searching for content leaves
function walkEntry(entry, fn) {
if (typeof entry === 'string') {
return fn(entry);
}
const newEntry = Object.create(null);
if (entry.value) {
newEntry.value = walkValue(entry.value, fn);
}
if (entry.index) {
newEntry.index = entry.index;
}
if (entry.attrs) {
newEntry.attrs = Object.create(null);
for (let key in entry.attrs) {
newEntry.attrs[key] = walkEntry(entry.attrs[key], fn);
}
}
return newEntry;
}
function walkValue(value, fn) {
if (typeof value === 'string') {
return fn(value);
}
// skip expressions in placeables
if (value.type) {
return value;
}
const newValue = Array.isArray(value) ? [] : Object.create(null);
const keys = Object.keys(value);
for (let i = 0, key; (key = keys[i]); i++) {
newValue[key] = walkValue(value[key], fn);
}
return newValue;
}
/* Pseudolocalizations
*
* pseudo is a dict of strategies to be used to modify the English
* context in order to create pseudolocalizations. These can be used by
* developers to test the localizability of their code without having to
* actually speak a foreign language.
*
* Currently, the following pseudolocales are supported:
*
* fr-x-psaccent - Ȧȧƈƈḗḗƞŧḗḗḓ Ḗḗƞɠŀīīşħ
*
* In Accented English all English letters are replaced by accented
* Unicode counterparts which don't impair the readability of the content.
* This allows developers to quickly test if any given string is being
* correctly displayed in its 'translated' form. Additionally, simple
* heuristics are used to make certain words longer to better simulate the
* experience of international users.
*
* ar-x-psbidi - ɥsıʅƃuƎ ıpıԐ
*
* Bidi English is a fake RTL locale. All words are surrounded by
* Unicode formatting marks forcing the RTL directionality of characters.
* In addition, to make the reversed text easier to read, individual
* letters are flipped.
*
* Note: The name above is hardcoded to be RTL in case code editors have
* trouble with the RLO and PDF Unicode marks. In reality, it should be
* surrounded by those marks as well.
*
* See https://bugzil.la/900182 for more information.
*
*/
function createGetter(id, name) {
let _pseudo = null;
return function getPseudo() {
if (_pseudo) {
return _pseudo;
}
const reAlphas = /[a-zA-Z]/g;
const reVowels = /[aeiouAEIOU]/g;
const reWords = /[^\W0-9_]+/g;
// strftime tokens (%a, %Eb), template {vars}, HTML entities (‪)
// and HTML tags.
const reExcluded = /(%[EO]?\w|\{\s*.+?\s*\}|&[#\w]+;|<\s*.+?\s*>)/;
const charMaps = {
'fr-x-psaccent':
'ȦƁƇḒḖƑƓĦĪĴĶĿḾȠǾƤɊŘŞŦŬṼẆẊẎẐ[\\]^_`ȧƀƈḓḗƒɠħīĵķŀḿƞǿƥɋřşŧŭṽẇẋẏẑ',
'ar-x-psbidi':
// XXX Use pɟפ˥ʎ as replacements for ᗡℲ⅁⅂⅄. https://bugzil.la/1007340
'∀ԐↃpƎɟפHIſӼ˥WNOԀÒᴚS⊥∩ɅMXʎZ[\\]ᵥ_,ɐqɔpǝɟƃɥıɾʞʅɯuodbɹsʇnʌʍxʎz',
};
const mods = {
'fr-x-psaccent': val =>
val.replace(reVowels, match => match + match.toLowerCase()),
// Surround each word with Unicode formatting codes, RLO and PDF:
// U+202E: RIGHT-TO-LEFT OVERRIDE (RLO)
// U+202C: POP DIRECTIONAL FORMATTING (PDF)
// See http://www.w3.org/International/questions/qa-bidi-controls
'ar-x-psbidi': val =>
val.replace(reWords, match => '\u202e' + match + '\u202c'),
};
// Replace each Latin letter with a Unicode character from map
const replaceChars =
(map, val) => val.replace(
reAlphas, match => map.charAt(match.charCodeAt(0) - 65));
const transform =
val => replaceChars(charMaps[id], mods[id](val));
// apply fn to translatable parts of val
const apply = (fn, val) => {
if (!val) {
return val;
}
const parts = val.split(reExcluded);
const modified = parts.map(function(part) {
if (reExcluded.test(part)) {
return part;
}
return fn(part);
});
return modified.join('');
};
return _pseudo = {
name: transform(name),
process: str => apply(transform, str)
};
};
}
const pseudo = Object.defineProperties(Object.create(null), {
'fr-x-psaccent': {
enumerable: true,
get: createGetter('fr-x-psaccent', 'Runtime Accented')
},
'ar-x-psbidi': {
enumerable: true,
get: createGetter('ar-x-psbidi', 'Runtime Bidi')
}
});
function emit(listeners, ...args) {
const type = args.shift();
if (listeners['*']) {
listeners['*'].slice().forEach(
listener => listener.apply(this, args));
}
if (listeners[type]) {
listeners[type].slice().forEach(
listener => listener.apply(this, args));
}
}
function addEventListener(listeners, type, listener) {
if (!(type in listeners)) {
listeners[type] = [];
}
listeners[type].push(listener);
}
function removeEventListener(listeners, type, listener) {
const typeListeners = listeners[type];
const pos = typeListeners.indexOf(listener);
if (pos === -1) {
return;
}
typeListeners.splice(pos, 1);
}
|
0e9aeacd
root
localization l20n
|
1790
|
class Env {
|
a1a3bc73
Luigi Serra
graphs updates
|
1791
|
constructor(fetchResource) {
|
0e9aeacd
root
localization l20n
|
1792
1793
|
this.fetchResource = fetchResource;
|
a1a3bc73
Luigi Serra
graphs updates
|
1794
1795
1796
1797
1798
1799
1800
|
this.resCache = new Map();
this.resRefs = new Map();
this.numberFormatters = null;
this.parsers = {
properties: PropertiesParser,
l20n: L20nParser,
};
|
0e9aeacd
root
localization l20n
|
1801
1802
1803
1804
1805
1806
1807
|
const listeners = {};
this.emit = emit.bind(this, listeners);
this.addEventListener = addEventListener.bind(this, listeners);
this.removeEventListener = removeEventListener.bind(this, listeners);
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1808
1809
1810
1811
1812
1813
1814
|
createContext(langs, resIds) {
const ctx = new Context(this, langs, resIds);
resIds.forEach(resId => {
const usedBy = this.resRefs.get(resId) || 0;
this.resRefs.set(resId, usedBy + 1);
});
|
0e9aeacd
root
localization l20n
|
1815
1816
1817
1818
|
return ctx;
}
destroyContext(ctx) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1819
1820
|
ctx.resIds.forEach(resId => {
const usedBy = this.resRefs.get(resId) || 0;
|
0e9aeacd
root
localization l20n
|
1821
|
|
a1a3bc73
Luigi Serra
graphs updates
|
1822
1823
1824
1825
1826
1827
1828
1829
|
if (usedBy > 1) {
return this.resRefs.set(resId, usedBy - 1);
}
this.resRefs.delete(resId);
this.resCache.forEach((val, key) =>
key.startsWith(resId) ? this.resCache.delete(key) : null);
});
|
0e9aeacd
root
localization l20n
|
1830
1831
1832
|
}
_parse(syntax, lang, data) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1833
|
const parser = this.parsers[syntax];
|
0e9aeacd
root
localization l20n
|
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
|
if (!parser) {
return data;
}
const emit = (type, err) => this.emit(type, amendError(lang, err));
return parser.parse.call(parser, emit, data);
}
_create(lang, entries) {
if (lang.src !== 'pseudo') {
return entries;
}
const pseudoentries = Object.create(null);
for (let key in entries) {
pseudoentries[key] = walkEntry(
entries[key], pseudo[lang.code].process);
}
return pseudoentries;
}
_getResource(lang, res) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1856
|
const cache = this.resCache;
|
0e9aeacd
root
localization l20n
|
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
|
const id = res + lang.code + lang.src;
if (cache.has(id)) {
return cache.get(id);
}
const syntax = res.substr(res.lastIndexOf('.') + 1);
const saveEntries = data => {
const entries = this._parse(syntax, lang, data);
cache.set(id, this._create(lang, entries));
};
const recover = err => {
err.lang = lang;
this.emit('fetcherror', err);
cache.set(id, err);
};
const langToFetch = lang.src === 'pseudo' ?
|
a1a3bc73
Luigi Serra
graphs updates
|
1877
|
{ code: 'en-US', src: 'app', ver: lang.ver } :
|
0e9aeacd
root
localization l20n
|
1878
1879
|
lang;
|
a1a3bc73
Luigi Serra
graphs updates
|
1880
1881
|
const resource = this.fetchResource(res, langToFetch)
.then(saveEntries, recover);
|
0e9aeacd
root
localization l20n
|
1882
1883
1884
1885
1886
1887
1888
|
cache.set(id, resource);
return resource;
}
}
|
0e9aeacd
root
localization l20n
|
1889
1890
1891
1892
1893
|
function amendError(lang, err) {
err.lang = lang;
return err;
}
|
0e9aeacd
root
localization l20n
|
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
|
function prioritizeLocales(def, availableLangs, requested) {
let supportedLocale;
// Find the first locale in the requested list that is supported.
for (let i = 0; i < requested.length; i++) {
const locale = requested[i];
if (availableLangs.indexOf(locale) !== -1) {
supportedLocale = locale;
break;
}
}
if (!supportedLocale ||
supportedLocale === def) {
return [def];
}
return [supportedLocale, def];
}
|
0e9aeacd
root
localization l20n
|
1912
|
function negotiateLanguages(
|
a1a3bc73
Luigi Serra
graphs updates
|
1913
|
{ appVersion, defaultLang, availableLangs }, additionalLangs, prevLangs,
|
0e9aeacd
root
localization l20n
|
1914
1915
|
requestedLangs) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1916
1917
1918
|
const allAvailableLangs = Object.keys(availableLangs)
.concat(Object.keys(additionalLangs))
.concat(Object.keys(pseudo));
|
0e9aeacd
root
localization l20n
|
1919
1920
1921
1922
1923
1924
|
const newLangs = prioritizeLocales(
defaultLang, allAvailableLangs, requestedLangs);
const langs = newLangs.map(code => ({
code: code,
src: getLangSource(appVersion, availableLangs, additionalLangs, code),
|
a1a3bc73
Luigi Serra
graphs updates
|
1925
|
ver: appVersion,
|
0e9aeacd
root
localization l20n
|
1926
1927
|
}));
|
a1a3bc73
Luigi Serra
graphs updates
|
1928
|
return { langs, haveChanged: !arrEqual(prevLangs, newLangs) };
|
0e9aeacd
root
localization l20n
|
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
|
}
function arrEqual(arr1, arr2) {
return arr1.length === arr2.length &&
arr1.every((elem, i) => elem === arr2[i]);
}
function getMatchingLangpack(appVersion, langpacks) {
for (let i = 0, langpack; (langpack = langpacks[i]); i++) {
if (langpack.target === appVersion) {
return langpack;
}
}
return null;
}
function getLangSource(appVersion, availableLangs, additionalLangs, code) {
if (additionalLangs && additionalLangs[code]) {
const lp = getMatchingLangpack(appVersion, additionalLangs[code]);
if (lp &&
(!(code in availableLangs) ||
parseInt(lp.revision) > availableLangs[code])) {
return 'extra';
}
}
if ((code in pseudo) && !(code in availableLangs)) {
return 'pseudo';
}
return 'app';
}
class Remote {
|
a1a3bc73
Luigi Serra
graphs updates
|
1963
|
constructor(fetchResource, broadcast) {
|
0e9aeacd
root
localization l20n
|
1964
|
this.broadcast = broadcast;
|
a1a3bc73
Luigi Serra
graphs updates
|
1965
|
this.env = new Env(fetchResource);
|
0e9aeacd
root
localization l20n
|
1966
|
this.ctxs = new Map();
|
0e9aeacd
root
localization l20n
|
1967
1968
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1969
1970
1971
1972
1973
|
registerView(view, resources, meta, additionalLangs, requestedLangs) {
const { langs } = negotiateLanguages(
meta, additionalLangs, [], requestedLangs);
this.ctxs.set(view, this.env.createContext(langs, resources));
return langs;
|
0e9aeacd
root
localization l20n
|
1974
1975
1976
|
}
unregisterView(view) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1977
1978
|
this.ctxs.delete(view);
return true;
|
0e9aeacd
root
localization l20n
|
1979
1980
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1981
1982
|
formatEntities(view, keys) {
return this.ctxs.get(view).formatEntities(...keys);
|
0e9aeacd
root
localization l20n
|
1983
1984
1985
|
}
formatValues(view, keys) {
|
a1a3bc73
Luigi Serra
graphs updates
|
1986
|
return this.ctxs.get(view).formatValues(...keys);
|
0e9aeacd
root
localization l20n
|
1987
1988
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
1989
1990
1991
1992
1993
1994
1995
1996
|
changeLanguages(view, meta, additionalLangs, requestedLangs) {
const oldCtx = this.ctxs.get(view);
const prevLangs = oldCtx.langs;
const newLangs = negotiateLanguages(
meta, additionalLangs, prevLangs, requestedLangs);
this.ctxs.set(view, this.env.createContext(
newLangs.langs, oldCtx.resIds));
return newLangs;
|
0e9aeacd
root
localization l20n
|
1997
1998
1999
|
}
requestLanguages(requestedLangs) {
|
a1a3bc73
Luigi Serra
graphs updates
|
2000
|
this.broadcast('languageschangerequest', requestedLangs);
|
0e9aeacd
root
localization l20n
|
2001
2002
2003
2004
2005
2006
2007
2008
2009
|
}
getName(code) {
return pseudo[code].name;
}
processString(code, str) {
return pseudo[code].process(str);
}
|
0e9aeacd
root
localization l20n
|
2010
2011
|
}
|
a1a3bc73
Luigi Serra
graphs updates
|
2012
|
const remote = new Remote(fetchResource, broadcast);
|
0e9aeacd
root
localization l20n
|
2013
2014
2015
|
remote.service = new Service('l20n')
.method('registerView', (...args) => remote.registerView(...args))
|
0e9aeacd
root
localization l20n
|
2016
|
.method('requestLanguages', (...args) => remote.requestLanguages(...args))
|
a1a3bc73
Luigi Serra
graphs updates
|
2017
2018
|
.method('changeLanguages', (...args) => remote.changeLanguages(...args))
.method('formatEntities', (...args) => remote.formatEntities(...args))
|
0e9aeacd
root
localization l20n
|
2019
2020
2021
2022
2023
2024
2025
|
.method('formatValues', (...args) => remote.formatValues(...args))
.method('getName', (...args) => remote.getName(...args))
.method('processString', (...args) => remote.processString(...args))
.on('disconnect', clientId => remote.unregisterView(clientId))
.listen(channel);
})();
|