Blame view

bower_components/l20n/dist/compat/gaia/build/l20n.js 65.6 KB
0e9aeacd   root   localization l20n
1
2
3
4
5
6
7
8
9
10
11
12
13
  'use strict';
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  
  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;
  
a1a3bc73   Luigi Serra   graphs updates
14
15
16
17
  function fetchResource(htmloptimizer, res, _ref) {
    var code = _ref.code;
  
    var url = decodeURI(res).replace('{locale}', code);
0e9aeacd   root   localization l20n
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
  
    var _htmloptimizer$getFileByRelativePath = htmloptimizer.getFileByRelativePath(url);
  
    var file = _htmloptimizer$getFileByRelativePath.file;
    var content = _htmloptimizer$getFileByRelativePath.content;
  
    if (!file) {
      return Promise.reject(new L10nError('Not found: ' + url));
    }
  
    var parsed = res.endsWith('.json') ? JSON.parse(content) : content;
    return Promise.resolve(parsed);
  }
  
  function walkEntry(entry, fn) {
    if (typeof entry === 'string') {
      return fn(entry);
    }
  
    var newEntry = Object.create(null);
  
    if (entry.value) {
      newEntry.value = walkValue$1(entry.value, fn);
    }
  
    if (entry.index) {
      newEntry.index = entry.index;
    }
  
    if (entry.attrs) {
      newEntry.attrs = Object.create(null);
      for (var key in entry.attrs) {
        newEntry.attrs[key] = walkEntry(entry.attrs[key], fn);
      }
    }
  
    return newEntry;
  }
  
  function walkValue$1(value, fn) {
    if (typeof value === 'string') {
      return fn(value);
    }
  
    if (value.type) {
      return value;
    }
  
    var newValue = Array.isArray(value) ? [] : Object.create(null);
    var keys = Object.keys(value);
  
    for (var i = 0, key = undefined; key = keys[i]; i++) {
      newValue[key] = walkValue$1(value[key], fn);
    }
  
    return newValue;
  }
  
  function createGetter(id, name) {
    var _pseudo = null;
  
    return function getPseudo() {
      if (_pseudo) {
        return _pseudo;
      }
  
      var reAlphas = /[a-zA-Z]/g;
      var reVowels = /[aeiouAEIOU]/g;
      var reWords = /[^\W0-9_]+/g;
  
      var reExcluded = /(%[EO]?\w|\{\s*.+?\s*\}|&[#\w]+;|<\s*.+?\s*>)/;
  
      var charMaps = {
        'fr-x-psaccent': 'ȦƁƇḒḖƑƓĦĪĴĶĿḾȠǾƤɊŘŞŦŬṼẆẊẎẐ[\\]^_`ȧƀƈḓḗƒɠħīĵķŀḿƞǿƥɋřşŧŭṽẇẋẏẑ',
        'ar-x-psbidi': '∀ԐↃpƎɟפHIſӼ˥WNOԀÒᴚS⊥∩ɅMXʎZ[\\]ᵥ_,ɐqɔpǝɟƃɥıɾʞʅɯuodbɹsʇnʌʍxʎz'
      };
  
      var mods = {
        'fr-x-psaccent': function (val) {
          return val.replace(reVowels, function (match) {
            return match + match.toLowerCase();
          });
        },
  
        'ar-x-psbidi': function (val) {
          return val.replace(reWords, function (match) {
            return '‮' + match + '‬';
          });
        }
      };
  
      var replaceChars = function (map, val) {
        return val.replace(reAlphas, function (match) {
          return map.charAt(match.charCodeAt(0) - 65);
        });
      };
  
      var transform = function (val) {
        return replaceChars(charMaps[id], mods[id](val));
      };
  
      var apply = function (fn, val) {
        if (!val) {
          return val;
        }
  
        var parts = val.split(reExcluded);
        var modified = parts.map(function (part) {
          if (reExcluded.test(part)) {
            return part;
          }
          return fn(part);
        });
        return modified.join('');
      };
  
      return _pseudo = {
        name: transform(name),
        process: function (str) {
          return apply(transform, str);
        }
      };
    };
  }
  
  var pseudo$1 = 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')
    }
  });
  
a1a3bc73   Luigi Serra   graphs updates
154
155
  var KNOWN_MACROS$1 = ['plural'];
  var MAX_PLACEABLE_LENGTH$1 = 2500;
0e9aeacd   root   localization l20n
156
  
a1a3bc73   Luigi Serra   graphs updates
157
158
  var FSI$1 = '⁨';
  var PDI$1 = '⁩';
0e9aeacd   root   localization l20n
159
  
a1a3bc73   Luigi Serra   graphs updates
160
  var resolutionChain$1 = new WeakSet();
0e9aeacd   root   localization l20n
161
  
a1a3bc73   Luigi Serra   graphs updates
162
163
164
165
  function format$1(ctx, lang, args, entity) {
    if (typeof entity === 'string') {
      return [{}, entity];
    }
0e9aeacd   root   localization l20n
166
  
a1a3bc73   Luigi Serra   graphs updates
167
168
169
    if (resolutionChain$1.has(entity)) {
      throw new L10nError('Cyclic reference detected');
    }
0e9aeacd   root   localization l20n
170
  
a1a3bc73   Luigi Serra   graphs updates
171
    resolutionChain$1.add(entity);
0e9aeacd   root   localization l20n
172
  
a1a3bc73   Luigi Serra   graphs updates
173
    var rv = undefined;
0e9aeacd   root   localization l20n
174
  
a1a3bc73   Luigi Serra   graphs updates
175
176
177
178
179
180
181
    try {
      rv = resolveValue$1({}, ctx, lang, args, entity.value, entity.index);
    } finally {
      resolutionChain$1.delete(entity);
    }
    return rv;
  }
0e9aeacd   root   localization l20n
182
  
a1a3bc73   Luigi Serra   graphs updates
183
184
185
186
  function resolveIdentifier$1(ctx, lang, args, id) {
    if (KNOWN_MACROS$1.indexOf(id) > -1) {
      return [{}, ctx._getMacro(lang, id)];
    }
0e9aeacd   root   localization l20n
187
  
a1a3bc73   Luigi Serra   graphs updates
188
189
190
    if (args && args.hasOwnProperty(id)) {
      if (typeof args[id] === 'string' || typeof args[id] === 'number' && !isNaN(args[id])) {
        return [{}, args[id]];
0e9aeacd   root   localization l20n
191
      } else {
a1a3bc73   Luigi Serra   graphs updates
192
        throw new L10nError('Arg must be a string or a number: ' + id);
0e9aeacd   root   localization l20n
193
      }
a1a3bc73   Luigi Serra   graphs updates
194
    }
0e9aeacd   root   localization l20n
195
  
a1a3bc73   Luigi Serra   graphs updates
196
197
198
    if (id === '__proto__') {
      throw new L10nError('Illegal id: ' + id);
    }
0e9aeacd   root   localization l20n
199
  
a1a3bc73   Luigi Serra   graphs updates
200
    var entity = ctx._getEntity(lang, id);
0e9aeacd   root   localization l20n
201
  
a1a3bc73   Luigi Serra   graphs updates
202
203
204
    if (entity) {
      return format$1(ctx, lang, args, entity);
    }
0e9aeacd   root   localization l20n
205
  
a1a3bc73   Luigi Serra   graphs updates
206
207
    throw new L10nError('Unknown reference: ' + id);
  }
0e9aeacd   root   localization l20n
208
  
a1a3bc73   Luigi Serra   graphs updates
209
210
211
  function subPlaceable$1(locals, ctx, lang, args, id) {
    var newLocals = undefined,
        value = undefined;
0e9aeacd   root   localization l20n
212
  
a1a3bc73   Luigi Serra   graphs updates
213
214
    try {
      var _resolveIdentifier$1 = resolveIdentifier$1(ctx, lang, args, id);
0e9aeacd   root   localization l20n
215
  
a1a3bc73   Luigi Serra   graphs updates
216
217
218
219
220
      newLocals = _resolveIdentifier$1[0];
      value = _resolveIdentifier$1[1];
    } catch (err) {
      return [{ error: err }, FSI$1 + '{{ ' + id + ' }}' + PDI$1];
    }
0e9aeacd   root   localization l20n
221
  
a1a3bc73   Luigi Serra   graphs updates
222
223
224
225
    if (typeof value === 'number') {
      var formatter = ctx._getNumberFormatter(lang);
      return [newLocals, formatter.format(value)];
    }
0e9aeacd   root   localization l20n
226
  
a1a3bc73   Luigi Serra   graphs updates
227
228
229
    if (typeof value === 'string') {
      if (value.length >= MAX_PLACEABLE_LENGTH$1) {
        throw new L10nError('Too many characters in placeable (' + value.length + ', max allowed is ' + MAX_PLACEABLE_LENGTH$1 + ')');
0e9aeacd   root   localization l20n
230
      }
a1a3bc73   Luigi Serra   graphs updates
231
232
      return [newLocals, FSI$1 + value + PDI$1];
    }
0e9aeacd   root   localization l20n
233
  
a1a3bc73   Luigi Serra   graphs updates
234
235
    return [{}, FSI$1 + '{{ ' + id + ' }}' + PDI$1];
  }
0e9aeacd   root   localization l20n
236
  
a1a3bc73   Luigi Serra   graphs updates
237
238
239
240
241
242
243
  function interpolate$1(locals, ctx, lang, args, arr) {
    return arr.reduce(function (_ref2, cur) {
      var localsSeq = _ref2[0];
      var valueSeq = _ref2[1];
  
      if (typeof cur === 'string') {
        return [localsSeq, valueSeq + cur];
0e9aeacd   root   localization l20n
244
      } else {
a1a3bc73   Luigi Serra   graphs updates
245
        var _subPlaceable$1 = subPlaceable$1(locals, ctx, lang, args, cur.name);
0e9aeacd   root   localization l20n
246
  
a1a3bc73   Luigi Serra   graphs updates
247
248
249
        var value = _subPlaceable$1[1];
  
        return [localsSeq, valueSeq + value];
0e9aeacd   root   localization l20n
250
      }
a1a3bc73   Luigi Serra   graphs updates
251
252
    }, [locals, '']);
  }
0e9aeacd   root   localization l20n
253
  
a1a3bc73   Luigi Serra   graphs updates
254
255
256
257
258
259
260
261
  function resolveSelector$1(ctx, lang, args, expr, index) {
    var selectorName = undefined;
    if (index[0].type === 'call' && index[0].expr.type === 'prop' && index[0].expr.expr.name === 'cldr') {
      selectorName = 'plural';
    } else {
      selectorName = index[0].name;
    }
    var selector = resolveIdentifier$1(ctx, lang, args, selectorName)[1];
0e9aeacd   root   localization l20n
262
  
a1a3bc73   Luigi Serra   graphs updates
263
264
265
    if (typeof selector !== 'function') {
      return selector;
    }
0e9aeacd   root   localization l20n
266
  
a1a3bc73   Luigi Serra   graphs updates
267
    var argValue = index[0].args ? resolveIdentifier$1(ctx, lang, args, index[0].args[0].name)[1] : undefined;
0e9aeacd   root   localization l20n
268
  
a1a3bc73   Luigi Serra   graphs updates
269
270
271
272
273
274
275
276
277
278
279
    if (selectorName === 'plural') {
      if (argValue === 0 && 'zero' in expr) {
        return 'zero';
      }
      if (argValue === 1 && 'one' in expr) {
        return 'one';
      }
      if (argValue === 2 && 'two' in expr) {
        return 'two';
      }
    }
0e9aeacd   root   localization l20n
280
  
a1a3bc73   Luigi Serra   graphs updates
281
282
    return selector(argValue);
  }
0e9aeacd   root   localization l20n
283
  
a1a3bc73   Luigi Serra   graphs updates
284
285
286
287
  function resolveValue$1(locals, ctx, lang, args, expr, index) {
    if (!expr) {
      return [locals, expr];
    }
0e9aeacd   root   localization l20n
288
  
a1a3bc73   Luigi Serra   graphs updates
289
290
291
    if (typeof expr === 'string' || typeof expr === 'boolean' || typeof expr === 'number') {
      return [locals, expr];
    }
0e9aeacd   root   localization l20n
292
  
a1a3bc73   Luigi Serra   graphs updates
293
294
295
    if (Array.isArray(expr)) {
      return interpolate$1(locals, ctx, lang, args, expr);
    }
0e9aeacd   root   localization l20n
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  
    if (index) {
      var selector = resolveSelector$1(ctx, lang, args, expr, index);
      if (selector in expr) {
        return resolveValue$1(locals, ctx, lang, args, expr[selector]);
      }
    }
  
    var defaultKey = expr.__default || 'other';
    if (defaultKey in expr) {
      return resolveValue$1(locals, ctx, lang, args, expr[defaultKey]);
    }
  
    throw new L10nError('Unresolvable value');
  }
  
  var 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
  };
  
  function isIn(n, list) {
    return list.indexOf(n) !== -1;
  }
  function isBetween(n, start, end) {
    return typeof n === typeof start && start <= n && n <= end;
  }
  
  var pluralRules = {
    '0': function () {
      return 'other';
    },
a1a3bc73   Luigi Serra   graphs updates
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    '1': function (n) {
      if (isBetween(n % 100, 3, 10)) {
        return 'few';
      }
      if (n === 0) {
        return 'zero';
      }
      if (isBetween(n % 100, 11, 99)) {
        return 'many';
      }
      if (n === 2) {
        return 'two';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '2': function (n) {
      if (n !== 0 && n % 10 === 0) {
        return 'many';
      }
      if (n === 2) {
        return 'two';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '3': function (n) {
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '4': function (n) {
      if (isBetween(n, 0, 1)) {
        return 'one';
      }
      return 'other';
    },
    '5': function (n) {
      if (isBetween(n, 0, 2) && n !== 2) {
        return 'one';
      }
      return 'other';
    },
    '6': function (n) {
      if (n === 0) {
        return 'zero';
      }
      if (n % 10 === 1 && n % 100 !== 11) {
        return 'one';
      }
      return 'other';
    },
    '7': function (n) {
      if (n === 2) {
        return 'two';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '8': function (n) {
      if (isBetween(n, 3, 6)) {
        return 'few';
      }
      if (isBetween(n, 7, 10)) {
        return 'many';
      }
      if (n === 2) {
        return 'two';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '9': function (n) {
      if (n === 0 || n !== 1 && isBetween(n % 100, 1, 19)) {
        return 'few';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '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';
    },
    '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';
      }
      return 'other';
    },
    '12': function (n) {
      if (isBetween(n, 2, 4)) {
        return 'few';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '13': function (n) {
      if (isBetween(n % 10, 2, 4) && !isBetween(n % 100, 12, 14)) {
        return 'few';
      }
      if (n !== 1 && isBetween(n % 10, 0, 1) || isBetween(n % 10, 5, 9) || isBetween(n % 100, 12, 14)) {
        return 'many';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '14': function (n) {
      if (isBetween(n % 100, 3, 4)) {
        return 'few';
      }
      if (n % 100 === 2) {
        return 'two';
      }
      if (n % 100 === 1) {
        return 'one';
      }
      return 'other';
    },
    '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';
    },
    '16': function (n) {
      if (n % 10 === 1 && n !== 11) {
        return 'one';
      }
      return 'other';
    },
    '17': function (n) {
      if (n === 3) {
0e9aeacd   root   localization l20n
660
661
662
663
664
        return 'few';
      }
      if (n === 0) {
        return 'zero';
      }
a1a3bc73   Luigi Serra   graphs updates
665
      if (n === 6) {
0e9aeacd   root   localization l20n
666
667
668
669
670
671
672
673
674
675
        return 'many';
      }
      if (n === 2) {
        return 'two';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
a1a3bc73   Luigi Serra   graphs updates
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    '18': function (n) {
      if (n === 0) {
        return 'zero';
      }
      if (isBetween(n, 0, 2) && n !== 0 && n !== 2) {
        return 'one';
      }
      return 'other';
    },
    '19': function (n) {
      if (isBetween(n, 2, 10)) {
        return 'few';
      }
      if (isBetween(n, 0, 1)) {
        return 'one';
      }
      return 'other';
    },
    '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';
      }
      if (n % 1000000 === 0 && n !== 0) {
0e9aeacd   root   localization l20n
699
700
        return 'many';
      }
a1a3bc73   Luigi Serra   graphs updates
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
      if (n % 10 === 2 && !isIn(n % 100, [12, 72, 92])) {
        return 'two';
      }
      if (n % 10 === 1 && !isIn(n % 100, [11, 71, 91])) {
        return 'one';
      }
      return 'other';
    },
    '21': function (n) {
      if (n === 0) {
        return 'zero';
      }
      if (n === 1) {
        return 'one';
      }
      return 'other';
    },
    '22': function (n) {
      if (isBetween(n, 0, 1) || isBetween(n, 11, 99)) {
        return 'one';
      }
      return 'other';
    },
    '23': function (n) {
      if (isBetween(n % 10, 1, 2) || n % 20 === 0) {
        return 'one';
      }
      return 'other';
    },
    '24': function (n) {
      if (isBetween(n, 3, 10) || isBetween(n, 13, 19)) {
        return 'few';
      }
      if (isIn(n, [2, 12])) {
0e9aeacd   root   localization l20n
735
736
        return 'two';
      }
a1a3bc73   Luigi Serra   graphs updates
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
      if (isIn(n, [1, 11])) {
        return 'one';
      }
      return 'other';
    }
  };
  
  function getPluralRule(code) {
    var index = locales2rules[code.replace(/-.*$/, '')];
    if (!(index in pluralRules)) {
      return function () {
        return 'other';
      };
    }
    return pluralRules[index];
  }
  
  var L20nIntl = typeof Intl !== 'undefined' ? Intl : {
    NumberFormat: function () {
      return {
        format: function (v) {
          return v;
        }
      };
    }
  };
  
  var Context = (function () {
    function Context(env, langs, resIds) {
      var _this = this;
  
      _classCallCheck(this, Context);
  
      this.langs = langs;
      this.resIds = resIds;
      this.env = env;
      this.emit = function (type, evt) {
        return env.emit(type, evt, _this);
      };
    }
  
    Context.prototype._formatTuple = function _formatTuple(lang, args, entity, id, key) {
      try {
        return format$1(this, lang, args, entity);
      } catch (err) {
        err.id = key ? id + '::' + key : id;
        err.lang = lang;
        this.emit('resolveerror', err);
        return [{ error: err }, err.id];
      }
    };
  
    Context.prototype._formatEntity = function _formatEntity(lang, args, entity, id) {
      var _formatTuple2 = this._formatTuple(lang, args, entity, id);
  
      var value = _formatTuple2[1];
  
      var formatted = {
        value: value,
        attrs: null
      };
  
      if (entity.attrs) {
        formatted.attrs = Object.create(null);
        for (var key in entity.attrs) {
          var _formatTuple3 = this._formatTuple(lang, args, entity.attrs[key], id, key);
  
          var attrValue = _formatTuple3[1];
  
          formatted.attrs[key] = attrValue;
        }
      }
  
      return formatted;
    };
  
    Context.prototype._formatValue = function _formatValue(lang, args, entity, id) {
      return this._formatTuple(lang, args, entity, id)[1];
    };
  
    Context.prototype.fetch = function fetch() {
      var _this2 = this;
  
      var langs = arguments.length <= 0 || arguments[0] === undefined ? this.langs : arguments[0];
  
      if (langs.length === 0) {
        return Promise.resolve(langs);
      }
  
      return Promise.all(this.resIds.map(function (resId) {
        return _this2.env._getResource(langs[0], resId);
      })).then(function () {
        return langs;
      });
    };
  
    Context.prototype._resolve = function _resolve(langs, keys, formatter, prevResolved) {
      var _this3 = this;
  
      var lang = langs[0];
  
      if (!lang) {
        return reportMissing.call(this, keys, formatter, prevResolved);
      }
  
      var hasUnresolved = false;
  
      var resolved = keys.map(function (key, i) {
        if (prevResolved && prevResolved[i] !== undefined) {
          return prevResolved[i];
        }
  
        var _ref3 = Array.isArray(key) ? key : [key, undefined];
  
        var id = _ref3[0];
        var args = _ref3[1];
  
        var entity = _this3._getEntity(lang, id);
  
        if (entity) {
          return formatter.call(_this3, lang, args, entity, id);
        }
  
        _this3.emit('notfounderror', new L10nError('"' + id + '"' + ' not found in ' + lang.code, id, lang));
        hasUnresolved = true;
      });
  
      if (!hasUnresolved) {
        return resolved;
0e9aeacd   root   localization l20n
866
      }
a1a3bc73   Luigi Serra   graphs updates
867
868
869
870
871
872
873
874
875
876
877
  
      return this.fetch(langs.slice(1)).then(function (nextLangs) {
        return _this3._resolve(nextLangs, keys, formatter, resolved);
      });
    };
  
    Context.prototype.formatEntities = function formatEntities() {
      var _this4 = this;
  
      for (var _len = arguments.length, keys = Array(_len), _key = 0; _key < _len; _key++) {
        keys[_key] = arguments[_key];
0e9aeacd   root   localization l20n
878
      }
a1a3bc73   Luigi Serra   graphs updates
879
880
881
882
883
884
885
886
887
888
889
  
      return this.fetch().then(function (langs) {
        return _this4._resolve(langs, keys, _this4._formatEntity);
      });
    };
  
    Context.prototype.formatValues = function formatValues() {
      var _this5 = this;
  
      for (var _len2 = arguments.length, keys = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        keys[_key2] = arguments[_key2];
0e9aeacd   root   localization l20n
890
      }
a1a3bc73   Luigi Serra   graphs updates
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
  
      return this.fetch().then(function (langs) {
        return _this5._resolve(langs, keys, _this5._formatValue);
      });
    };
  
    Context.prototype._getEntity = function _getEntity(lang, id) {
      var cache = this.env.resCache;
  
      for (var i = 0, resId = undefined; resId = this.resIds[i]; i++) {
        var resource = cache.get(resId + lang.code + lang.src);
        if (resource instanceof L10nError) {
          continue;
        }
        if (id in resource) {
          return resource[id];
        }
0e9aeacd   root   localization l20n
908
      }
a1a3bc73   Luigi Serra   graphs updates
909
910
911
912
913
914
      return undefined;
    };
  
    Context.prototype._getNumberFormatter = function _getNumberFormatter(lang) {
      if (!this.env.numberFormatters) {
        this.env.numberFormatters = new Map();
0e9aeacd   root   localization l20n
915
      }
a1a3bc73   Luigi Serra   graphs updates
916
917
918
919
      if (!this.env.numberFormatters.has(lang)) {
        var formatter = L20nIntl.NumberFormat(lang);
        this.env.numberFormatters.set(lang, formatter);
        return formatter;
0e9aeacd   root   localization l20n
920
      }
a1a3bc73   Luigi Serra   graphs updates
921
922
923
924
925
926
927
928
929
      return this.env.numberFormatters.get(lang);
    };
  
    Context.prototype._getMacro = function _getMacro(lang, id) {
      switch (id) {
        case 'plural':
          return getPluralRule(lang.code);
        default:
          return undefined;
0e9aeacd   root   localization l20n
930
      }
a1a3bc73   Luigi Serra   graphs updates
931
932
933
934
935
936
937
938
939
940
941
942
943
    };
  
    return Context;
  })();
  
  function reportMissing(keys, formatter, resolved) {
    var _this6 = this;
  
    var missingIds = new Set();
  
    keys.forEach(function (key, i) {
      if (resolved && resolved[i] !== undefined) {
        return;
0e9aeacd   root   localization l20n
944
      }
a1a3bc73   Luigi Serra   graphs updates
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
      var id = Array.isArray(key) ? key[0] : key;
      missingIds.add(id);
      resolved[i] = formatter === _this6._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
973
    },
a1a3bc73   Luigi Serra   graphs updates
974
975
976
977
  
    parse: function (emit, source) {
      if (!this.patterns) {
        this.init();
0e9aeacd   root   localization l20n
978
      }
a1a3bc73   Luigi Serra   graphs updates
979
980
981
982
983
984
985
      this.emit = emit;
  
      var entries = {};
  
      var lines = source.match(this.patterns.entries);
      if (!lines) {
        return entries;
0e9aeacd   root   localization l20n
986
      }
a1a3bc73   Luigi Serra   graphs updates
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
      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
1008
      }
a1a3bc73   Luigi Serra   graphs updates
1009
      return entries;
0e9aeacd   root   localization l20n
1010
    },
a1a3bc73   Luigi Serra   graphs updates
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  
    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
1022
      }
a1a3bc73   Luigi Serra   graphs updates
1023
1024
1025
1026
1027
  
      var nameElements = name.split('.');
  
      if (nameElements.length > 2) {
        throw this.error('Error in ID: "' + name + '".' + ' Nested attributes are not supported.');
0e9aeacd   root   localization l20n
1028
      }
a1a3bc73   Luigi Serra   graphs updates
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  
      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
1040
      }
a1a3bc73   Luigi Serra   graphs updates
1041
1042
  
      this.setEntityValue(name, attr, key, this.unescapeString(value), entries);
0e9aeacd   root   localization l20n
1043
    },
a1a3bc73   Luigi Serra   graphs updates
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
  
    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
1056
      }
a1a3bc73   Luigi Serra   graphs updates
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
  
      if (attr) {
        if (isSimpleNode) {
          var 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;
      }
  
      if (key) {
        isSimpleNode = false;
        if (typeof root[id] === 'string') {
          var 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
1085
      }
a1a3bc73   Luigi Serra   graphs updates
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  
      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
1097
      }
0e9aeacd   root   localization l20n
1098
    },
a1a3bc73   Luigi Serra   graphs updates
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
  
    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
1109
      }
a1a3bc73   Luigi Serra   graphs updates
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  
      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
1120
      }
a1a3bc73   Luigi Serra   graphs updates
1121
      return complexStr;
0e9aeacd   root   localization l20n
1122
    },
a1a3bc73   Luigi Serra   graphs updates
1123
1124
1125
1126
  
    unescapeString: function (str) {
      if (str.lastIndexOf('\\') !== -1) {
        str = str.replace(this.patterns.controlChars, '$1');
0e9aeacd   root   localization l20n
1127
      }
a1a3bc73   Luigi Serra   graphs updates
1128
1129
1130
      return str.replace(this.patterns.unicode, function (match, token) {
        return String.fromCodePoint(parseInt(token, 16));
      });
0e9aeacd   root   localization l20n
1131
    },
a1a3bc73   Luigi Serra   graphs updates
1132
1133
1134
1135
1136
  
    parseIndex: function (str) {
      var match = str.match(this.patterns.index);
      if (!match) {
        throw new L10nError('Malformed index');
0e9aeacd   root   localization l20n
1137
      }
a1a3bc73   Luigi Serra   graphs updates
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
      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
1156
      }
0e9aeacd   root   localization l20n
1157
    },
a1a3bc73   Luigi Serra   graphs updates
1158
1159
1160
1161
1162
1163
1164
  
    error: function (msg) {
      var type = arguments.length <= 1 || arguments[1] === undefined ? 'parsererror' : arguments[1];
  
      var err = new L10nError(msg);
      if (this.emit) {
        this.emit(type, err);
0e9aeacd   root   localization l20n
1165
      }
a1a3bc73   Luigi Serra   graphs updates
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
      return err;
    }
  };
  
  var 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
1181
    },
a1a3bc73   Luigi Serra   graphs updates
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  
    getResource: function () {
      this.getWS();
      while (this._index < this._length) {
        try {
          this.getEntry();
        } catch (e) {
          if (e instanceof L10nError) {
            this.getJunkEntry();
            if (!this.emit) {
              throw e;
            }
          } else {
            throw e;
          }
        }
  
        if (this._index < this._length) {
          this.getWS();
        }
0e9aeacd   root   localization l20n
1202
      }
a1a3bc73   Luigi Serra   graphs updates
1203
1204
  
      return this.entries;
0e9aeacd   root   localization l20n
1205
    },
a1a3bc73   Luigi Serra   graphs updates
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
  
    getEntry: function () {
      if (this._source[this._index] === '<') {
        ++this._index;
        var 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
1216
      }
a1a3bc73   Luigi Serra   graphs updates
1217
1218
1219
  
      if (this._source.startsWith('/*', this._index)) {
        return this.getComment();
0e9aeacd   root   localization l20n
1220
      }
a1a3bc73   Luigi Serra   graphs updates
1221
1222
1223
1224
1225
1226
1227
  
      throw this.error('Invalid entry');
    },
  
    getEntity: function (id, index) {
      if (!this.getRequiredWS()) {
        throw this.error('Expected white space');
0e9aeacd   root   localization l20n
1228
      }
a1a3bc73   Luigi Serra   graphs updates
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  
      var ch = this._source[this._index];
      var hasIndex = index !== undefined;
      var value = this.getValue(ch, hasIndex, hasIndex);
      var attrs = undefined;
  
      if (value === undefined) {
        if (ch === '>') {
          throw this.error('Expected ">"');
        }
        attrs = this.getAttributes();
      } else {
        var ws1 = this.getRequiredWS();
        if (this._source[this._index] !== '>') {
          if (!ws1) {
            throw this.error('Expected ">"');
          }
          attrs = this.getAttributes();
        }
0e9aeacd   root   localization l20n
1248
      }
a1a3bc73   Luigi Serra   graphs updates
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
  
      ++this._index;
  
      if (id in this.entries) {
        throw this.error('Duplicate entry ID "' + id, 'duplicateerror');
      }
      if (!attrs && !index && typeof value === 'string') {
        this.entries[id] = value;
      } else {
        this.entries[id] = {
          value: value,
          attrs: attrs,
          index: index
        };
0e9aeacd   root   localization l20n
1263
      }
0e9aeacd   root   localization l20n
1264
    },
a1a3bc73   Luigi Serra   graphs updates
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
  
    getValue: function () {
      var ch = arguments.length <= 0 || arguments[0] === undefined ? this._source[this._index] : arguments[0];
      var index = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
      var required = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
  
      switch (ch) {
        case '\'':
        case '"':
          return this.getString(ch, 1);
        case '{':
          return this.getHash(index);
0e9aeacd   root   localization l20n
1277
      }
a1a3bc73   Luigi Serra   graphs updates
1278
1279
1280
  
      if (required) {
        throw this.error('Unknown value type');
0e9aeacd   root   localization l20n
1281
      }
a1a3bc73   Luigi Serra   graphs updates
1282
1283
  
      return;
0e9aeacd   root   localization l20n
1284
    },
a1a3bc73   Luigi Serra   graphs updates
1285
1286
1287
1288
1289
1290
  
    getWS: function () {
      var cc = this._source.charCodeAt(this._index);
  
      while (cc === 32 || cc === 10 || cc === 9 || cc === 13) {
        cc = this._source.charCodeAt(++this._index);
0e9aeacd   root   localization l20n
1291
      }
0e9aeacd   root   localization l20n
1292
    },
a1a3bc73   Luigi Serra   graphs updates
1293
1294
1295
1296
1297
1298
1299
  
    getRequiredWS: function () {
      var pos = this._index;
      var cc = this._source.charCodeAt(pos);
  
      while (cc === 32 || cc === 10 || cc === 9 || cc === 13) {
        cc = this._source.charCodeAt(++this._index);
0e9aeacd   root   localization l20n
1300
      }
a1a3bc73   Luigi Serra   graphs updates
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
      return this._index !== pos;
    },
  
    getIdentifier: function () {
      var start = this._index;
      var cc = this._source.charCodeAt(this._index);
  
      if (cc >= 97 && cc <= 122 || cc >= 65 && cc <= 90 || cc === 95) {
        cc = this._source.charCodeAt(++this._index);
      } else {
        throw this.error('Identifier has to start with [a-zA-Z_]');
0e9aeacd   root   localization l20n
1312
      }
a1a3bc73   Luigi Serra   graphs updates
1313
1314
1315
  
      while (cc >= 97 && cc <= 122 || cc >= 65 && cc <= 90 || cc >= 48 && cc <= 57 || cc === 95) {
        cc = this._source.charCodeAt(++this._index);
0e9aeacd   root   localization l20n
1316
      }
a1a3bc73   Luigi Serra   graphs updates
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
  
      return this._source.slice(start, this._index);
    },
  
    getUnicodeChar: function () {
      for (var i = 0; i < 4; i++) {
        var cc = this._source.charCodeAt(++this._index);
        if (cc > 96 && cc < 103 || cc > 64 && cc < 71 || cc > 47 && cc < 58) {
          continue;
        }
        throw this.error('Illegal unicode escape sequence');
0e9aeacd   root   localization l20n
1328
      }
a1a3bc73   Luigi Serra   graphs updates
1329
1330
      this._index++;
      return String.fromCharCode(parseInt(this._source.slice(this._index - 4, this._index), 16));
0e9aeacd   root   localization l20n
1331
    },
a1a3bc73   Luigi Serra   graphs updates
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
  
    stringRe: /"|'|{{|\\/g,
    getString: function (opchar, opcharLen) {
      var body = [];
      var placeables = 0;
  
      this._index += opcharLen;
      var start = this._index;
  
      var bufStart = start;
      var buf = '';
  
      while (true) {
        this.stringRe.lastIndex = this._index;
        var 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;
          var 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
1395
      }
a1a3bc73   Luigi Serra   graphs updates
1396
1397
1398
  
      if (body.length === 0) {
        return buf + this._source.slice(bufStart, this._index - opcharLen);
0e9aeacd   root   localization l20n
1399
      }
a1a3bc73   Luigi Serra   graphs updates
1400
1401
1402
  
      if (this._index - opcharLen > bufStart || buf.length > 0) {
        body.push(buf + this._source.slice(bufStart, this._index - opcharLen));
0e9aeacd   root   localization l20n
1403
      }
a1a3bc73   Luigi Serra   graphs updates
1404
1405
  
      return body;
0e9aeacd   root   localization l20n
1406
    },
a1a3bc73   Luigi Serra   graphs updates
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
  
    getAttributes: function () {
      var attrs = Object.create(null);
  
      while (true) {
        this.getAttribute(attrs);
        var ws1 = this.getRequiredWS();
        var ch = this._source.charAt(this._index);
        if (ch === '>') {
          break;
        } else if (!ws1) {
          throw this.error('Expected ">"');
        }
0e9aeacd   root   localization l20n
1420
      }
a1a3bc73   Luigi Serra   graphs updates
1421
      return attrs;
0e9aeacd   root   localization l20n
1422
    },
a1a3bc73   Luigi Serra   graphs updates
1423
1424
1425
1426
1427
1428
1429
1430
1431
  
    getAttribute: function (attrs) {
      var key = this.getIdentifier();
      var index = undefined;
  
      if (this._source[this._index] === '[') {
        ++this._index;
        this.getWS();
        index = this.getItemList(this.getExpression, ']');
0e9aeacd   root   localization l20n
1432
      }
a1a3bc73   Luigi Serra   graphs updates
1433
1434
1435
      this.getWS();
      if (this._source[this._index] !== ':') {
        throw this.error('Expected ":"');
0e9aeacd   root   localization l20n
1436
      }
a1a3bc73   Luigi Serra   graphs updates
1437
1438
1439
1440
      ++this._index;
      this.getWS();
      var hasIndex = index !== undefined;
      var value = this.getValue(undefined, hasIndex);
0e9aeacd   root   localization l20n
1441
  
a1a3bc73   Luigi Serra   graphs updates
1442
1443
1444
      if (key in attrs) {
        throw this.error('Duplicate attribute "' + key, 'duplicateerror');
      }
0e9aeacd   root   localization l20n
1445
  
a1a3bc73   Luigi Serra   graphs updates
1446
1447
1448
1449
1450
1451
1452
1453
1454
      if (!index && typeof value === 'string') {
        attrs[key] = value;
      } else {
        attrs[key] = {
          value: value,
          index: index
        };
      }
    },
0e9aeacd   root   localization l20n
1455
  
a1a3bc73   Luigi Serra   graphs updates
1456
1457
    getHash: function (index) {
      var items = Object.create(null);
0e9aeacd   root   localization l20n
1458
  
a1a3bc73   Luigi Serra   graphs updates
1459
1460
      ++this._index;
      this.getWS();
0e9aeacd   root   localization l20n
1461
  
a1a3bc73   Luigi Serra   graphs updates
1462
      var defKey = undefined;
0e9aeacd   root   localization l20n
1463
  
a1a3bc73   Luigi Serra   graphs updates
1464
1465
      while (true) {
        var _getHashItem = this.getHashItem();
0e9aeacd   root   localization l20n
1466
  
a1a3bc73   Luigi Serra   graphs updates
1467
1468
1469
        var key = _getHashItem[0];
        var value = _getHashItem[1];
        var def = _getHashItem[2];
0e9aeacd   root   localization l20n
1470
  
a1a3bc73   Luigi Serra   graphs updates
1471
        items[key] = value;
0e9aeacd   root   localization l20n
1472
  
a1a3bc73   Luigi Serra   graphs updates
1473
1474
1475
1476
1477
1478
1479
        if (def) {
          if (defKey) {
            throw this.error('Default item redefinition forbidden');
          }
          defKey = key;
        }
        this.getWS();
0e9aeacd   root   localization l20n
1480
  
a1a3bc73   Luigi Serra   graphs updates
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
        var 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
1492
1493
1494
        }
      }
  
a1a3bc73   Luigi Serra   graphs updates
1495
1496
1497
1498
1499
      if (defKey) {
        items.__default = defKey;
      } else if (!index) {
        throw this.error('Unresolvable Hash Value');
      }
0e9aeacd   root   localization l20n
1500
  
a1a3bc73   Luigi Serra   graphs updates
1501
1502
      return items;
    },
0e9aeacd   root   localization l20n
1503
  
a1a3bc73   Luigi Serra   graphs updates
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
    getHashItem: function () {
      var defItem = false;
      if (this._source[this._index] === '*') {
        ++this._index;
        defItem = true;
      }
  
      var key = this.getIdentifier();
      this.getWS();
      if (this._source[this._index] !== ':') {
        throw this.error('Expected ":"');
0e9aeacd   root   localization l20n
1515
      }
a1a3bc73   Luigi Serra   graphs updates
1516
1517
      ++this._index;
      this.getWS();
0e9aeacd   root   localization l20n
1518
  
a1a3bc73   Luigi Serra   graphs updates
1519
1520
      return [key, this.getValue(), defItem];
    },
0e9aeacd   root   localization l20n
1521
  
a1a3bc73   Luigi Serra   graphs updates
1522
1523
1524
1525
    getComment: function () {
      this._index += 2;
      var start = this._index;
      var end = this._source.indexOf('*/', start);
0e9aeacd   root   localization l20n
1526
  
a1a3bc73   Luigi Serra   graphs updates
1527
1528
      if (end === -1) {
        throw this.error('Comment without a closing tag');
0e9aeacd   root   localization l20n
1529
1530
      }
  
a1a3bc73   Luigi Serra   graphs updates
1531
1532
      this._index = end + 2;
    },
0e9aeacd   root   localization l20n
1533
  
a1a3bc73   Luigi Serra   graphs updates
1534
1535
    getExpression: function () {
      var exp = this.getPrimaryExpression();
0e9aeacd   root   localization l20n
1536
  
a1a3bc73   Luigi Serra   graphs updates
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
      while (true) {
        var 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
1549
  
a1a3bc73   Luigi Serra   graphs updates
1550
1551
      return exp;
    },
0e9aeacd   root   localization l20n
1552
  
a1a3bc73   Luigi Serra   graphs updates
1553
1554
    getPropertyExpression: function (idref, computed) {
      var exp = undefined;
0e9aeacd   root   localization l20n
1555
  
a1a3bc73   Luigi Serra   graphs updates
1556
1557
1558
1559
1560
1561
      if (computed) {
        this.getWS();
        exp = this.getExpression();
        this.getWS();
        if (this._source[this._index] !== ']') {
          throw this.error('Expected "]"');
0e9aeacd   root   localization l20n
1562
        }
a1a3bc73   Luigi Serra   graphs updates
1563
1564
1565
1566
        ++this._index;
      } else {
        exp = this.getIdentifier();
      }
0e9aeacd   root   localization l20n
1567
  
a1a3bc73   Luigi Serra   graphs updates
1568
1569
1570
1571
1572
1573
1574
      return {
        type: 'prop',
        expr: idref,
        prop: exp,
        cmpt: computed
      };
    },
0e9aeacd   root   localization l20n
1575
  
a1a3bc73   Luigi Serra   graphs updates
1576
1577
    getCallExpression: function (callee) {
      this.getWS();
0e9aeacd   root   localization l20n
1578
  
a1a3bc73   Luigi Serra   graphs updates
1579
1580
1581
1582
1583
1584
      return {
        type: 'call',
        expr: callee,
        args: this.getItemList(this.getExpression, ')')
      };
    },
0e9aeacd   root   localization l20n
1585
  
a1a3bc73   Luigi Serra   graphs updates
1586
1587
    getPrimaryExpression: function () {
      var ch = this._source[this._index];
0e9aeacd   root   localization l20n
1588
  
a1a3bc73   Luigi Serra   graphs updates
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
      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
1609
  
a1a3bc73   Luigi Serra   graphs updates
1610
1611
1612
    getItemList: function (callback, closeChar) {
      var items = [];
      var closed = false;
0e9aeacd   root   localization l20n
1613
  
a1a3bc73   Luigi Serra   graphs updates
1614
      this.getWS();
0e9aeacd   root   localization l20n
1615
  
a1a3bc73   Luigi Serra   graphs updates
1616
1617
1618
1619
      if (this._source[this._index] === closeChar) {
        ++this._index;
        closed = true;
      }
0e9aeacd   root   localization l20n
1620
  
a1a3bc73   Luigi Serra   graphs updates
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
      while (!closed) {
        items.push(callback.call(this));
        this.getWS();
        var 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
1636
1637
        }
      }
0e9aeacd   root   localization l20n
1638
  
a1a3bc73   Luigi Serra   graphs updates
1639
1640
      return items;
    },
0e9aeacd   root   localization l20n
1641
  
a1a3bc73   Luigi Serra   graphs updates
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
    getJunkEntry: function () {
      var pos = this._index;
      var nextEntity = this._source.indexOf('<', pos);
      var nextComment = this._source.indexOf('/*', pos);
  
      if (nextEntity === -1) {
        nextEntity = this._length;
      }
      if (nextComment === -1) {
        nextComment = this._length;
0e9aeacd   root   localization l20n
1652
      }
0e9aeacd   root   localization l20n
1653
  
a1a3bc73   Luigi Serra   graphs updates
1654
      var nextEntry = Math.min(nextEntity, nextComment);
0e9aeacd   root   localization l20n
1655
  
a1a3bc73   Luigi Serra   graphs updates
1656
1657
      this._index = nextEntry;
    },
0e9aeacd   root   localization l20n
1658
  
a1a3bc73   Luigi Serra   graphs updates
1659
1660
    error: function (message) {
      var type = arguments.length <= 1 || arguments[1] === undefined ? 'parsererror' : arguments[1];
0e9aeacd   root   localization l20n
1661
  
a1a3bc73   Luigi Serra   graphs updates
1662
      var pos = this._index;
0e9aeacd   root   localization l20n
1663
  
a1a3bc73   Luigi Serra   graphs updates
1664
1665
1666
1667
      var start = this._source.lastIndexOf('<', pos - 1);
      var lastClose = this._source.lastIndexOf('>', pos - 1);
      start = lastClose > start ? lastClose + 1 : start;
      var context = this._source.slice(start, pos + 10);
0e9aeacd   root   localization l20n
1668
  
a1a3bc73   Luigi Serra   graphs updates
1669
1670
1671
1672
1673
1674
1675
1676
      var msg = message + ' at pos ' + pos + ': `' + context + '`';
      var err = new L10nError(msg);
      if (this.emit) {
        this.emit(type, err);
      }
      return err;
    }
  };
0e9aeacd   root   localization l20n
1677
1678
  
  function emit(listeners) {
a1a3bc73   Luigi Serra   graphs updates
1679
    var _this7 = this;
0e9aeacd   root   localization l20n
1680
  
a1a3bc73   Luigi Serra   graphs updates
1681
1682
    for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
      args[_key3 - 1] = arguments[_key3];
0e9aeacd   root   localization l20n
1683
1684
1685
1686
1687
1688
    }
  
    var type = args.shift();
  
    if (listeners['*']) {
      listeners['*'].slice().forEach(function (listener) {
a1a3bc73   Luigi Serra   graphs updates
1689
        return listener.apply(_this7, args);
0e9aeacd   root   localization l20n
1690
1691
1692
1693
1694
      });
    }
  
    if (listeners[type]) {
      listeners[type].slice().forEach(function (listener) {
a1a3bc73   Luigi Serra   graphs updates
1695
        return listener.apply(_this7, args);
0e9aeacd   root   localization l20n
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
      });
    }
  }
  
  function addEventListener(listeners, type, listener) {
    if (!(type in listeners)) {
      listeners[type] = [];
    }
    listeners[type].push(listener);
  }
  
  function removeEventListener(listeners, type, listener) {
    var typeListeners = listeners[type];
    var pos = typeListeners.indexOf(listener);
    if (pos === -1) {
      return;
    }
  
    typeListeners.splice(pos, 1);
  }
  
0e9aeacd   root   localization l20n
1717
  var Env = (function () {
a1a3bc73   Luigi Serra   graphs updates
1718
    function Env(fetchResource) {
0e9aeacd   root   localization l20n
1719
1720
      _classCallCheck(this, Env);
  
0e9aeacd   root   localization l20n
1721
1722
      this.fetchResource = fetchResource;
  
a1a3bc73   Luigi Serra   graphs updates
1723
1724
1725
1726
1727
1728
1729
      this.resCache = new Map();
      this.resRefs = new Map();
      this.numberFormatters = null;
      this.parsers = {
        properties: PropertiesParser,
        l20n: L20nParser
      };
0e9aeacd   root   localization l20n
1730
1731
1732
1733
1734
1735
1736
  
      var listeners = {};
      this.emit = emit.bind(this, listeners);
      this.addEventListener = addEventListener.bind(this, listeners);
      this.removeEventListener = removeEventListener.bind(this, listeners);
    }
  
a1a3bc73   Luigi Serra   graphs updates
1737
1738
1739
1740
1741
1742
1743
1744
1745
    Env.prototype.createContext = function createContext(langs, resIds) {
      var _this8 = this;
  
      var ctx = new Context(this, langs, resIds);
      resIds.forEach(function (resId) {
        var usedBy = _this8.resRefs.get(resId) || 0;
        _this8.resRefs.set(resId, usedBy + 1);
      });
  
0e9aeacd   root   localization l20n
1746
1747
1748
1749
      return ctx;
    };
  
    Env.prototype.destroyContext = function destroyContext(ctx) {
a1a3bc73   Luigi Serra   graphs updates
1750
      var _this9 = this;
0e9aeacd   root   localization l20n
1751
  
a1a3bc73   Luigi Serra   graphs updates
1752
1753
1754
1755
1756
1757
      ctx.resIds.forEach(function (resId) {
        var usedBy = _this9.resRefs.get(resId) || 0;
  
        if (usedBy > 1) {
          return _this9.resRefs.set(resId, usedBy - 1);
        }
0e9aeacd   root   localization l20n
1758
  
a1a3bc73   Luigi Serra   graphs updates
1759
1760
1761
1762
        _this9.resRefs.delete(resId);
        _this9.resCache.forEach(function (val, key) {
          return key.startsWith(resId) ? _this9.resCache.delete(key) : null;
        });
0e9aeacd   root   localization l20n
1763
1764
1765
1766
      });
    };
  
    Env.prototype._parse = function _parse(syntax, lang, data) {
a1a3bc73   Luigi Serra   graphs updates
1767
      var _this10 = this;
0e9aeacd   root   localization l20n
1768
  
a1a3bc73   Luigi Serra   graphs updates
1769
      var parser = this.parsers[syntax];
0e9aeacd   root   localization l20n
1770
1771
1772
1773
1774
      if (!parser) {
        return data;
      }
  
      var emit = function (type, err) {
a1a3bc73   Luigi Serra   graphs updates
1775
        return _this10.emit(type, amendError$1(lang, err));
0e9aeacd   root   localization l20n
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
      };
      return parser.parse.call(parser, emit, data);
    };
  
    Env.prototype._create = function _create(lang, entries) {
      if (lang.src !== 'pseudo') {
        return entries;
      }
  
      var pseudoentries = Object.create(null);
      for (var key in entries) {
        pseudoentries[key] = walkEntry(entries[key], pseudo$1[lang.code].process);
      }
      return pseudoentries;
    };
  
    Env.prototype._getResource = function _getResource(lang, res) {
a1a3bc73   Luigi Serra   graphs updates
1793
      var _this11 = this;
0e9aeacd   root   localization l20n
1794
  
a1a3bc73   Luigi Serra   graphs updates
1795
      var cache = this.resCache;
0e9aeacd   root   localization l20n
1796
1797
1798
1799
1800
1801
1802
1803
1804
      var id = res + lang.code + lang.src;
  
      if (cache.has(id)) {
        return cache.get(id);
      }
  
      var syntax = res.substr(res.lastIndexOf('.') + 1);
  
      var saveEntries = function (data) {
a1a3bc73   Luigi Serra   graphs updates
1805
1806
        var entries = _this11._parse(syntax, lang, data);
        cache.set(id, _this11._create(lang, entries));
0e9aeacd   root   localization l20n
1807
1808
1809
1810
      };
  
      var recover = function (err) {
        err.lang = lang;
a1a3bc73   Luigi Serra   graphs updates
1811
        _this11.emit('fetcherror', err);
0e9aeacd   root   localization l20n
1812
1813
1814
        cache.set(id, err);
      };
  
a1a3bc73   Luigi Serra   graphs updates
1815
      var langToFetch = lang.src === 'pseudo' ? { code: 'en-US', src: 'app', ver: lang.ver } : lang;
0e9aeacd   root   localization l20n
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
  
      var resource = this.fetchResource(res, langToFetch).then(saveEntries, recover);
  
      cache.set(id, resource);
  
      return resource;
    };
  
    return Env;
  })();
  
0e9aeacd   root   localization l20n
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
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
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
  function amendError$1(lang, err) {
    err.lang = lang;
    return err;
  }
  
  var KNOWN_MACROS = ['plural'];
  var MAX_PLACEABLE_LENGTH = 2500;
  
  var nonLatin1 = /[^\x01-\xFF]/;
  
  var FSI = '⁨';
  var PDI = '⁩';
  
  var resolutionChain = new WeakSet();
  
  function createEntry(node) {
    var keys = Object.keys(node);
  
    if (typeof node.$v === 'string' && keys.length === 2) {
      return node.$v;
    }
  
    var attrs = undefined;
  
    for (var i = 0, key = undefined; key = keys[i]; i++) {
      if (key[0] === '$') {
        continue;
      }
  
      if (!attrs) {
        attrs = Object.create(null);
      }
      attrs[key] = createAttribute(node[key]);
    }
  
    return {
      value: node.$v !== undefined ? node.$v : null,
      index: node.$x || null,
      attrs: attrs || null
    };
  }
  
  function createAttribute(node) {
    if (typeof node === 'string') {
      return node;
    }
  
    return {
      value: node.$v || (node !== undefined ? node : null),
      index: node.$x || null
    };
  }
  
  function format(ctx, lang, args, entity) {
    if (typeof entity === 'string') {
      return [{}, entity];
    }
  
    if (resolutionChain.has(entity)) {
      throw new L10nError('Cyclic reference detected');
    }
  
    resolutionChain.add(entity);
  
    var rv = undefined;
  
    try {
      rv = resolveValue({}, ctx, lang, args, entity.value, entity.index);
    } finally {
      resolutionChain.delete(entity);
    }
    return rv;
  }
  
  function resolveIdentifier(ctx, lang, args, id) {
    if (KNOWN_MACROS.indexOf(id) > -1) {
      return [{}, ctx._getMacro(lang, id)];
    }
  
    if (args && args.hasOwnProperty(id)) {
      if (typeof args[id] === 'string' || typeof args[id] === 'number' && !isNaN(args[id])) {
        return [{}, args[id]];
      } else {
        throw new L10nError('Arg must be a string or a number: ' + id);
      }
    }
  
    if (id === '__proto__') {
      throw new L10nError('Illegal id: ' + id);
    }
  
    var entity = ctx._getEntity(lang, id);
  
    if (entity) {
      return format(ctx, lang, args, entity);
    }
  
    throw new L10nError('Unknown reference: ' + id);
  }
  
  function subPlaceable(locals, ctx, lang, args, id) {
    var res = undefined;
  
    try {
      res = resolveIdentifier(ctx, lang, args, id);
    } catch (err) {
      return [{ error: err }, '{{ ' + id + ' }}'];
    }
  
    var value = res[1];
  
    if (typeof value === 'number') {
      return res;
    }
  
    if (typeof value === 'string') {
      if (value.length >= MAX_PLACEABLE_LENGTH) {
        throw new L10nError('Too many characters in placeable (' + value.length + ', max allowed is ' + MAX_PLACEABLE_LENGTH + ')');
      }
  
      if (locals.contextIsNonLatin1 || value.match(nonLatin1)) {
        res[1] = FSI + value + PDI;
      }
  
      return res;
    }
  
    return [{}, '{{ ' + id + ' }}'];
  }
  
  function interpolate(locals, ctx, lang, args, arr) {
    return arr.reduce(function (_ref4, cur) {
      var localsSeq = _ref4[0];
      var valueSeq = _ref4[1];
  
      if (typeof cur === 'string') {
        return [localsSeq, valueSeq + cur];
      } else if (cur.t === 'idOrVar') {
        var _subPlaceable = subPlaceable(locals, ctx, lang, args, cur.v);
  
        var value = _subPlaceable[1];
  
        return [localsSeq, valueSeq + value];
      }
    }, [locals, '']);
  }
  
  function resolveSelector(ctx, lang, args, expr, index) {
    var selectorName = index[0].v;
    var selector = resolveIdentifier(ctx, lang, args, selectorName)[1];
  
    if (typeof selector !== 'function') {
      return selector;
    }
  
    var argValue = index[1] ? resolveIdentifier(ctx, lang, args, index[1])[1] : undefined;
  
    if (selectorName === 'plural') {
      if (argValue === 0 && 'zero' in expr) {
        return 'zero';
      }
      if (argValue === 1 && 'one' in expr) {
        return 'one';
      }
      if (argValue === 2 && 'two' in expr) {
        return 'two';
      }
    }
  
    return selector(argValue);
  }
  
  function resolveValue(locals, ctx, lang, args, expr, index) {
    if (!expr) {
      return [locals, expr];
    }
  
    if (typeof expr === 'string' || typeof expr === 'boolean' || typeof expr === 'number') {
      return [locals, expr];
    }
  
    if (Array.isArray(expr)) {
      locals.contextIsNonLatin1 = expr.some(function ($_) {
        return typeof $_ === 'string' && $_.match(nonLatin1);
      });
      return interpolate(locals, ctx, lang, args, expr);
    }
  
    if (index) {
      var selector = resolveSelector(ctx, lang, args, expr, index);
      if (expr.hasOwnProperty(selector)) {
        return resolveValue(locals, ctx, lang, args, expr[selector]);
      }
    }
  
    if ('other' in expr) {
      return resolveValue(locals, ctx, lang, args, expr.other);
    }
  
    throw new L10nError('Unresolvable value');
  }
  
a1a3bc73   Luigi Serra   graphs updates
2029
2030
  function LegacyContext(env, langs, resIds) {
    Context.call(this, env, langs, resIds);
0e9aeacd   root   localization l20n
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
  }
  
  LegacyContext.prototype = Object.create(Context.prototype);
  
  LegacyContext.prototype._formatTuple = function (lang, args, entity, id, key) {
    try {
      return format(this, lang, args, entity);
    } catch (err) {
      err.id = key ? id + '::' + key : id;
      err.lang = lang;
a1a3bc73   Luigi Serra   graphs updates
2041
      this.env.emit('resolveerror', err, this);
0e9aeacd   root   localization l20n
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
      return [{ error: err }, err.id];
    }
  };
  
  var MAX_PLACEABLES$2 = 100;
  
  var PropertiesParser$1 = {
    patterns: null,
    entryIds: 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*\}\}/
      };
    },
  
    parse: function (emit, source) {
      if (!this.patterns) {
        this.init();
      }
  
      var ast = [];
      this.entryIds = Object.create(null);
  
      var entries = source.match(this.patterns.entries);
      if (!entries) {
        return ast;
      }
      for (var i = 0; i < entries.length; i++) {
        var line = entries[i];
  
        if (this.patterns.comment.test(line)) {
          continue;
        }
  
        while (this.patterns.multiline.test(line) && i < entries.length) {
          line = line.slice(0, -1) + entries[++i].trim();
        }
  
        var entityMatch = line.match(this.patterns.entity);
        if (entityMatch) {
          try {
            this.parseEntity(entityMatch[1], entityMatch[2], ast);
          } catch (e) {
            if (emit) {
              emit('parseerror', e);
            } else {
              throw e;
            }
          }
        }
      }
      return ast;
    },
  
    parseEntity: function (id, value, ast) {
      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;
      }
  
      var nameElements = name.split('.');
  
      if (nameElements.length > 2) {
        throw new L10nError('Error in ID: "' + name + '".' + ' Nested attributes are not supported.');
      }
  
      var attr;
      if (nameElements.length > 1) {
        name = nameElements[0];
        attr = nameElements[1];
  
        if (attr[0] === '$') {
          throw new L10nError('Attribute can\'t start with "$"', id);
        }
      } else {
        attr = null;
      }
  
      this.setEntityValue(name, attr, key, this.unescapeString(value), ast);
    },
  
    setEntityValue: function (id, attr, key, rawValue, ast) {
      var pos, v;
  
      var value = rawValue.indexOf('{{') > -1 ? this.parseString(rawValue) : rawValue;
  
      if (attr) {
        pos = this.entryIds[id];
        if (pos === undefined) {
          v = { $i: id };
          if (key) {
            v[attr] = { $v: {} };
            v[attr].$v[key] = value;
          } else {
            v[attr] = value;
          }
          ast.push(v);
          this.entryIds[id] = ast.length - 1;
          return;
        }
        if (key) {
          if (typeof ast[pos][attr] === 'string') {
            ast[pos][attr] = {
              $x: this.parseIndex(ast[pos][attr]),
              $v: {}
            };
          }
          ast[pos][attr].$v[key] = value;
          return;
        }
        ast[pos][attr] = value;
        return;
      }
  
      if (key) {
        pos = this.entryIds[id];
        if (pos === undefined) {
          v = {};
          v[key] = value;
          ast.push({ $i: id, $v: v });
          this.entryIds[id] = ast.length - 1;
          return;
        }
        if (typeof ast[pos].$v === 'string') {
          ast[pos].$x = this.parseIndex(ast[pos].$v);
          ast[pos].$v = {};
        }
        ast[pos].$v[key] = value;
        return;
      }
  
      ast.push({ $i: id, $v: value });
      this.entryIds[id] = ast.length - 1;
    },
  
    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$2) {
        throw new L10nError('Too many placeables (' + placeablesCount + ', max allowed is ' + MAX_PLACEABLES$2 + ')');
      }
  
      for (var i = 0; i < chunks.length; i++) {
        if (chunks[i].length === 0) {
          continue;
        }
        if (i % 2 === 1) {
          complexStr.push({ t: 'idOrVar', v: chunks[i] });
        } else {
          complexStr.push(chunks[i]);
        }
      }
      return complexStr;
    },
  
    unescapeString: function (str) {
      if (str.lastIndexOf('\\') !== -1) {
        str = str.replace(this.patterns.controlChars, '$1');
      }
      return str.replace(this.patterns.unicode, function (match, token) {
        return String.fromCodePoint(parseInt(token, 16));
      });
    },
  
    parseIndex: function (str) {
      var match = str.match(this.patterns.index);
      if (!match) {
        throw new L10nError('Malformed index');
      }
      if (match[2]) {
        return [{ t: 'idOrVar', v: match[1] }, match[2]];
      } else {
        return [{ t: 'idOrVar', v: match[1] }];
      }
    }
  };
  
  function walkContent(node, fn) {
    if (typeof node === 'string') {
      return fn(node);
    }
  
    if (node.t === 'idOrVar') {
      return node;
    }
  
    var rv = Array.isArray(node) ? [] : {};
    var keys = Object.keys(node);
  
    for (var i = 0, key = undefined; key = keys[i]; i++) {
      if (key === '$i' || key === '$x') {
        rv[key] = node[key];
      } else {
        rv[key] = walkContent(node[key], fn);
      }
    }
    return rv;
  }
  
a1a3bc73   Luigi Serra   graphs updates
2259
2260
2261
2262
2263
  function LegacyEnv(fetchResource) {
    Env.call(this, fetchResource);
    this.parsers = {
      properties: PropertiesParser$1
    };
0e9aeacd   root   localization l20n
2264
2265
2266
2267
  }
  
  LegacyEnv.prototype = Object.create(Env.prototype);
  
a1a3bc73   Luigi Serra   graphs updates
2268
2269
  LegacyEnv.prototype.createContext = function (langs, resIds) {
    return new LegacyContext(this, langs, resIds);
0e9aeacd   root   localization l20n
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
  };
  
  LegacyEnv.prototype._create = function (lang, ast) {
    var entries = Object.create(null);
    var create = lang.src === 'pseudo' ? createPseudoEntry : createEntry;
  
    for (var i = 0, node = undefined; node = ast[i]; i++) {
      var id = node.$i;
      if (id in entries) {
        this.emit('duplicateerror', new L10nError('Duplicate string "' + id + '" found in ' + lang.code, id, lang));
      }
      entries[id] = create(node, lang);
    }
  
    return entries;
  };
  
  function createPseudoEntry(node, lang) {
    return createEntry(walkContent(node, pseudo$1[lang.code].process));
  }
  
a1a3bc73   Luigi Serra   graphs updates
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
  var observerConfig = {
    attributes: true,
    characterData: false,
    childList: true,
    subtree: true,
    attributeFilter: ['data-l10n-id', 'data-l10n-args']
  };
  
  var observers = new WeakMap();
  
  function disconnect(view, root, allRoots) {
    var obs = observers.get(view);
    if (obs) {
      obs.observer.disconnect();
      if (allRoots) {
        return;
      }
      obs.roots.delete(root);
      obs.roots.forEach(function (other) {
        return obs.observer.observe(other, observerConfig);
      });
    }
  }
  
  function reconnect(view) {
    var obs = observers.get(view);
    if (obs) {
      obs.roots.forEach(function (root) {
        return obs.observer.observe(root, observerConfig);
      });
    }
  }
  
0e9aeacd   root   localization l20n
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
  var reOverlay = /<|&#?\w+;/;
  
  var allowed = {
    elements: ['a', 'em', 'strong', 'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u', 'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr'],
    attributes: {
      global: ['title', 'aria-label', 'aria-valuetext', 'aria-moz-hint'],
      a: ['download'],
      area: ['download', 'alt'],
  
      input: ['alt', 'placeholder'],
      menuitem: ['label'],
      menu: ['label'],
      optgroup: ['label'],
      option: ['label'],
      track: ['label'],
      img: ['alt'],
      textarea: ['placeholder'],
      th: ['abbr']
    }
  };
  
  function overlayElement(element, translation) {
    var value = translation.value;
  
    if (typeof value === 'string') {
      if (!reOverlay.test(value)) {
        element.textContent = value;
      } else {
        var tmpl = element.ownerDocument.createElement('template');
        tmpl.innerHTML = value;
  
        overlay(element, tmpl.content);
      }
    }
  
    for (var key in translation.attrs) {
      var attrName = camelCaseToDashed(key);
      if (isAttrAllowed({ name: attrName }, element)) {
        element.setAttribute(attrName, translation.attrs[key]);
      }
    }
  }
  
  function overlay(sourceElement, translationElement) {
    var result = translationElement.ownerDocument.createDocumentFragment();
    var k = undefined,
        attr = undefined;
  
    var childElement = undefined;
    while (childElement = translationElement.childNodes[0]) {
      translationElement.removeChild(childElement);
  
      if (childElement.nodeType === childElement.TEXT_NODE) {
        result.appendChild(childElement);
        continue;
      }
  
      var index = getIndexOfType(childElement);
      var sourceChild = getNthElementOfType(sourceElement, childElement, index);
      if (sourceChild) {
        overlay(sourceChild, childElement);
        result.appendChild(sourceChild);
        continue;
      }
  
      if (isElementAllowed(childElement)) {
        var sanitizedChild = childElement.ownerDocument.createElement(childElement.nodeName);
        overlay(sanitizedChild, childElement);
        result.appendChild(sanitizedChild);
        continue;
      }
  
      result.appendChild(translationElement.ownerDocument.createTextNode(childElement.textContent));
    }
  
    sourceElement.textContent = '';
    sourceElement.appendChild(result);
  
    if (translationElement.attributes) {
      for (k = 0, attr; attr = translationElement.attributes[k]; k++) {
        if (isAttrAllowed(attr, sourceElement)) {
          sourceElement.setAttribute(attr.name, attr.value);
        }
      }
    }
  }
  
  function isElementAllowed(element) {
    return allowed.elements.indexOf(element.tagName.toLowerCase()) !== -1;
  }
  
  function isAttrAllowed(attr, element) {
    var attrName = attr.name.toLowerCase();
    var tagName = element.tagName.toLowerCase();
  
    if (allowed.attributes.global.indexOf(attrName) !== -1) {
      return true;
    }
  
    if (!allowed.attributes[tagName]) {
      return false;
    }
  
    if (allowed.attributes[tagName].indexOf(attrName) !== -1) {
      return true;
    }
  
    if (tagName === 'input' && attrName === 'value') {
      var type = element.type.toLowerCase();
      if (type === 'submit' || type === 'button' || type === 'reset') {
        return true;
      }
    }
    return false;
  }
  
  function getNthElementOfType(context, element, index) {
    var nthOfType = 0;
    for (var i = 0, child = undefined; child = context.children[i]; i++) {
      if (child.nodeType === child.ELEMENT_NODE && child.tagName === element.tagName) {
        if (nthOfType === index) {
          return child;
        }
        nthOfType++;
      }
    }
    return null;
  }
  
  function getIndexOfType(element) {
    var index = 0;
    var child = undefined;
    while (child = element.previousElementSibling) {
      if (child.tagName === element.tagName) {
        index++;
      }
    }
    return index;
  }
  
  function camelCaseToDashed(string) {
    if (string === 'ariaValueText') {
      return 'aria-valuetext';
    }
  
    return string.replace(/[A-Z]/g, function (match) {
      return '-' + match.toLowerCase();
    }).replace(/^-/, '');
  }
  
  var reHtml = /[&<>]/g;
  var htmlEntities = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;'
  };
  
0e9aeacd   root   localization l20n
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
  function getTranslatables(element) {
    var nodes = Array.from(element.querySelectorAll('[data-l10n-id]'));
  
    if (typeof element.hasAttribute === 'function' && element.hasAttribute('data-l10n-id')) {
      nodes.push(element);
    }
  
    return nodes;
  }
  
a1a3bc73   Luigi Serra   graphs updates
2491
2492
  function translateFragment(view, frag) {
    return translateElements(view, getTranslatables(frag));
0e9aeacd   root   localization l20n
2493
2494
  }
  
a1a3bc73   Luigi Serra   graphs updates
2495
  function getElementsTranslation(view, elems) {
0e9aeacd   root   localization l20n
2496
2497
2498
2499
2500
2501
2502
2503
    var keys = elems.map(function (elem) {
      var id = elem.getAttribute('data-l10n-id');
      var args = elem.getAttribute('data-l10n-args');
      return args ? [id, JSON.parse(args.replace(reHtml, function (match) {
        return htmlEntities[match];
      }))] : id;
    });
  
a1a3bc73   Luigi Serra   graphs updates
2504
    return view.formatEntities.apply(view, keys);
0e9aeacd   root   localization l20n
2505
2506
  }
  
a1a3bc73   Luigi Serra   graphs updates
2507
2508
  function translateElements(view, elements) {
    return getElementsTranslation(view, elements).then(function (translations) {
0e9aeacd   root   localization l20n
2509
2510
2511
2512
2513
      return applyTranslations(view, elements, translations);
    });
  }
  
  function applyTranslations(view, elems, translations) {
a1a3bc73   Luigi Serra   graphs updates
2514
    disconnect(view, null, true);
0e9aeacd   root   localization l20n
2515
2516
2517
    for (var i = 0; i < elems.length; i++) {
      overlayElement(elems[i], translations[i]);
    }
a1a3bc73   Luigi Serra   graphs updates
2518
    reconnect(view);
0e9aeacd   root   localization l20n
2519
2520
  }
  
a1a3bc73   Luigi Serra   graphs updates
2521
2522
2523
2524
  function getResourceLinks(head) {
    return Array.prototype.map.call(head.querySelectorAll('link[rel="localization"]'), function (el) {
      return el.getAttribute('href');
    });
0e9aeacd   root   localization l20n
2525
2526
  }
  
a1a3bc73   Luigi Serra   graphs updates
2527
2528
2529
2530
  function serializeContext(ctx) {
    var lang = ctx.langs[0];
    var cache = ctx.env.resCache;
    return ctx.resIds.reduceRight(function (_ref5, cur) {
0e9aeacd   root   localization l20n
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
      var errorsSeq = _ref5[0];
      var entriesSeq = _ref5[1];
  
      var sourceRes = cache.get(cur + 'en-USapp');
      var langRes = cache.get(cur + lang.code + lang.src);
  
      var _serializeEntries = serializeEntries(lang, langRes instanceof L10nError ? {} : langRes, sourceRes instanceof L10nError ? {} : sourceRes);
  
      var errors = _serializeEntries[0];
      var entries = _serializeEntries[1];
  
      return [errorsSeq.concat(errors), Object.assign(entriesSeq, entries)];
    }, [[], Object.create(null)]);
  }
  
  function serializeEntries(lang, langEntries, sourceEntries) {
    var errors = [];
    var entries = Object.create(null);
  
    for (var id in sourceEntries) {
      var sourceEntry = sourceEntries[id];
      var langEntry = langEntries[id];
  
      if (!langEntry) {
        errors.push(new L10nError('"' + id + '"' + ' not found in ' + lang.code, id, lang));
        entries[id] = sourceEntry;
        continue;
      }
  
      if (!areEntityStructsEqual(sourceEntry, langEntry)) {
        errors.push(new L10nError('"' + id + '"' + ' is malformed in ' + lang.code, id, lang));
        entries[id] = sourceEntry;
        continue;
      }
  
      entries[id] = langEntry;
    }
  
    return [errors, entries];
  }
  
  function resolvesToString(entity) {
    return typeof entity === 'string' || typeof entity.value === 'string' || Array.isArray(entity.value) || typeof entity.value === 'object' && entity.index !== null;
  }
  
  function areAttrsEqual(attrs1, attrs2) {
    var keys1 = Object.keys(attrs1 || Object.create(null));
    var keys2 = Object.keys(attrs2 || Object.create(null));
  
    if (keys1.length !== keys2.length) {
      return false;
    }
  
    for (var i = 0; i < keys1.length; i++) {
      if (keys2.indexOf(keys1[i]) === -1) {
        return false;
      }
    }
  
    return true;
  }
  
  function areEntityStructsEqual(source, translation) {
    if (resolvesToString(source) && !resolvesToString(translation)) {
      return false;
    }
  
    if (source.attrs || translation.attrs) {
      return areAttrsEqual(source.attrs, translation.attrs);
    }
  
    return true;
  }
  
a1a3bc73   Luigi Serra   graphs updates
2605
2606
2607
2608
  function serializeLegacyContext(ctx) {
    var lang = ctx.langs[0];
    var cache = ctx.env.resCache;
    return ctx.resIds.reduce(function (_ref6, cur) {
0e9aeacd   root   localization l20n
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
      var errorsSeq = _ref6[0];
      var entriesSeq = _ref6[1];
  
      var sourceRes = cache.get(cur + 'en-USapp');
      var langRes = cache.get(cur + lang.code + lang.src);
  
      var _serializeEntries$1 = serializeEntries$1(lang, langRes instanceof L10nError ? {} : langRes, sourceRes instanceof L10nError ? {} : sourceRes);
  
      var errors = _serializeEntries$1[0];
      var entries = _serializeEntries$1[1];
  
      return [errorsSeq.concat(errors), entriesSeq.concat(entries)];
    }, [[], []]);
  }
  
  function serializeEntries$1(lang, langEntries, sourceEntries) {
    var errors = [];
    var entries = Object.keys(sourceEntries).map(function (id) {
      var sourceEntry = sourceEntries[id];
      var langEntry = langEntries[id];
  
      if (!langEntry) {
        errors.push(new L10nError('"' + id + '"' + ' not found in ' + lang.code, id, lang));
        return serializeEntry(sourceEntry, id);
      }
  
      if (!areEntityStructsEqual$1(sourceEntry, langEntry)) {
        errors.push(new L10nError('"' + id + '"' + ' is malformed in ' + lang.code, id, lang));
        return serializeEntry(sourceEntry, id);
      }
  
      return serializeEntry(langEntry, id);
    });
  
    return [errors, entries];
  }
  
  function serializeEntry(entry, id) {
    if (typeof entry === 'string') {
      return { $i: id, $v: entry };
    }
  
    var node = {
      $i: id
    };
  
    if (entry.value !== null) {
      node.$v = entry.value;
    }
  
    if (entry.index !== null) {
      node.$x = entry.index;
    }
  
    for (var key in entry.attrs) {
      node[key] = serializeAttribute(entry.attrs[key]);
    }
  
    return node;
  }
  
  function serializeAttribute(attr) {
    if (typeof attr === 'string') {
      return attr;
    }
  
    var node = {};
  
    if (attr.value !== null) {
      node.$v = attr.value;
    }
  
    if (attr.index !== null) {
      node.$x = attr.index;
    }
  
    return node;
  }
  
  function resolvesToString$1(entity) {
    return typeof entity === 'string' || typeof entity.value === 'string' || Array.isArray(entity.value) || typeof entity.value === 'object' && entity.index !== null;
  }
  
  function areAttrsEqual$1(attrs1, attrs2) {
    var keys1 = Object.keys(attrs1 || Object.create(null));
    var keys2 = Object.keys(attrs2 || Object.create(null));
  
    if (keys1.length !== keys2.length) {
      return false;
    }
  
    for (var i = 0; i < keys1.length; i++) {
      if (keys2.indexOf(keys1[i]) === -1) {
        return false;
      }
    }
  
    return true;
  }
  
  function areEntityStructsEqual$1(source, translation) {
    if (resolvesToString$1(source) && !resolvesToString$1(translation)) {
      return false;
    }
  
    if (source.attrs || translation.attrs) {
      return areAttrsEqual$1(source.attrs, translation.attrs);
    }
  
    return true;
  }
  
  var View = (function () {
    function View(htmloptimizer, fetchResource) {
      _classCallCheck(this, View);
  
      this.htmloptimizer = htmloptimizer;
      this.doc = htmloptimizer.document;
a1a3bc73   Luigi Serra   graphs updates
2727
      this.resLinks = getResourceLinks(this.doc.head);
0e9aeacd   root   localization l20n
2728
2729
2730
2731
2732
2733
  
      this.isEnabled = this.doc.querySelector('link[rel="localization"]');
  
      this.isLegacy = !this.doc.querySelector('script[src*="l20n"]');
  
      var EnvClass = this.isLegacy ? LegacyEnv : Env;
a1a3bc73   Luigi Serra   graphs updates
2734
2735
      this.env = new EnvClass(fetchResource);
      this.sourceCtx = this.env.createContext([{ code: 'en-US', src: 'app' }], this.resLinks);
0e9aeacd   root   localization l20n
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
  
      this.env.addEventListener('*', amendError.bind(this));
  
      this.stopBuildError = null;
      var log = logError.bind(this);
      var stop = stopBuild.bind(this);
  
      this.env.addEventListener('parseerror', stop);
      this.env.addEventListener('duplicateerror', stop);
      this.env.addEventListener('notfounderror', stop);
  
      this.env.addEventListener('deprecatewarning', log);
  
      if (htmloptimizer.config.LOCALE_BASEDIR !== '') {
        this.env.addEventListener('fetcherror', log);
        this.env.addEventListener('parseerror', log);
        this.env.addEventListener('duplicateerror', log);
      }
    }
  
a1a3bc73   Luigi Serra   graphs updates
2756
2757
    View.prototype.formatEntities = function formatEntities() {
      var _ctx;
0e9aeacd   root   localization l20n
2758
  
a1a3bc73   Luigi Serra   graphs updates
2759
      return (_ctx = this.ctx).formatEntities.apply(_ctx, arguments);
0e9aeacd   root   localization l20n
2760
2761
2762
    };
  
    View.prototype.translateDocument = function translateDocument(code) {
a1a3bc73   Luigi Serra   graphs updates
2763
      var _this12 = this;
0e9aeacd   root   localization l20n
2764
2765
  
      var dir = getDirection(code);
a1a3bc73   Luigi Serra   graphs updates
2766
      var langs = [{ code: code, src: 'app' }, { code: 'en-US', src: 'app' }];
0e9aeacd   root   localization l20n
2767
      var setDocLang = function () {
a1a3bc73   Luigi Serra   graphs updates
2768
2769
        _this12.doc.documentElement.lang = code;
        _this12.doc.documentElement.dir = dir;
0e9aeacd   root   localization l20n
2770
      };
a1a3bc73   Luigi Serra   graphs updates
2771
2772
2773
  
      var ctx = this.env.createContext(langs, getResourceLinks(this.doc.head));
      return translateFragment(ctx, this.doc.documentElement).then(setDocLang);
0e9aeacd   root   localization l20n
2774
2775
2776
    };
  
    View.prototype.serializeResources = function serializeResources(code) {
a1a3bc73   Luigi Serra   graphs updates
2777
      var _this13 = this;
0e9aeacd   root   localization l20n
2778
  
a1a3bc73   Luigi Serra   graphs updates
2779
2780
2781
2782
2783
2784
      var langCtx = this.env.createContext([{ code: code, src: code in pseudo$1 ? 'pseudo' : 'app' }], this.resLinks);
  
      return Promise.all([this.sourceCtx, langCtx].map(function (ctx) {
        return ctx.fetch();
      })).then(function () {
        var _ref7 = _this13.isLegacy ? serializeLegacyContext(langCtx) : serializeContext(langCtx);
0e9aeacd   root   localization l20n
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
  
        var errors = _ref7[0];
        var entries = _ref7[1];
  
        if (errors.length) {
          var notFoundErrors = errors.filter(function (err) {
            return err.message.indexOf('not found') > -1;
          }).map(function (err) {
            return err.id;
          });
          var malformedErrors = errors.filter(function (err) {
            return err.message.indexOf('malformed') > -1;
          }).map(function (err) {
            return err.id;
          });
  
          if (notFoundErrors.length) {
a1a3bc73   Luigi Serra   graphs updates
2802
            _this13.htmloptimizer.dump('[l10n] [' + code + ']: ' + notFoundErrors.length + ' missing compared to en-US: ' + notFoundErrors.join(', '));
0e9aeacd   root   localization l20n
2803
2804
          }
          if (malformedErrors.length) {
a1a3bc73   Luigi Serra   graphs updates
2805
            _this13.htmloptimizer.dump('[l10n] [' + code + ']: ' + malformedErrors.length + ' malformed compared to en-US: ' + malformedErrors.join(', '));
0e9aeacd   root   localization l20n
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
          }
        }
  
        return entries;
      });
    };
  
    View.prototype.checkError = function checkError() {
      return {
        wait: false,
        error: this.stopBuildError
      };
    };
  
    return View;
  })();
  
  function amendError(err) {
    err.message = err.message + ' (' + this.htmloptimizer.webapp.url + ')';
  }
  
  function logError(err) {
    this.htmloptimizer.dump('[l10n] ' + err);
  }
  
  function stopBuild(err) {
    if (err.lang && err.lang.code === 'en-US' && !this.stopBuildError) {
      this.stopBuildError = err;
    }
  }
  
a1a3bc73   Luigi Serra   graphs updates
2837
2838
2839
  function getDirection(code) {
    var tag = code.split('-')[0];
    return ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(tag) >= 0 ? 'rtl' : 'ltr';
0e9aeacd   root   localization l20n
2840
2841
2842
2843
  }
  
  function getView(htmloptimizer) {
    var htmlFetch = function () {
a1a3bc73   Luigi Serra   graphs updates
2844
2845
      for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
        args[_key4] = arguments[_key4];
0e9aeacd   root   localization l20n
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
      }
  
      return fetchResource.apply(undefined, [htmloptimizer].concat(args));
    };
    return new View(htmloptimizer, htmlFetch);
  }
  
  exports.getView = getView;
  exports.pseudo = pseudo$1;
  exports.walkValue = walkValue$1;