Blame view

bower_components/l20n/dist/compat/bridge/l20n-service.js 51.9 KB
0e9aeacd   root   localization l20n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  'use strict';
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  
  (function () {
    'use strict';
  
    var Service = bridge.service;
    var channel = new BroadcastChannel('l20n-channel');
  
    function broadcast(type, data) {
      return this.service.broadcast(type, data);
    }
  
    function L10nError(message, id, lang) {
      this.name = 'L10nError';
      this.message = message;
      this.id = id;
      this.lang = lang;
    }
    L10nError.prototype = Object.create(Error.prototype);
    L10nError.prototype.constructor = L10nError;
  
    function load(type, url) {
      return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
  
        if (xhr.overrideMimeType) {
          xhr.overrideMimeType(type);
        }
  
        xhr.open('GET', url, true);
  
        if (type === 'application/json') {
          xhr.responseType = 'json';
        }
  
        xhr.addEventListener('load', function io_onload(e) {
          if (e.target.status === 200 || e.target.status === 0) {
            resolve(e.target.response || e.target.responseText);
          } else {
            reject(new L10nError('Not found: ' + url));
          }
        });
        xhr.addEventListener('error', reject);
        xhr.addEventListener('timeout', reject);
  
        try {
          xhr.send(null);
        } catch (e) {
          if (e.name === 'NS_ERROR_FILE_NOT_FOUND') {
            reject(new L10nError('Not found: ' + url));
          } else {
            throw e;
          }
        }
      });
    }
  
    var io = {
      extra: function (code, ver, path, type) {
        return navigator.mozApps.getLocalizationResource(code, ver, path, type);
      },
      app: function (code, ver, path, type) {
        switch (type) {
          case 'text':
            return load('text/plain', path);
          case 'json':
            return load('application/json', path);
          default:
            throw new L10nError('Unknown file type: ' + type);
        }
      }
    };
  
c5169e0e   Renato De Donato   a new hope
76
77
    function fetchResource(ver, res, lang) {
      var url = res.replace('{locale}', lang.code);
0e9aeacd   root   localization l20n
78
      var type = res.endsWith('.json') ? 'json' : 'text';
c5169e0e   Renato De Donato   a new hope
79
      return io[lang.src](lang.code, ver, url, type);
0e9aeacd   root   localization l20n
80
81
    }
  
c5169e0e   Renato De Donato   a new hope
82
    var MAX_PLACEABLES$1 = 100;
0e9aeacd   root   localization l20n
83
  
c5169e0e   Renato De Donato   a new hope
84
85
86
87
88
89
90
    var L20nParser = {
      parse: function (emit, string) {
        this._source = string;
        this._index = 0;
        this._length = string.length;
        this.entries = Object.create(null);
        this.emit = emit;
0e9aeacd   root   localization l20n
91
  
c5169e0e   Renato De Donato   a new hope
92
93
        return this.getResource();
      },
0e9aeacd   root   localization l20n
94
  
c5169e0e   Renato De Donato   a new hope
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      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;
            }
          }
0e9aeacd   root   localization l20n
110
  
c5169e0e   Renato De Donato   a new hope
111
112
113
          if (this._index < this._length) {
            this.getWS();
          }
0e9aeacd   root   localization l20n
114
        }
0e9aeacd   root   localization l20n
115
  
c5169e0e   Renato De Donato   a new hope
116
117
        return this.entries;
      },
0e9aeacd   root   localization l20n
118
  
c5169e0e   Renato De Donato   a new hope
119
120
121
122
123
124
125
126
127
128
      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
129
  
c5169e0e   Renato De Donato   a new hope
130
131
132
        if (this._source.startsWith('/*', this._index)) {
          return this.getComment();
        }
0e9aeacd   root   localization l20n
133
  
c5169e0e   Renato De Donato   a new hope
134
135
        throw this.error('Invalid entry');
      },
0e9aeacd   root   localization l20n
136
  
c5169e0e   Renato De Donato   a new hope
137
138
139
140
      getEntity: function (id, index) {
        if (!this.getRequiredWS()) {
          throw this.error('Expected white space');
        }
0e9aeacd   root   localization l20n
141
  
c5169e0e   Renato De Donato   a new hope
142
143
144
        var ch = this._source[this._index];
        var value = this.getValue(ch, index === undefined);
        var attrs = undefined;
0e9aeacd   root   localization l20n
145
  
c5169e0e   Renato De Donato   a new hope
146
147
148
149
150
151
152
153
154
155
156
157
158
        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
159
        }
0e9aeacd   root   localization l20n
160
  
c5169e0e   Renato De Donato   a new hope
161
        ++this._index;
0e9aeacd   root   localization l20n
162
  
c5169e0e   Renato De Donato   a new hope
163
164
165
166
167
        if (id in this.entries) {
          throw this.error('Duplicate entry ID "' + id, 'duplicateerror');
        }
        if (!attrs && !index && typeof value === 'string') {
          this.entries[id] = value;
a1a3bc73   Luigi Serra   graphs updates
168
        } else {
c5169e0e   Renato De Donato   a new hope
169
170
171
172
173
174
175
          this.entries[id] = {
            value: value,
            attrs: attrs,
            index: index
          };
        }
      },
0e9aeacd   root   localization l20n
176
  
c5169e0e   Renato De Donato   a new hope
177
178
179
      getValue: function () {
        var ch = arguments.length <= 0 || arguments[0] === undefined ? this._source[this._index] : arguments[0];
        var optional = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
0e9aeacd   root   localization l20n
180
  
c5169e0e   Renato De Donato   a new hope
181
182
183
184
185
186
        switch (ch) {
          case '\'':
          case '"':
            return this.getString(ch, 1);
          case '{':
            return this.getHash();
0e9aeacd   root   localization l20n
187
        }
0e9aeacd   root   localization l20n
188
  
c5169e0e   Renato De Donato   a new hope
189
190
191
        if (!optional) {
          throw this.error('Unknown value type');
        }
0e9aeacd   root   localization l20n
192
  
c5169e0e   Renato De Donato   a new hope
193
194
        return;
      },
0e9aeacd   root   localization l20n
195
  
c5169e0e   Renato De Donato   a new hope
196
197
      getWS: function () {
        var cc = this._source.charCodeAt(this._index);
0e9aeacd   root   localization l20n
198
  
c5169e0e   Renato De Donato   a new hope
199
200
        while (cc === 32 || cc === 10 || cc === 9 || cc === 13) {
          cc = this._source.charCodeAt(++this._index);
a1a3bc73   Luigi Serra   graphs updates
201
        }
c5169e0e   Renato De Donato   a new hope
202
203
204
205
206
207
208
209
      },
  
      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);
a1a3bc73   Luigi Serra   graphs updates
210
        }
c5169e0e   Renato De Donato   a new hope
211
212
        return this._index !== pos;
      },
0e9aeacd   root   localization l20n
213
  
c5169e0e   Renato De Donato   a new hope
214
215
216
      getIdentifier: function () {
        var start = this._index;
        var cc = this._source.charCodeAt(this._index);
0e9aeacd   root   localization l20n
217
  
c5169e0e   Renato De Donato   a new hope
218
219
220
221
222
        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
223
  
c5169e0e   Renato De Donato   a new hope
224
225
226
        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
227
  
c5169e0e   Renato De Donato   a new hope
228
229
        return this._source.slice(start, this._index);
      },
0e9aeacd   root   localization l20n
230
  
c5169e0e   Renato De Donato   a new hope
231
232
233
234
235
236
237
      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
238
        }
c5169e0e   Renato De Donato   a new hope
239
240
241
        this._index++;
        return String.fromCharCode(parseInt(this._source.slice(this._index - 4, this._index), 16));
      },
0e9aeacd   root   localization l20n
242
  
c5169e0e   Renato De Donato   a new hope
243
244
245
246
      stringRe: /"|'|{{|\\/g,
      getString: function (opchar, opcharLen) {
        var body = [];
        var placeables = 0;
0e9aeacd   root   localization l20n
247
  
c5169e0e   Renato De Donato   a new hope
248
249
        this._index += opcharLen;
        var start = this._index;
0e9aeacd   root   localization l20n
250
  
c5169e0e   Renato De Donato   a new hope
251
252
        var bufStart = start;
        var buf = '';
0e9aeacd   root   localization l20n
253
  
c5169e0e   Renato De Donato   a new hope
254
255
256
        while (true) {
          this.stringRe.lastIndex = this._index;
          var match = this.stringRe.exec(this._source);
0e9aeacd   root   localization l20n
257
  
c5169e0e   Renato De Donato   a new hope
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
          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
305
        }
c5169e0e   Renato De Donato   a new hope
306
307
308
  
        if (body.length === 0) {
          return buf + this._source.slice(bufStart, this._index - opcharLen);
0e9aeacd   root   localization l20n
309
        }
c5169e0e   Renato De Donato   a new hope
310
311
312
  
        if (this._index - opcharLen > bufStart || buf.length > 0) {
          body.push(buf + this._source.slice(bufStart, this._index - opcharLen));
0e9aeacd   root   localization l20n
313
        }
c5169e0e   Renato De Donato   a new hope
314
315
  
        return body;
a1a3bc73   Luigi Serra   graphs updates
316
      },
c5169e0e   Renato De Donato   a new hope
317
318
319
320
321
322
323
324
325
326
327
328
329
  
      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
330
        }
c5169e0e   Renato De Donato   a new hope
331
        return attrs;
0e9aeacd   root   localization l20n
332
      },
c5169e0e   Renato De Donato   a new hope
333
334
335
336
337
338
339
340
341
  
      getAttribute: function (attrs) {
        var key = this.getIdentifier();
        var index = undefined;
  
        if (this._source[this._index] === '[') {
          ++this._index;
          this.getWS();
          index = this.getItemList(this.getExpression, ']');
a1a3bc73   Luigi Serra   graphs updates
342
        }
c5169e0e   Renato De Donato   a new hope
343
344
345
        this.getWS();
        if (this._source[this._index] !== ':') {
          throw this.error('Expected ":"');
0e9aeacd   root   localization l20n
346
        }
c5169e0e   Renato De Donato   a new hope
347
348
349
350
351
352
        ++this._index;
        this.getWS();
        var value = this.getValue();
  
        if (key in attrs) {
          throw this.error('Duplicate attribute "' + key, 'duplicateerror');
0e9aeacd   root   localization l20n
353
        }
c5169e0e   Renato De Donato   a new hope
354
355
356
357
358
359
360
361
  
        if (!index && typeof value === 'string') {
          attrs[key] = value;
        } else {
          attrs[key] = {
            value: value,
            index: index
          };
0e9aeacd   root   localization l20n
362
        }
0e9aeacd   root   localization l20n
363
      },
c5169e0e   Renato De Donato   a new hope
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
  
      getHash: function () {
        var items = Object.create(null);
  
        ++this._index;
        this.getWS();
  
        var defKey = undefined;
  
        while (true) {
          var _getHashItem = this.getHashItem();
  
          var key = _getHashItem[0];
          var value = _getHashItem[1];
          var def = _getHashItem[2];
  
          items[key] = value;
  
          if (def) {
            if (defKey) {
              throw this.error('Default item redefinition forbidden');
            }
            defKey = key;
          }
          this.getWS();
  
          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 "}"');
          }
a1a3bc73   Luigi Serra   graphs updates
402
        }
c5169e0e   Renato De Donato   a new hope
403
404
405
  
        if (defKey) {
          items.__default = defKey;
0e9aeacd   root   localization l20n
406
        }
c5169e0e   Renato De Donato   a new hope
407
408
  
        return items;
0e9aeacd   root   localization l20n
409
      },
c5169e0e   Renato De Donato   a new hope
410
411
412
413
414
415
  
      getHashItem: function () {
        var defItem = false;
        if (this._source[this._index] === '*') {
          ++this._index;
          defItem = true;
a1a3bc73   Luigi Serra   graphs updates
416
        }
c5169e0e   Renato De Donato   a new hope
417
418
419
420
421
  
        var key = this.getIdentifier();
        this.getWS();
        if (this._source[this._index] !== ':') {
          throw this.error('Expected ":"');
a1a3bc73   Luigi Serra   graphs updates
422
        }
c5169e0e   Renato De Donato   a new hope
423
424
425
426
        ++this._index;
        this.getWS();
  
        return [key, this.getValue(), defItem];
a1a3bc73   Luigi Serra   graphs updates
427
      },
c5169e0e   Renato De Donato   a new hope
428
429
430
431
432
433
434
435
  
      getComment: function () {
        this._index += 2;
        var start = this._index;
        var end = this._source.indexOf('*/', start);
  
        if (end === -1) {
          throw this.error('Comment without a closing tag');
a1a3bc73   Luigi Serra   graphs updates
436
        }
c5169e0e   Renato De Donato   a new hope
437
438
  
        this._index = end + 2;
a1a3bc73   Luigi Serra   graphs updates
439
      },
c5169e0e   Renato De Donato   a new hope
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  
      getExpression: function () {
        var exp = this.getPrimaryExpression();
  
        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;
          }
a1a3bc73   Luigi Serra   graphs updates
455
        }
c5169e0e   Renato De Donato   a new hope
456
457
  
        return exp;
a1a3bc73   Luigi Serra   graphs updates
458
      },
c5169e0e   Renato De Donato   a new hope
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  
      getPropertyExpression: function (idref, computed) {
        var exp = undefined;
  
        if (computed) {
          this.getWS();
          exp = this.getExpression();
          this.getWS();
          if (this._source[this._index] !== ']') {
            throw this.error('Expected "]"');
          }
          ++this._index;
        } else {
          exp = this.getIdentifier();
a1a3bc73   Luigi Serra   graphs updates
473
        }
c5169e0e   Renato De Donato   a new hope
474
475
476
477
478
479
480
  
        return {
          type: 'prop',
          expr: idref,
          prop: exp,
          cmpt: computed
        };
a1a3bc73   Luigi Serra   graphs updates
481
      },
c5169e0e   Renato De Donato   a new hope
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  
      getCallExpression: function (callee) {
        this.getWS();
  
        return {
          type: 'call',
          expr: callee,
          args: this.getItemList(this.getExpression, ')')
        };
      },
  
      getPrimaryExpression: function () {
        var ch = this._source[this._index];
  
        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()
            };
a1a3bc73   Luigi Serra   graphs updates
514
        }
c5169e0e   Renato De Donato   a new hope
515
516
517
518
519
520
521
522
523
524
525
      },
  
      getItemList: function (callback, closeChar) {
        var items = [];
        var closed = false;
  
        this.getWS();
  
        if (this._source[this._index] === closeChar) {
          ++this._index;
          closed = true;
a1a3bc73   Luigi Serra   graphs updates
526
        }
c5169e0e   Renato De Donato   a new hope
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  
        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 + '"');
          }
a1a3bc73   Luigi Serra   graphs updates
544
        }
c5169e0e   Renato De Donato   a new hope
545
546
  
        return items;
a1a3bc73   Luigi Serra   graphs updates
547
      },
c5169e0e   Renato De Donato   a new hope
548
549
550
551
552
553
554
555
  
      getJunkEntry: function () {
        var pos = this._index;
        var nextEntity = this._source.indexOf('<', pos);
        var nextComment = this._source.indexOf('/*', pos);
  
        if (nextEntity === -1) {
          nextEntity = this._length;
a1a3bc73   Luigi Serra   graphs updates
556
        }
c5169e0e   Renato De Donato   a new hope
557
558
        if (nextComment === -1) {
          nextComment = this._length;
a1a3bc73   Luigi Serra   graphs updates
559
        }
c5169e0e   Renato De Donato   a new hope
560
561
562
563
  
        var nextEntry = Math.min(nextEntity, nextComment);
  
        this._index = nextEntry;
a1a3bc73   Luigi Serra   graphs updates
564
      },
c5169e0e   Renato De Donato   a new hope
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
  
      error: function (message) {
        var type = arguments.length <= 1 || arguments[1] === undefined ? 'parsererror' : arguments[1];
  
        var pos = this._index;
  
        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);
  
        var msg = message + ' at pos ' + pos + ': `' + context + '`';
        var err = new L10nError(msg);
        if (this.emit) {
          this.emit(type, err);
a1a3bc73   Luigi Serra   graphs updates
580
        }
c5169e0e   Renato De Donato   a new hope
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
        return err;
      }
    };
  
    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*\}\}/
        };
a1a3bc73   Luigi Serra   graphs updates
603
      },
c5169e0e   Renato De Donato   a new hope
604
605
606
607
  
      parse: function (emit, source) {
        if (!this.patterns) {
          this.init();
a1a3bc73   Luigi Serra   graphs updates
608
        }
c5169e0e   Renato De Donato   a new hope
609
610
611
612
613
614
615
        this.emit = emit;
  
        var entries = {};
  
        var lines = source.match(this.patterns.entries);
        if (!lines) {
          return entries;
a1a3bc73   Luigi Serra   graphs updates
616
        }
c5169e0e   Renato De Donato   a new hope
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
        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;
              }
            }
          }
a1a3bc73   Luigi Serra   graphs updates
638
        }
c5169e0e   Renato De Donato   a new hope
639
        return entries;
a1a3bc73   Luigi Serra   graphs updates
640
      },
c5169e0e   Renato De Donato   a new hope
641
642
643
644
645
646
647
648
649
650
651
  
      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;
a1a3bc73   Luigi Serra   graphs updates
652
        }
c5169e0e   Renato De Donato   a new hope
653
654
655
656
657
  
        var nameElements = name.split('.');
  
        if (nameElements.length > 2) {
          throw this.error('Error in ID: "' + name + '".' + ' Nested attributes are not supported.');
a1a3bc73   Luigi Serra   graphs updates
658
        }
c5169e0e   Renato De Donato   a new hope
659
660
661
662
663
664
665
666
667
668
669
  
        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
670
        }
c5169e0e   Renato De Donato   a new hope
671
672
  
        this.setEntityValue(name, attr, key, this.unescapeString(value), entries);
0e9aeacd   root   localization l20n
673
      },
c5169e0e   Renato De Donato   a new hope
674
675
676
677
678
679
680
681
682
683
684
685
  
      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;
a1a3bc73   Luigi Serra   graphs updates
686
        }
c5169e0e   Renato De Donato   a new hope
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
  
        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;
0e9aeacd   root   localization l20n
702
        }
c5169e0e   Renato De Donato   a new hope
703
704
705
706
707
708
709
710
711
712
713
714
  
        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
715
        }
c5169e0e   Renato De Donato   a new hope
716
717
718
719
720
721
722
723
724
725
726
  
        if (isSimpleValue && (!entries[id] || isSimpleNode)) {
          if (id in root) {
            throw this.error();
          }
          root[id] = value;
        } else {
          if (!root[id]) {
            root[id] = Object.create(null);
          }
          root[id].value = value;
a1a3bc73   Luigi Serra   graphs updates
727
        }
a1a3bc73   Luigi Serra   graphs updates
728
      },
c5169e0e   Renato De Donato   a new hope
729
730
731
732
733
734
735
736
737
738
  
      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 + ')');
a1a3bc73   Luigi Serra   graphs updates
739
        }
c5169e0e   Renato De Donato   a new hope
740
741
742
743
744
745
746
747
748
749
750
751
  
        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]);
          }
        }
        return complexStr;
a1a3bc73   Luigi Serra   graphs updates
752
      },
c5169e0e   Renato De Donato   a new hope
753
754
755
756
  
      unescapeString: function (str) {
        if (str.lastIndexOf('\\') !== -1) {
          str = str.replace(this.patterns.controlChars, '$1');
a1a3bc73   Luigi Serra   graphs updates
757
        }
c5169e0e   Renato De Donato   a new hope
758
759
760
        return str.replace(this.patterns.unicode, function (match, token) {
          return String.fromCodePoint(parseInt(token, 16));
        });
a1a3bc73   Luigi Serra   graphs updates
761
      },
c5169e0e   Renato De Donato   a new hope
762
763
764
765
766
  
      parseIndex: function (str) {
        var match = str.match(this.patterns.index);
        if (!match) {
          throw new L10nError('Malformed index');
a1a3bc73   Luigi Serra   graphs updates
767
        }
c5169e0e   Renato De Donato   a new hope
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
        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] }];
a1a3bc73   Luigi Serra   graphs updates
786
        }
c5169e0e   Renato De Donato   a new hope
787
788
789
790
791
792
793
794
      },
  
      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
795
        }
c5169e0e   Renato De Donato   a new hope
796
        return err;
a1a3bc73   Luigi Serra   graphs updates
797
798
799
      }
    };
  
c5169e0e   Renato De Donato   a new hope
800
801
802
803
804
805
806
807
808
809
810
    var KNOWN_MACROS = ['plural'];
    var MAX_PLACEABLE_LENGTH = 2500;
  
    var FSI = '⁨';
    var PDI = '⁩';
  
    var resolutionChain = new WeakSet();
  
    function format(ctx, lang, args, entity) {
      if (typeof entity === 'string') {
        return [{}, entity];
a1a3bc73   Luigi Serra   graphs updates
811
      }
a1a3bc73   Luigi Serra   graphs updates
812
  
c5169e0e   Renato De Donato   a new hope
813
814
      if (resolutionChain.has(entity)) {
        throw new L10nError('Cyclic reference detected');
a1a3bc73   Luigi Serra   graphs updates
815
      }
a1a3bc73   Luigi Serra   graphs updates
816
  
c5169e0e   Renato De Donato   a new hope
817
      resolutionChain.add(entity);
a1a3bc73   Luigi Serra   graphs updates
818
  
c5169e0e   Renato De Donato   a new hope
819
      var rv = undefined;
a1a3bc73   Luigi Serra   graphs updates
820
  
c5169e0e   Renato De Donato   a new hope
821
822
823
824
825
826
827
828
829
830
831
      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)];
a1a3bc73   Luigi Serra   graphs updates
832
833
      }
  
c5169e0e   Renato De Donato   a new hope
834
835
836
837
838
      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);
a1a3bc73   Luigi Serra   graphs updates
839
        }
c5169e0e   Renato De Donato   a new hope
840
      }
a1a3bc73   Luigi Serra   graphs updates
841
  
c5169e0e   Renato De Donato   a new hope
842
843
844
      if (id === '__proto__') {
        throw new L10nError('Illegal id: ' + id);
      }
a1a3bc73   Luigi Serra   graphs updates
845
  
c5169e0e   Renato De Donato   a new hope
846
      var entity = ctx._getEntity(lang, id);
a1a3bc73   Luigi Serra   graphs updates
847
  
c5169e0e   Renato De Donato   a new hope
848
849
850
      if (entity) {
        return format(ctx, lang, args, entity);
      }
a1a3bc73   Luigi Serra   graphs updates
851
  
c5169e0e   Renato De Donato   a new hope
852
853
      throw new L10nError('Unknown reference: ' + id);
    }
a1a3bc73   Luigi Serra   graphs updates
854
  
c5169e0e   Renato De Donato   a new hope
855
856
857
    function subPlaceable(locals, ctx, lang, args, id) {
      var newLocals = undefined,
          value = undefined;
a1a3bc73   Luigi Serra   graphs updates
858
  
c5169e0e   Renato De Donato   a new hope
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
      try {
        var _resolveIdentifier = resolveIdentifier(ctx, lang, args, id);
  
        newLocals = _resolveIdentifier[0];
        value = _resolveIdentifier[1];
      } catch (err) {
        return [{ error: err }, FSI + '{{ ' + id + ' }}' + PDI];
      }
  
      if (typeof value === 'number') {
        var formatter = ctx._getNumberFormatter(lang);
        return [newLocals, formatter.format(value)];
      }
  
      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 + ')');
a1a3bc73   Luigi Serra   graphs updates
876
        }
c5169e0e   Renato De Donato   a new hope
877
878
        return [newLocals, FSI + value + PDI];
      }
a1a3bc73   Luigi Serra   graphs updates
879
  
c5169e0e   Renato De Donato   a new hope
880
881
      return [{}, FSI + '{{ ' + id + ' }}' + PDI];
    }
a1a3bc73   Luigi Serra   graphs updates
882
  
c5169e0e   Renato De Donato   a new hope
883
884
885
886
    function interpolate(locals, ctx, lang, args, arr) {
      return arr.reduce(function (_ref2, cur) {
        var localsSeq = _ref2[0];
        var valueSeq = _ref2[1];
a1a3bc73   Luigi Serra   graphs updates
887
  
c5169e0e   Renato De Donato   a new hope
888
889
890
891
        if (typeof cur === 'string') {
          return [localsSeq, valueSeq + cur];
        } else {
          var _subPlaceable = subPlaceable(locals, ctx, lang, args, cur.name);
a1a3bc73   Luigi Serra   graphs updates
892
  
c5169e0e   Renato De Donato   a new hope
893
          var value = _subPlaceable[1];
a1a3bc73   Luigi Serra   graphs updates
894
  
c5169e0e   Renato De Donato   a new hope
895
          return [localsSeq, valueSeq + value];
a1a3bc73   Luigi Serra   graphs updates
896
        }
c5169e0e   Renato De Donato   a new hope
897
898
      }, [locals, '']);
    }
a1a3bc73   Luigi Serra   graphs updates
899
  
c5169e0e   Renato De Donato   a new hope
900
901
902
903
904
905
906
907
    function resolveSelector(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(ctx, lang, args, selectorName)[1];
a1a3bc73   Luigi Serra   graphs updates
908
  
c5169e0e   Renato De Donato   a new hope
909
910
911
      if (typeof selector !== 'function') {
        return selector;
      }
a1a3bc73   Luigi Serra   graphs updates
912
  
c5169e0e   Renato De Donato   a new hope
913
      var argValue = index[0].args ? resolveIdentifier(ctx, lang, args, index[0].args[0].name)[1] : undefined;
a1a3bc73   Luigi Serra   graphs updates
914
  
c5169e0e   Renato De Donato   a new hope
915
916
917
      if (selectorName === 'plural') {
        if (argValue === 0 && 'zero' in expr) {
          return 'zero';
a1a3bc73   Luigi Serra   graphs updates
918
        }
c5169e0e   Renato De Donato   a new hope
919
920
921
922
923
924
925
        if (argValue === 1 && 'one' in expr) {
          return 'one';
        }
        if (argValue === 2 && 'two' in expr) {
          return 'two';
        }
      }
a1a3bc73   Luigi Serra   graphs updates
926
  
c5169e0e   Renato De Donato   a new hope
927
928
      return selector(argValue);
    }
a1a3bc73   Luigi Serra   graphs updates
929
  
c5169e0e   Renato De Donato   a new hope
930
931
932
933
    function resolveValue(locals, ctx, lang, args, expr, index) {
      if (!expr) {
        return [locals, expr];
      }
a1a3bc73   Luigi Serra   graphs updates
934
  
c5169e0e   Renato De Donato   a new hope
935
936
937
      if (typeof expr === 'string' || typeof expr === 'boolean' || typeof expr === 'number') {
        return [locals, expr];
      }
a1a3bc73   Luigi Serra   graphs updates
938
  
c5169e0e   Renato De Donato   a new hope
939
940
941
      if (Array.isArray(expr)) {
        return interpolate(locals, ctx, lang, args, expr);
      }
a1a3bc73   Luigi Serra   graphs updates
942
  
c5169e0e   Renato De Donato   a new hope
943
944
945
946
      if (index) {
        var selector = resolveSelector(ctx, lang, args, expr, index);
        if (selector in expr) {
          return resolveValue(locals, ctx, lang, args, expr[selector]);
0e9aeacd   root   localization l20n
947
        }
c5169e0e   Renato De Donato   a new hope
948
      }
a1a3bc73   Luigi Serra   graphs updates
949
  
c5169e0e   Renato De Donato   a new hope
950
951
952
953
      var defaultKey = expr.__default || 'other';
      if (defaultKey in expr) {
        return resolveValue(locals, ctx, lang, args, expr[defaultKey]);
      }
a1a3bc73   Luigi Serra   graphs updates
954
  
c5169e0e   Renato De Donato   a new hope
955
956
      throw new L10nError('Unresolvable value');
    }
a1a3bc73   Luigi Serra   graphs updates
957
  
c5169e0e   Renato De Donato   a new hope
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
    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
    };
a1a3bc73   Luigi Serra   graphs updates
1130
  
c5169e0e   Renato De Donato   a new hope
1131
1132
1133
1134
1135
1136
    function isIn(n, list) {
      return list.indexOf(n) !== -1;
    }
    function isBetween(n, start, end) {
      return typeof n === typeof start && start <= n && n <= end;
    }
a1a3bc73   Luigi Serra   graphs updates
1137
  
c5169e0e   Renato De Donato   a new hope
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
    var pluralRules = {
      '0': function () {
        return 'other';
      },
      '1': function (n) {
        if (isBetween(n % 100, 3, 10)) {
          return 'few';
        }
        if (n === 0) {
          return 'zero';
0e9aeacd   root   localization l20n
1148
        }
c5169e0e   Renato De Donato   a new hope
1149
1150
        if (isBetween(n % 100, 11, 99)) {
          return 'many';
0e9aeacd   root   localization l20n
1151
        }
c5169e0e   Renato De Donato   a new hope
1152
1153
        if (n === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1154
        }
c5169e0e   Renato De Donato   a new hope
1155
1156
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1157
        }
c5169e0e   Renato De Donato   a new hope
1158
1159
1160
1161
1162
        return 'other';
      },
      '2': function (n) {
        if (n !== 0 && n % 10 === 0) {
          return 'many';
0e9aeacd   root   localization l20n
1163
        }
c5169e0e   Renato De Donato   a new hope
1164
1165
        if (n === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1166
        }
c5169e0e   Renato De Donato   a new hope
1167
1168
1169
1170
        if (n === 1) {
          return 'one';
        }
        return 'other';
0e9aeacd   root   localization l20n
1171
      },
c5169e0e   Renato De Donato   a new hope
1172
1173
1174
      '3': function (n) {
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1175
        }
c5169e0e   Renato De Donato   a new hope
1176
1177
1178
1179
1180
        return 'other';
      },
      '4': function (n) {
        if (isBetween(n, 0, 1)) {
          return 'one';
0e9aeacd   root   localization l20n
1181
        }
c5169e0e   Renato De Donato   a new hope
1182
1183
1184
1185
1186
        return 'other';
      },
      '5': function (n) {
        if (isBetween(n, 0, 2) && n !== 2) {
          return 'one';
a1a3bc73   Luigi Serra   graphs updates
1187
        }
c5169e0e   Renato De Donato   a new hope
1188
        return 'other';
0e9aeacd   root   localization l20n
1189
      },
c5169e0e   Renato De Donato   a new hope
1190
1191
1192
      '6': function (n) {
        if (n === 0) {
          return 'zero';
0e9aeacd   root   localization l20n
1193
        }
c5169e0e   Renato De Donato   a new hope
1194
1195
        if (n % 10 === 1 && n % 100 !== 11) {
          return 'one';
0e9aeacd   root   localization l20n
1196
        }
c5169e0e   Renato De Donato   a new hope
1197
1198
1199
1200
1201
        return 'other';
      },
      '7': function (n) {
        if (n === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1202
        }
c5169e0e   Renato De Donato   a new hope
1203
1204
1205
1206
        if (n === 1) {
          return 'one';
        }
        return 'other';
0e9aeacd   root   localization l20n
1207
      },
c5169e0e   Renato De Donato   a new hope
1208
1209
1210
      '8': function (n) {
        if (isBetween(n, 3, 6)) {
          return 'few';
0e9aeacd   root   localization l20n
1211
        }
c5169e0e   Renato De Donato   a new hope
1212
1213
        if (isBetween(n, 7, 10)) {
          return 'many';
0e9aeacd   root   localization l20n
1214
        }
c5169e0e   Renato De Donato   a new hope
1215
1216
        if (n === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1217
        }
c5169e0e   Renato De Donato   a new hope
1218
1219
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1220
        }
c5169e0e   Renato De Donato   a new hope
1221
        return 'other';
0e9aeacd   root   localization l20n
1222
      },
c5169e0e   Renato De Donato   a new hope
1223
1224
1225
      '9': function (n) {
        if (n === 0 || n !== 1 && isBetween(n % 100, 1, 19)) {
          return 'few';
0e9aeacd   root   localization l20n
1226
        }
c5169e0e   Renato De Donato   a new hope
1227
1228
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1229
        }
c5169e0e   Renato De Donato   a new hope
1230
        return 'other';
0e9aeacd   root   localization l20n
1231
      },
c5169e0e   Renato De Donato   a new hope
1232
1233
1234
      '10': function (n) {
        if (isBetween(n % 10, 2, 9) && !isBetween(n % 100, 11, 19)) {
          return 'few';
0e9aeacd   root   localization l20n
1235
        }
c5169e0e   Renato De Donato   a new hope
1236
1237
1238
1239
        if (n % 10 === 1 && !isBetween(n % 100, 11, 19)) {
          return 'one';
        }
        return 'other';
a1a3bc73   Luigi Serra   graphs updates
1240
      },
c5169e0e   Renato De Donato   a new hope
1241
1242
1243
      '11': function (n) {
        if (isBetween(n % 10, 2, 4) && !isBetween(n % 100, 12, 14)) {
          return 'few';
0e9aeacd   root   localization l20n
1244
        }
c5169e0e   Renato De Donato   a new hope
1245
1246
1247
1248
1249
        if (n % 10 === 0 || isBetween(n % 10, 5, 9) || isBetween(n % 100, 11, 14)) {
          return 'many';
        }
        if (n % 10 === 1 && n % 100 !== 11) {
          return 'one';
0e9aeacd   root   localization l20n
1250
        }
c5169e0e   Renato De Donato   a new hope
1251
        return 'other';
0e9aeacd   root   localization l20n
1252
      },
c5169e0e   Renato De Donato   a new hope
1253
1254
1255
      '12': function (n) {
        if (isBetween(n, 2, 4)) {
          return 'few';
0e9aeacd   root   localization l20n
1256
        }
c5169e0e   Renato De Donato   a new hope
1257
1258
1259
1260
        if (n === 1) {
          return 'one';
        }
        return 'other';
0e9aeacd   root   localization l20n
1261
      },
c5169e0e   Renato De Donato   a new hope
1262
1263
1264
      '13': function (n) {
        if (isBetween(n % 10, 2, 4) && !isBetween(n % 100, 12, 14)) {
          return 'few';
0e9aeacd   root   localization l20n
1265
        }
c5169e0e   Renato De Donato   a new hope
1266
1267
1268
1269
1270
1271
1272
        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';
a1a3bc73   Luigi Serra   graphs updates
1273
      },
c5169e0e   Renato De Donato   a new hope
1274
1275
1276
      '14': function (n) {
        if (isBetween(n % 100, 3, 4)) {
          return 'few';
0e9aeacd   root   localization l20n
1277
        }
c5169e0e   Renato De Donato   a new hope
1278
1279
        if (n % 100 === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1280
        }
c5169e0e   Renato De Donato   a new hope
1281
1282
1283
1284
        if (n % 100 === 1) {
          return 'one';
        }
        return 'other';
0e9aeacd   root   localization l20n
1285
      },
c5169e0e   Renato De Donato   a new hope
1286
1287
1288
      '15': function (n) {
        if (n === 0 || isBetween(n % 100, 2, 10)) {
          return 'few';
0e9aeacd   root   localization l20n
1289
        }
c5169e0e   Renato De Donato   a new hope
1290
1291
        if (isBetween(n % 100, 11, 19)) {
          return 'many';
0e9aeacd   root   localization l20n
1292
        }
c5169e0e   Renato De Donato   a new hope
1293
1294
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1295
        }
c5169e0e   Renato De Donato   a new hope
1296
1297
1298
1299
1300
        return 'other';
      },
      '16': function (n) {
        if (n % 10 === 1 && n !== 11) {
          return 'one';
0e9aeacd   root   localization l20n
1301
        }
c5169e0e   Renato De Donato   a new hope
1302
        return 'other';
0e9aeacd   root   localization l20n
1303
      },
c5169e0e   Renato De Donato   a new hope
1304
1305
1306
      '17': function (n) {
        if (n === 3) {
          return 'few';
0e9aeacd   root   localization l20n
1307
        }
c5169e0e   Renato De Donato   a new hope
1308
1309
        if (n === 0) {
          return 'zero';
0e9aeacd   root   localization l20n
1310
        }
c5169e0e   Renato De Donato   a new hope
1311
1312
1313
1314
1315
        if (n === 6) {
          return 'many';
        }
        if (n === 2) {
          return 'two';
0e9aeacd   root   localization l20n
1316
        }
c5169e0e   Renato De Donato   a new hope
1317
1318
1319
1320
        if (n === 1) {
          return 'one';
        }
        return 'other';
a1a3bc73   Luigi Serra   graphs updates
1321
      },
c5169e0e   Renato De Donato   a new hope
1322
1323
1324
      '18': function (n) {
        if (n === 0) {
          return 'zero';
0e9aeacd   root   localization l20n
1325
        }
c5169e0e   Renato De Donato   a new hope
1326
1327
1328
1329
        if (isBetween(n, 0, 2) && n !== 0 && n !== 2) {
          return 'one';
        }
        return 'other';
0e9aeacd   root   localization l20n
1330
      },
c5169e0e   Renato De Donato   a new hope
1331
1332
1333
      '19': function (n) {
        if (isBetween(n, 2, 10)) {
          return 'few';
0e9aeacd   root   localization l20n
1334
        }
c5169e0e   Renato De Donato   a new hope
1335
1336
        if (isBetween(n, 0, 1)) {
          return 'one';
0e9aeacd   root   localization l20n
1337
        }
c5169e0e   Renato De Donato   a new hope
1338
        return 'other';
0e9aeacd   root   localization l20n
1339
      },
c5169e0e   Renato De Donato   a new hope
1340
1341
1342
      '20': function (n) {
        if ((isBetween(n % 10, 3, 4) || n % 10 === 9) && !(isBetween(n % 100, 10, 19) || isBetween(n % 100, 70, 79) || isBetween(n % 100, 90, 99))) {
          return 'few';
0e9aeacd   root   localization l20n
1343
        }
c5169e0e   Renato De Donato   a new hope
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
        if (n % 1000000 === 0 && n !== 0) {
          return 'many';
        }
        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';
0e9aeacd   root   localization l20n
1354
      },
c5169e0e   Renato De Donato   a new hope
1355
1356
1357
      '21': function (n) {
        if (n === 0) {
          return 'zero';
0e9aeacd   root   localization l20n
1358
        }
c5169e0e   Renato De Donato   a new hope
1359
1360
        if (n === 1) {
          return 'one';
0e9aeacd   root   localization l20n
1361
        }
c5169e0e   Renato De Donato   a new hope
1362
1363
1364
1365
1366
        return 'other';
      },
      '22': function (n) {
        if (isBetween(n, 0, 1) || isBetween(n, 11, 99)) {
          return 'one';
0e9aeacd   root   localization l20n
1367
        }
c5169e0e   Renato De Donato   a new hope
1368
        return 'other';
a1a3bc73   Luigi Serra   graphs updates
1369
      },
c5169e0e   Renato De Donato   a new hope
1370
1371
1372
      '23': function (n) {
        if (isBetween(n % 10, 1, 2) || n % 20 === 0) {
          return 'one';
0e9aeacd   root   localization l20n
1373
        }
c5169e0e   Renato De Donato   a new hope
1374
        return 'other';
a1a3bc73   Luigi Serra   graphs updates
1375
      },
c5169e0e   Renato De Donato   a new hope
1376
1377
1378
      '24': function (n) {
        if (isBetween(n, 3, 10) || isBetween(n, 13, 19)) {
          return 'few';
a1a3bc73   Luigi Serra   graphs updates
1379
        }
c5169e0e   Renato De Donato   a new hope
1380
1381
        if (isIn(n, [2, 12])) {
          return 'two';
a1a3bc73   Luigi Serra   graphs updates
1382
        }
c5169e0e   Renato De Donato   a new hope
1383
1384
        if (isIn(n, [1, 11])) {
          return 'one';
a1a3bc73   Luigi Serra   graphs updates
1385
        }
c5169e0e   Renato De Donato   a new hope
1386
1387
1388
        return 'other';
      }
    };
0e9aeacd   root   localization l20n
1389
  
c5169e0e   Renato De Donato   a new hope
1390
1391
1392
1393
1394
1395
1396
1397
1398
    function getPluralRule(code) {
      var index = locales2rules[code.replace(/-.*$/, '')];
      if (!(index in pluralRules)) {
        return function () {
          return 'other';
        };
      }
      return pluralRules[index];
    }
0e9aeacd   root   localization l20n
1399
  
c5169e0e   Renato De Donato   a new hope
1400
1401
1402
    var Context = (function () {
      function Context(env) {
        _classCallCheck(this, Context);
0e9aeacd   root   localization l20n
1403
  
c5169e0e   Renato De Donato   a new hope
1404
1405
1406
        this._env = env;
        this._numberFormatters = null;
      }
0e9aeacd   root   localization l20n
1407
  
c5169e0e   Renato De Donato   a new hope
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
      Context.prototype._formatTuple = function _formatTuple(lang, args, entity, id, key) {
        try {
          return format(this, lang, args, entity);
        } catch (err) {
          err.id = key ? id + '::' + key : id;
          err.lang = lang;
          this._env.emit('resolveerror', err, this);
          return [{ error: err }, err.id];
        }
      };
0e9aeacd   root   localization l20n
1418
  
c5169e0e   Renato De Donato   a new hope
1419
1420
      Context.prototype._formatEntity = function _formatEntity(lang, args, entity, id) {
        var _formatTuple2 = this._formatTuple(lang, args, entity, id);
0e9aeacd   root   localization l20n
1421
  
c5169e0e   Renato De Donato   a new hope
1422
        var value = _formatTuple2[1];
0e9aeacd   root   localization l20n
1423
  
c5169e0e   Renato De Donato   a new hope
1424
1425
1426
1427
        var formatted = {
          value: value,
          attrs: null
        };
a1a3bc73   Luigi Serra   graphs updates
1428
  
c5169e0e   Renato De Donato   a new hope
1429
1430
1431
1432
        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);
a1a3bc73   Luigi Serra   graphs updates
1433
  
c5169e0e   Renato De Donato   a new hope
1434
1435
1436
            var attrValue = _formatTuple3[1];
  
            formatted.attrs[key] = attrValue;
0e9aeacd   root   localization l20n
1437
1438
1439
          }
        }
  
c5169e0e   Renato De Donato   a new hope
1440
1441
        return formatted;
      };
0e9aeacd   root   localization l20n
1442
  
c5169e0e   Renato De Donato   a new hope
1443
1444
1445
      Context.prototype._formatValue = function _formatValue(lang, args, entity, id) {
        return this._formatTuple(lang, args, entity, id)[1];
      };
0e9aeacd   root   localization l20n
1446
  
c5169e0e   Renato De Donato   a new hope
1447
1448
1449
      Context.prototype.fetch = function fetch(langs) {
        if (langs.length === 0) {
          return Promise.resolve(langs);
0e9aeacd   root   localization l20n
1450
1451
        }
  
c5169e0e   Renato De Donato   a new hope
1452
        var resIds = Array.from(this._env._resLists.get(this));
0e9aeacd   root   localization l20n
1453
  
c5169e0e   Renato De Donato   a new hope
1454
1455
1456
1457
        return Promise.all(resIds.map(this._env._getResource.bind(this._env, langs[0]))).then(function () {
          return langs;
        });
      };
0e9aeacd   root   localization l20n
1458
  
c5169e0e   Renato De Donato   a new hope
1459
1460
      Context.prototype._resolve = function _resolve(langs, keys, formatter, prevResolved) {
        var _this = this;
0e9aeacd   root   localization l20n
1461
  
c5169e0e   Renato De Donato   a new hope
1462
        var lang = langs[0];
0e9aeacd   root   localization l20n
1463
  
c5169e0e   Renato De Donato   a new hope
1464
1465
1466
        if (!lang) {
          return reportMissing.call(this, keys, formatter, prevResolved);
        }
a1a3bc73   Luigi Serra   graphs updates
1467
  
c5169e0e   Renato De Donato   a new hope
1468
        var hasUnresolved = false;
0e9aeacd   root   localization l20n
1469
  
c5169e0e   Renato De Donato   a new hope
1470
1471
1472
        var resolved = keys.map(function (key, i) {
          if (prevResolved && prevResolved[i] !== undefined) {
            return prevResolved[i];
0e9aeacd   root   localization l20n
1473
          }
0e9aeacd   root   localization l20n
1474
  
c5169e0e   Renato De Donato   a new hope
1475
          var _ref3 = Array.isArray(key) ? key : [key, undefined];
0e9aeacd   root   localization l20n
1476
  
c5169e0e   Renato De Donato   a new hope
1477
1478
          var id = _ref3[0];
          var args = _ref3[1];
0e9aeacd   root   localization l20n
1479
  
c5169e0e   Renato De Donato   a new hope
1480
1481
1482
1483
          var entity = _this._getEntity(lang, id);
  
          if (entity) {
            return formatter.call(_this, lang, args, entity, id);
0e9aeacd   root   localization l20n
1484
          }
0e9aeacd   root   localization l20n
1485
  
c5169e0e   Renato De Donato   a new hope
1486
1487
1488
          _this._env.emit('notfounderror', new L10nError('"' + id + '"' + ' not found in ' + lang.code, id, lang), _this);
          hasUnresolved = true;
        });
0e9aeacd   root   localization l20n
1489
  
c5169e0e   Renato De Donato   a new hope
1490
1491
1492
        if (!hasUnresolved) {
          return resolved;
        }
0e9aeacd   root   localization l20n
1493
  
c5169e0e   Renato De Donato   a new hope
1494
1495
1496
1497
        return this.fetch(langs.slice(1)).then(function (nextLangs) {
          return _this._resolve(nextLangs, keys, formatter, resolved);
        });
      };
0e9aeacd   root   localization l20n
1498
  
c5169e0e   Renato De Donato   a new hope
1499
1500
      Context.prototype.resolveEntities = function resolveEntities(langs, keys) {
        var _this2 = this;
0e9aeacd   root   localization l20n
1501
  
c5169e0e   Renato De Donato   a new hope
1502
1503
1504
1505
        return this.fetch(langs).then(function (langs) {
          return _this2._resolve(langs, keys, _this2._formatEntity);
        });
      };
0e9aeacd   root   localization l20n
1506
  
c5169e0e   Renato De Donato   a new hope
1507
1508
      Context.prototype.resolveValues = function resolveValues(langs, keys) {
        var _this3 = this;
0e9aeacd   root   localization l20n
1509
  
c5169e0e   Renato De Donato   a new hope
1510
1511
1512
1513
        return this.fetch(langs).then(function (langs) {
          return _this3._resolve(langs, keys, _this3._formatValue);
        });
      };
0e9aeacd   root   localization l20n
1514
  
c5169e0e   Renato De Donato   a new hope
1515
1516
1517
      Context.prototype._getEntity = function _getEntity(lang, id) {
        var cache = this._env._resCache;
        var resIds = Array.from(this._env._resLists.get(this));
0e9aeacd   root   localization l20n
1518
  
c5169e0e   Renato De Donato   a new hope
1519
1520
1521
1522
1523
1524
1525
        for (var i = 0, resId = undefined; resId = 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
1526
1527
          }
        }
c5169e0e   Renato De Donato   a new hope
1528
1529
        return undefined;
      };
0e9aeacd   root   localization l20n
1530
  
c5169e0e   Renato De Donato   a new hope
1531
1532
1533
      Context.prototype._getNumberFormatter = function _getNumberFormatter(lang) {
        if (!this._numberFormatters) {
          this._numberFormatters = new Map();
a1a3bc73   Luigi Serra   graphs updates
1534
        }
c5169e0e   Renato De Donato   a new hope
1535
1536
1537
1538
1539
1540
        if (!this._numberFormatters.has(lang)) {
          var formatter = Intl.NumberFormat(lang, {
            useGrouping: false
          });
          this._numberFormatters.set(lang, formatter);
          return formatter;
0e9aeacd   root   localization l20n
1541
        }
c5169e0e   Renato De Donato   a new hope
1542
1543
        return this._numberFormatters.get(lang);
      };
0e9aeacd   root   localization l20n
1544
  
c5169e0e   Renato De Donato   a new hope
1545
1546
1547
1548
1549
1550
1551
1552
      Context.prototype._getMacro = function _getMacro(lang, id) {
        switch (id) {
          case 'plural':
            return getPluralRule(lang.code);
          default:
            return undefined;
        }
      };
0e9aeacd   root   localization l20n
1553
  
c5169e0e   Renato De Donato   a new hope
1554
1555
      return Context;
    })();
0e9aeacd   root   localization l20n
1556
  
c5169e0e   Renato De Donato   a new hope
1557
1558
    function reportMissing(keys, formatter, resolved) {
      var _this4 = this;
0e9aeacd   root   localization l20n
1559
  
c5169e0e   Renato De Donato   a new hope
1560
      var missingIds = new Set();
0e9aeacd   root   localization l20n
1561
  
c5169e0e   Renato De Donato   a new hope
1562
1563
1564
      keys.forEach(function (key, i) {
        if (resolved && resolved[i] !== undefined) {
          return;
a1a3bc73   Luigi Serra   graphs updates
1565
        }
c5169e0e   Renato De Donato   a new hope
1566
1567
1568
1569
1570
1571
1572
1573
1574
        var id = Array.isArray(key) ? key[0] : key;
        missingIds.add(id);
        resolved[i] = formatter === _this4._formatValue ? id : { value: id, attrs: null };
      });
  
      this._env.emit('notfounderror', new L10nError('"' + Array.from(missingIds).join(', ') + '"' + ' not found in any language', missingIds), this);
  
      return resolved;
    }
0e9aeacd   root   localization l20n
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
  
    function walkEntry(entry, fn) {
      if (typeof entry === 'string') {
        return fn(entry);
      }
  
      var newEntry = Object.create(null);
  
      if (entry.value) {
        newEntry.value = walkValue(entry.value, fn);
      }
  
      if (entry.index) {
        newEntry.index = entry.index;
      }
  
      if (entry.attrs) {
        newEntry.attrs = Object.create(null);
        for (var key in entry.attrs) {
          newEntry.attrs[key] = walkEntry(entry.attrs[key], fn);
        }
      }
  
      return newEntry;
    }
  
    function walkValue(value, fn) {
      if (typeof value === 'string') {
        return fn(value);
      }
  
      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(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 = Object.defineProperties(Object.create(null), {
      'fr-x-psaccent': {
        enumerable: true,
        get: createGetter('fr-x-psaccent', 'Runtime Accented')
      },
      'ar-x-psbidi': {
        enumerable: true,
        get: createGetter('ar-x-psbidi', 'Runtime Bidi')
      }
    });
  
    function emit(listeners) {
c5169e0e   Renato De Donato   a new hope
1699
      var _this5 = this;
0e9aeacd   root   localization l20n
1700
  
c5169e0e   Renato De Donato   a new hope
1701
1702
      for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        args[_key - 1] = arguments[_key];
0e9aeacd   root   localization l20n
1703
1704
1705
1706
1707
1708
      }
  
      var type = args.shift();
  
      if (listeners['*']) {
        listeners['*'].slice().forEach(function (listener) {
c5169e0e   Renato De Donato   a new hope
1709
          return listener.apply(_this5, args);
0e9aeacd   root   localization l20n
1710
1711
1712
1713
1714
        });
      }
  
      if (listeners[type]) {
        listeners[type].slice().forEach(function (listener) {
c5169e0e   Renato De Donato   a new hope
1715
          return listener.apply(_this5, args);
0e9aeacd   root   localization l20n
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
        });
      }
    }
  
    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);
    }
  
c5169e0e   Renato De Donato   a new hope
1737
1738
1739
1740
1741
    var parsers = {
      properties: PropertiesParser,
      l20n: L20nParser
    };
  
0e9aeacd   root   localization l20n
1742
    var Env = (function () {
c5169e0e   Renato De Donato   a new hope
1743
      function Env(defaultLang, fetchResource) {
0e9aeacd   root   localization l20n
1744
1745
        _classCallCheck(this, Env);
  
c5169e0e   Renato De Donato   a new hope
1746
        this.defaultLang = defaultLang;
0e9aeacd   root   localization l20n
1747
1748
        this.fetchResource = fetchResource;
  
c5169e0e   Renato De Donato   a new hope
1749
1750
        this._resLists = new Map();
        this._resCache = new Map();
0e9aeacd   root   localization l20n
1751
1752
1753
1754
1755
1756
1757
  
        var listeners = {};
        this.emit = emit.bind(this, listeners);
        this.addEventListener = addEventListener.bind(this, listeners);
        this.removeEventListener = removeEventListener.bind(this, listeners);
      }
  
c5169e0e   Renato De Donato   a new hope
1758
1759
1760
      Env.prototype.createContext = function createContext(resIds) {
        var ctx = new Context(this);
        this._resLists.set(ctx, new Set(resIds));
0e9aeacd   root   localization l20n
1761
1762
1763
1764
        return ctx;
      };
  
      Env.prototype.destroyContext = function destroyContext(ctx) {
c5169e0e   Renato De Donato   a new hope
1765
        var _this6 = this;
0e9aeacd   root   localization l20n
1766
  
c5169e0e   Renato De Donato   a new hope
1767
1768
        var lists = this._resLists;
        var resList = lists.get(ctx);
0e9aeacd   root   localization l20n
1769
  
c5169e0e   Renato De Donato   a new hope
1770
1771
1772
        lists.delete(ctx);
        resList.forEach(function (resId) {
          return deleteIfOrphan(_this6._resCache, lists, resId);
0e9aeacd   root   localization l20n
1773
1774
1775
1776
        });
      };
  
      Env.prototype._parse = function _parse(syntax, lang, data) {
c5169e0e   Renato De Donato   a new hope
1777
        var _this7 = this;
0e9aeacd   root   localization l20n
1778
  
c5169e0e   Renato De Donato   a new hope
1779
        var parser = parsers[syntax];
0e9aeacd   root   localization l20n
1780
1781
1782
1783
1784
        if (!parser) {
          return data;
        }
  
        var emit = function (type, err) {
c5169e0e   Renato De Donato   a new hope
1785
          return _this7.emit(type, amendError(lang, err));
0e9aeacd   root   localization l20n
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
        };
        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[lang.code].process);
        }
        return pseudoentries;
      };
  
      Env.prototype._getResource = function _getResource(lang, res) {
c5169e0e   Renato De Donato   a new hope
1803
        var _this8 = this;
0e9aeacd   root   localization l20n
1804
  
c5169e0e   Renato De Donato   a new hope
1805
        var cache = this._resCache;
0e9aeacd   root   localization l20n
1806
1807
1808
1809
1810
1811
1812
1813
1814
        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) {
c5169e0e   Renato De Donato   a new hope
1815
1816
          var entries = _this8._parse(syntax, lang, data);
          cache.set(id, _this8._create(lang, entries));
0e9aeacd   root   localization l20n
1817
1818
1819
1820
        };
  
        var recover = function (err) {
          err.lang = lang;
c5169e0e   Renato De Donato   a new hope
1821
          _this8.emit('fetcherror', err);
0e9aeacd   root   localization l20n
1822
1823
1824
          cache.set(id, err);
        };
  
c5169e0e   Renato De Donato   a new hope
1825
        var langToFetch = lang.src === 'pseudo' ? { code: this.defaultLang, src: 'app' } : lang;
0e9aeacd   root   localization l20n
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  
        var resource = this.fetchResource(res, langToFetch).then(saveEntries, recover);
  
        cache.set(id, resource);
  
        return resource;
      };
  
      return Env;
    })();
  
c5169e0e   Renato De Donato   a new hope
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
    function deleteIfOrphan(cache, lists, resId) {
      var isNeeded = Array.from(lists).some(function (_ref4) {
        var ctx = _ref4[0];
        var resIds = _ref4[1];
        return resIds.has(resId);
      });
  
      if (!isNeeded) {
        cache.forEach(function (val, key) {
          return key.startsWith(resId) ? cache.delete(key) : null;
        });
      }
    }
  
0e9aeacd   root   localization l20n
1851
1852
1853
1854
1855
    function amendError(lang, err) {
      err.lang = lang;
      return err;
    }
  
c5169e0e   Renato De Donato   a new hope
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
    if (typeof NodeList === 'function' && !NodeList.prototype[Symbol.iterator]) {
      NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
    }
  
    function documentReady() {
      if (document.readyState !== 'loading') {
        return Promise.resolve();
      }
  
      return new Promise(function (resolve) {
        document.addEventListener('readystatechange', function onrsc() {
          document.removeEventListener('readystatechange', onrsc);
          resolve();
        });
      });
    }
  
0e9aeacd   root   localization l20n
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
    function prioritizeLocales(def, availableLangs, requested) {
      var supportedLocale = undefined;
  
      for (var i = 0; i < requested.length; i++) {
        var locale = requested[i];
        if (availableLangs.indexOf(locale) !== -1) {
          supportedLocale = locale;
          break;
        }
      }
      if (!supportedLocale || supportedLocale === def) {
        return [def];
      }
  
      return [supportedLocale, def];
    }
  
c5169e0e   Renato De Donato   a new hope
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
    function getMeta(head) {
      var availableLangs = Object.create(null);
      var defaultLang = null;
      var appVersion = null;
  
      var metas = head.querySelectorAll('meta[name="availableLanguages"],' + 'meta[name="defaultLanguage"],' + 'meta[name="appVersion"]');
      for (var _iterator = metas, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
        var _ref;
  
        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref = _i.value;
        }
  
        var meta = _ref;
  
        var _name = meta.getAttribute('name');
        var content = meta.getAttribute('content').trim();
        switch (_name) {
          case 'availableLanguages':
            availableLangs = getLangRevisionMap(availableLangs, content);
            break;
          case 'defaultLanguage':
            var _getLangRevisionTuple = getLangRevisionTuple(content),
                lang = _getLangRevisionTuple[0],
                rev = _getLangRevisionTuple[1];
  
            defaultLang = lang;
            if (!(lang in availableLangs)) {
              availableLangs[lang] = rev;
            }
            break;
          case 'appVersion':
            appVersion = content;
        }
      }
  
      return {
        defaultLang: defaultLang,
        availableLangs: availableLangs,
        appVersion: appVersion
      };
    }
  
    function getLangRevisionMap(seq, str) {
      return str.split(',').reduce(function (seq, cur) {
        var _getLangRevisionTuple2 = getLangRevisionTuple(cur);
  
        var lang = _getLangRevisionTuple2[0];
        var rev = _getLangRevisionTuple2[1];
  
        seq[lang] = rev;
        return seq;
      }, seq);
    }
  
    function getLangRevisionTuple(str) {
      var _str$trim$split = str.trim().split(':');
  
      var lang = _str$trim$split[0];
      var rev = _str$trim$split[1];
  
      return [lang, parseInt(rev)];
    }
  
    function negotiateLanguages(fn, appVersion, defaultLang, availableLangs, additionalLangs, prevLangs, requestedLangs) {
0e9aeacd   root   localization l20n
1960
  
c5169e0e   Renato De Donato   a new hope
1961
      var allAvailableLangs = Object.keys(availableLangs).concat(additionalLangs || []).concat(Object.keys(pseudo));
0e9aeacd   root   localization l20n
1962
1963
1964
1965
1966
      var newLangs = prioritizeLocales(defaultLang, allAvailableLangs, requestedLangs);
  
      var langs = newLangs.map(function (code) {
        return {
          code: code,
c5169e0e   Renato De Donato   a new hope
1967
          src: getLangSource(appVersion, availableLangs, additionalLangs, code)
0e9aeacd   root   localization l20n
1968
1969
1970
        };
      });
  
c5169e0e   Renato De Donato   a new hope
1971
1972
1973
1974
1975
      if (!arrEqual(prevLangs, newLangs)) {
        fn(langs);
      }
  
      return langs;
0e9aeacd   root   localization l20n
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
    }
  
    function arrEqual(arr1, arr2) {
      return arr1.length === arr2.length && arr1.every(function (elem, i) {
        return elem === arr2[i];
      });
    }
  
    function getMatchingLangpack(appVersion, langpacks) {
      for (var i = 0, langpack = undefined; langpack = langpacks[i]; i++) {
        if (langpack.target === appVersion) {
          return langpack;
        }
      }
      return null;
    }
  
    function getLangSource(appVersion, availableLangs, additionalLangs, code) {
      if (additionalLangs && additionalLangs[code]) {
        var lp = getMatchingLangpack(appVersion, additionalLangs[code]);
        if (lp && (!(code in availableLangs) || parseInt(lp.revision) > availableLangs[code])) {
          return 'extra';
        }
      }
  
      if (code in pseudo && !(code in availableLangs)) {
        return 'pseudo';
      }
  
      return 'app';
    }
  
    var Remote = (function () {
c5169e0e   Renato De Donato   a new hope
2009
2010
2011
      function Remote(fetchResource, broadcast, requestedLangs) {
        var _this9 = this;
  
0e9aeacd   root   localization l20n
2012
2013
        _classCallCheck(this, Remote);
  
c5169e0e   Renato De Donato   a new hope
2014
        this.fetchResource = fetchResource;
0e9aeacd   root   localization l20n
2015
        this.broadcast = broadcast;
0e9aeacd   root   localization l20n
2016
        this.ctxs = new Map();
c5169e0e   Renato De Donato   a new hope
2017
2018
2019
        this.interactive = documentReady().then(function () {
          return _this9.init(requestedLangs);
        });
0e9aeacd   root   localization l20n
2020
2021
      }
  
c5169e0e   Renato De Donato   a new hope
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
      Remote.prototype.init = function init(requestedLangs) {
        var _this10 = this;
  
        var meta = getMeta(document.head);
        this.defaultLanguage = meta.defaultLang;
        this.availableLanguages = meta.availableLangs;
        this.appVersion = meta.appVersion;
  
        this.env = new Env(this.defaultLanguage, function () {
          for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
            args[_key2] = arguments[_key2];
          }
0e9aeacd   root   localization l20n
2034
  
c5169e0e   Renato De Donato   a new hope
2035
2036
          return _this10.fetchResource.apply(_this10, [_this10.appVersion].concat(args));
        });
0e9aeacd   root   localization l20n
2037
  
c5169e0e   Renato De Donato   a new hope
2038
        return this.requestLanguages(requestedLangs);
0e9aeacd   root   localization l20n
2039
2040
      };
  
c5169e0e   Renato De Donato   a new hope
2041
2042
2043
2044
2045
2046
2047
      Remote.prototype.registerView = function registerView(view, resources) {
        var _this11 = this;
  
        return this.interactive.then(function () {
          _this11.ctxs.set(view, _this11.env.createContext(resources));
          return true;
        });
0e9aeacd   root   localization l20n
2048
2049
      };
  
c5169e0e   Renato De Donato   a new hope
2050
2051
2052
      Remote.prototype.unregisterView = function unregisterView(view) {
        return this.ctxs.delete(view);
      };
a1a3bc73   Luigi Serra   graphs updates
2053
  
c5169e0e   Renato De Donato   a new hope
2054
2055
      Remote.prototype.resolveEntities = function resolveEntities(view, langs, keys) {
        return this.ctxs.get(view).resolveEntities(langs, keys);
0e9aeacd   root   localization l20n
2056
2057
2058
      };
  
      Remote.prototype.formatValues = function formatValues(view, keys) {
c5169e0e   Renato De Donato   a new hope
2059
        var _this12 = this;
0e9aeacd   root   localization l20n
2060
  
c5169e0e   Renato De Donato   a new hope
2061
2062
2063
        return this.languages.then(function (langs) {
          return _this12.ctxs.get(view).resolveValues(langs, keys);
        });
0e9aeacd   root   localization l20n
2064
2065
      };
  
c5169e0e   Renato De Donato   a new hope
2066
2067
      Remote.prototype.resolvedLanguages = function resolvedLanguages() {
        return this.languages;
0e9aeacd   root   localization l20n
2068
2069
2070
      };
  
      Remote.prototype.requestLanguages = function requestLanguages(requestedLangs) {
c5169e0e   Renato De Donato   a new hope
2071
        return changeLanguages.call(this, getAdditionalLanguages(), requestedLangs);
0e9aeacd   root   localization l20n
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
      };
  
      Remote.prototype.getName = function getName(code) {
        return pseudo[code].name;
      };
  
      Remote.prototype.processString = function processString(code, str) {
        return pseudo[code].process(str);
      };
  
c5169e0e   Renato De Donato   a new hope
2082
2083
2084
2085
      Remote.prototype.handleEvent = function handleEvent(evt) {
        return changeLanguages.call(this, evt.detail || getAdditionalLanguages(), navigator.languages);
      };
  
0e9aeacd   root   localization l20n
2086
2087
2088
      return Remote;
    })();
  
c5169e0e   Renato De Donato   a new hope
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
    function getAdditionalLanguages() {
      if (navigator.mozApps && navigator.mozApps.getAdditionalLanguages) {
        return navigator.mozApps.getAdditionalLanguages().catch(function () {
          return [];
        });
      }
  
      return Promise.resolve([]);
    }
  
    function changeLanguages(additionalLangs, requestedLangs) {
      var _this13 = this;
  
      var prevLangs = this.languages || [];
      return this.languages = Promise.all([additionalLangs, prevLangs]).then(function (_ref5) {
        var additionalLangs = _ref5[0];
        var prevLangs = _ref5[1];
        return negotiateLanguages(_this13.broadcast.bind(_this13, 'translateDocument'), _this13.appVersion, _this13.defaultLanguage, _this13.availableLanguages, additionalLangs, prevLangs, requestedLangs);
      });
    }
  
    var remote = new Remote(fetchResource, broadcast, navigator.languages);
    window.addEventListener('languagechange', remote);
    document.addEventListener('additionallanguageschange', remote);
0e9aeacd   root   localization l20n
2113
2114
2115
  
    remote.service = new Service('l20n').method('registerView', function () {
      return remote.registerView.apply(remote, arguments);
c5169e0e   Renato De Donato   a new hope
2116
2117
    }).method('resolvedLanguages', function () {
      return remote.resolvedLanguages.apply(remote, arguments);
0e9aeacd   root   localization l20n
2118
2119
    }).method('requestLanguages', function () {
      return remote.requestLanguages.apply(remote, arguments);
c5169e0e   Renato De Donato   a new hope
2120
2121
    }).method('resolveEntities', function () {
      return remote.resolveEntities.apply(remote, arguments);
0e9aeacd   root   localization l20n
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
    }).method('formatValues', function () {
      return remote.formatValues.apply(remote, arguments);
    }).method('getName', function () {
      return remote.getName.apply(remote, arguments);
    }).method('processString', function () {
      return remote.processString.apply(remote, arguments);
    }).on('disconnect', function (clientId) {
      return remote.unregisterView(clientId);
    }).listen(channel);
  })();