ztxkutils 10.0.2 → 10.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2380 @@
1
+ const isString = obj => typeof obj === 'string';
2
+ const defer = () => {
3
+ let res;
4
+ let rej;
5
+ const promise = new Promise((resolve, reject) => {
6
+ res = resolve;
7
+ rej = reject;
8
+ });
9
+ promise.resolve = res;
10
+ promise.reject = rej;
11
+ return promise;
12
+ };
13
+ const makeString = object => {
14
+ if (object == null) return '';
15
+ return '' + object;
16
+ };
17
+ const copy = (a, s, t) => {
18
+ a.forEach(m => {
19
+ if (s[m]) t[m] = s[m];
20
+ });
21
+ };
22
+ const lastOfPathSeparatorRegExp = /###/g;
23
+ const cleanKey = key => key && key.indexOf('###') > -1 ? key.replace(lastOfPathSeparatorRegExp, '.') : key;
24
+ const canNotTraverseDeeper = object => !object || isString(object);
25
+ const getLastOfPath = (object, path, Empty) => {
26
+ const stack = !isString(path) ? path : path.split('.');
27
+ let stackIndex = 0;
28
+ while (stackIndex < stack.length - 1) {
29
+ if (canNotTraverseDeeper(object)) return {};
30
+ const key = cleanKey(stack[stackIndex]);
31
+ if (!object[key] && Empty) object[key] = new Empty();
32
+ if (Object.prototype.hasOwnProperty.call(object, key)) {
33
+ object = object[key];
34
+ } else {
35
+ object = {};
36
+ }
37
+ ++stackIndex;
38
+ }
39
+ if (canNotTraverseDeeper(object)) return {};
40
+ return {
41
+ obj: object,
42
+ k: cleanKey(stack[stackIndex])
43
+ };
44
+ };
45
+ const setPath = (object, path, newValue) => {
46
+ const {
47
+ obj,
48
+ k
49
+ } = getLastOfPath(object, path, Object);
50
+ if (obj !== undefined || path.length === 1) {
51
+ obj[k] = newValue;
52
+ return;
53
+ }
54
+ let e = path[path.length - 1];
55
+ let p = path.slice(0, path.length - 1);
56
+ let last = getLastOfPath(object, p, Object);
57
+ while (last.obj === undefined && p.length) {
58
+ e = `${p[p.length - 1]}.${e}`;
59
+ p = p.slice(0, p.length - 1);
60
+ last = getLastOfPath(object, p, Object);
61
+ if (last && last.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
62
+ last.obj = undefined;
63
+ }
64
+ }
65
+ last.obj[`${last.k}.${e}`] = newValue;
66
+ };
67
+ const pushPath = (object, path, newValue, concat) => {
68
+ const {
69
+ obj,
70
+ k
71
+ } = getLastOfPath(object, path, Object);
72
+ obj[k] = obj[k] || [];
73
+ obj[k].push(newValue);
74
+ };
75
+ const getPath = (object, path) => {
76
+ const {
77
+ obj,
78
+ k
79
+ } = getLastOfPath(object, path);
80
+ if (!obj) return undefined;
81
+ return obj[k];
82
+ };
83
+ const getPathWithDefaults = (data, defaultData, key) => {
84
+ const value = getPath(data, key);
85
+ if (value !== undefined) {
86
+ return value;
87
+ }
88
+ return getPath(defaultData, key);
89
+ };
90
+ const deepExtend = (target, source, overwrite) => {
91
+ for (const prop in source) {
92
+ if (prop !== '__proto__' && prop !== 'constructor') {
93
+ if (prop in target) {
94
+ if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
95
+ if (overwrite) target[prop] = source[prop];
96
+ } else {
97
+ deepExtend(target[prop], source[prop], overwrite);
98
+ }
99
+ } else {
100
+ target[prop] = source[prop];
101
+ }
102
+ }
103
+ }
104
+ return target;
105
+ };
106
+ const regexEscape = str => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
107
+ var _entityMap = {
108
+ '&': '&amp;',
109
+ '<': '&lt;',
110
+ '>': '&gt;',
111
+ '"': '&quot;',
112
+ "'": '&#39;',
113
+ '/': '&#x2F;'
114
+ };
115
+ const escape = data => {
116
+ if (isString(data)) {
117
+ return data.replace(/[&<>"'\/]/g, s => _entityMap[s]);
118
+ }
119
+ return data;
120
+ };
121
+ class RegExpCache {
122
+ constructor(capacity) {
123
+ this.capacity = capacity;
124
+ this.regExpMap = new Map();
125
+ this.regExpQueue = [];
126
+ }
127
+ getRegExp(pattern) {
128
+ const regExpFromCache = this.regExpMap.get(pattern);
129
+ if (regExpFromCache !== undefined) {
130
+ return regExpFromCache;
131
+ }
132
+ const regExpNew = new RegExp(pattern);
133
+ if (this.regExpQueue.length === this.capacity) {
134
+ this.regExpMap.delete(this.regExpQueue.shift());
135
+ }
136
+ this.regExpMap.set(pattern, regExpNew);
137
+ this.regExpQueue.push(pattern);
138
+ return regExpNew;
139
+ }
140
+ }
141
+ const chars = [' ', ',', '?', '!', ';'];
142
+ const looksLikeObjectPathRegExpCache = new RegExpCache(20);
143
+ const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
144
+ nsSeparator = nsSeparator || '';
145
+ keySeparator = keySeparator || '';
146
+ const possibleChars = chars.filter(c => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
147
+ if (possibleChars.length === 0) return true;
148
+ const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map(c => c === '?' ? '\\?' : c).join('|')})`);
149
+ let matched = !r.test(key);
150
+ if (!matched) {
151
+ const ki = key.indexOf(keySeparator);
152
+ if (ki > 0 && !r.test(key.substring(0, ki))) {
153
+ matched = true;
154
+ }
155
+ }
156
+ return matched;
157
+ };
158
+ const deepFind = function (obj, path) {
159
+ let keySeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';
160
+ if (!obj) return undefined;
161
+ if (obj[path]) return obj[path];
162
+ const tokens = path.split(keySeparator);
163
+ let current = obj;
164
+ for (let i = 0; i < tokens.length;) {
165
+ if (!current || typeof current !== 'object') {
166
+ return undefined;
167
+ }
168
+ let next;
169
+ let nextPath = '';
170
+ for (let j = i; j < tokens.length; ++j) {
171
+ if (j !== i) {
172
+ nextPath += keySeparator;
173
+ }
174
+ nextPath += tokens[j];
175
+ next = current[nextPath];
176
+ if (next !== undefined) {
177
+ if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
178
+ continue;
179
+ }
180
+ i += j - i + 1;
181
+ break;
182
+ }
183
+ }
184
+ current = next;
185
+ }
186
+ return current;
187
+ };
188
+ const getCleanedCode = code => code && code.replace('_', '-');
189
+
190
+ const consoleLogger = {
191
+ type: 'logger',
192
+ log(args) {
193
+ this.output('log', args);
194
+ },
195
+ warn(args) {
196
+ this.output('warn', args);
197
+ },
198
+ error(args) {
199
+ this.output('error', args);
200
+ },
201
+ output(type, args) {
202
+ if (console && console[type]) console[type].apply(console, args);
203
+ }
204
+ };
205
+ class Logger {
206
+ constructor(concreteLogger) {
207
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
208
+ this.init(concreteLogger, options);
209
+ }
210
+ init(concreteLogger) {
211
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
212
+ this.prefix = options.prefix || 'i18next:';
213
+ this.logger = concreteLogger || consoleLogger;
214
+ this.options = options;
215
+ this.debug = options.debug;
216
+ }
217
+ log() {
218
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
219
+ args[_key] = arguments[_key];
220
+ }
221
+ return this.forward(args, 'log', '', true);
222
+ }
223
+ warn() {
224
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
225
+ args[_key2] = arguments[_key2];
226
+ }
227
+ return this.forward(args, 'warn', '', true);
228
+ }
229
+ error() {
230
+ for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
231
+ args[_key3] = arguments[_key3];
232
+ }
233
+ return this.forward(args, 'error', '');
234
+ }
235
+ deprecate() {
236
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
237
+ args[_key4] = arguments[_key4];
238
+ }
239
+ return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
240
+ }
241
+ forward(args, lvl, prefix, debugOnly) {
242
+ if (debugOnly && !this.debug) return null;
243
+ if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
244
+ return this.logger[lvl](args);
245
+ }
246
+ create(moduleName) {
247
+ return new Logger(this.logger, {
248
+ ...{
249
+ prefix: `${this.prefix}:${moduleName}:`
250
+ },
251
+ ...this.options
252
+ });
253
+ }
254
+ clone(options) {
255
+ options = options || this.options;
256
+ options.prefix = options.prefix || this.prefix;
257
+ return new Logger(this.logger, options);
258
+ }
259
+ }
260
+ var baseLogger = new Logger();
261
+
262
+ class EventEmitter {
263
+ constructor() {
264
+ this.observers = {};
265
+ }
266
+ on(events, listener) {
267
+ events.split(' ').forEach(event => {
268
+ if (!this.observers[event]) this.observers[event] = new Map();
269
+ const numListeners = this.observers[event].get(listener) || 0;
270
+ this.observers[event].set(listener, numListeners + 1);
271
+ });
272
+ return this;
273
+ }
274
+ off(event, listener) {
275
+ if (!this.observers[event]) return;
276
+ if (!listener) {
277
+ delete this.observers[event];
278
+ return;
279
+ }
280
+ this.observers[event].delete(listener);
281
+ }
282
+ emit(event) {
283
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
284
+ args[_key - 1] = arguments[_key];
285
+ }
286
+ if (this.observers[event]) {
287
+ const cloned = Array.from(this.observers[event].entries());
288
+ cloned.forEach(_ref => {
289
+ let [observer, numTimesAdded] = _ref;
290
+ for (let i = 0; i < numTimesAdded; i++) {
291
+ observer(...args);
292
+ }
293
+ });
294
+ }
295
+ if (this.observers['*']) {
296
+ const cloned = Array.from(this.observers['*'].entries());
297
+ cloned.forEach(_ref2 => {
298
+ let [observer, numTimesAdded] = _ref2;
299
+ for (let i = 0; i < numTimesAdded; i++) {
300
+ observer.apply(observer, [event, ...args]);
301
+ }
302
+ });
303
+ }
304
+ }
305
+ }
306
+
307
+ class ResourceStore extends EventEmitter {
308
+ constructor(data) {
309
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
310
+ ns: ['translation'],
311
+ defaultNS: 'translation'
312
+ };
313
+ super();
314
+ this.data = data || {};
315
+ this.options = options;
316
+ if (this.options.keySeparator === undefined) {
317
+ this.options.keySeparator = '.';
318
+ }
319
+ if (this.options.ignoreJSONStructure === undefined) {
320
+ this.options.ignoreJSONStructure = true;
321
+ }
322
+ }
323
+ addNamespaces(ns) {
324
+ if (this.options.ns.indexOf(ns) < 0) {
325
+ this.options.ns.push(ns);
326
+ }
327
+ }
328
+ removeNamespaces(ns) {
329
+ const index = this.options.ns.indexOf(ns);
330
+ if (index > -1) {
331
+ this.options.ns.splice(index, 1);
332
+ }
333
+ }
334
+ getResource(lng, ns, key) {
335
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
336
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
337
+ const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
338
+ let path;
339
+ if (lng.indexOf('.') > -1) {
340
+ path = lng.split('.');
341
+ } else {
342
+ path = [lng, ns];
343
+ if (key) {
344
+ if (Array.isArray(key)) {
345
+ path.push(...key);
346
+ } else if (isString(key) && keySeparator) {
347
+ path.push(...key.split(keySeparator));
348
+ } else {
349
+ path.push(key);
350
+ }
351
+ }
352
+ }
353
+ const result = getPath(this.data, path);
354
+ if (!result && !ns && !key && lng.indexOf('.') > -1) {
355
+ lng = path[0];
356
+ ns = path[1];
357
+ key = path.slice(2).join('.');
358
+ }
359
+ if (result || !ignoreJSONStructure || !isString(key)) return result;
360
+ return deepFind(this.data && this.data[lng] && this.data[lng][ns], key, keySeparator);
361
+ }
362
+ addResource(lng, ns, key, value) {
363
+ let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
364
+ silent: false
365
+ };
366
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
367
+ let path = [lng, ns];
368
+ if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
369
+ if (lng.indexOf('.') > -1) {
370
+ path = lng.split('.');
371
+ value = ns;
372
+ ns = path[1];
373
+ }
374
+ this.addNamespaces(ns);
375
+ setPath(this.data, path, value);
376
+ if (!options.silent) this.emit('added', lng, ns, key, value);
377
+ }
378
+ addResources(lng, ns, resources) {
379
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
380
+ silent: false
381
+ };
382
+ for (const m in resources) {
383
+ if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
384
+ silent: true
385
+ });
386
+ }
387
+ if (!options.silent) this.emit('added', lng, ns, resources);
388
+ }
389
+ addResourceBundle(lng, ns, resources, deep, overwrite) {
390
+ let options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {
391
+ silent: false,
392
+ skipCopy: false
393
+ };
394
+ let path = [lng, ns];
395
+ if (lng.indexOf('.') > -1) {
396
+ path = lng.split('.');
397
+ deep = resources;
398
+ resources = ns;
399
+ ns = path[1];
400
+ }
401
+ this.addNamespaces(ns);
402
+ let pack = getPath(this.data, path) || {};
403
+ if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
404
+ if (deep) {
405
+ deepExtend(pack, resources, overwrite);
406
+ } else {
407
+ pack = {
408
+ ...pack,
409
+ ...resources
410
+ };
411
+ }
412
+ setPath(this.data, path, pack);
413
+ if (!options.silent) this.emit('added', lng, ns, resources);
414
+ }
415
+ removeResourceBundle(lng, ns) {
416
+ if (this.hasResourceBundle(lng, ns)) {
417
+ delete this.data[lng][ns];
418
+ }
419
+ this.removeNamespaces(ns);
420
+ this.emit('removed', lng, ns);
421
+ }
422
+ hasResourceBundle(lng, ns) {
423
+ return this.getResource(lng, ns) !== undefined;
424
+ }
425
+ getResourceBundle(lng, ns) {
426
+ if (!ns) ns = this.options.defaultNS;
427
+ if (this.options.compatibilityAPI === 'v1') return {
428
+ ...{},
429
+ ...this.getResource(lng, ns)
430
+ };
431
+ return this.getResource(lng, ns);
432
+ }
433
+ getDataByLanguage(lng) {
434
+ return this.data[lng];
435
+ }
436
+ hasLanguageSomeTranslations(lng) {
437
+ const data = this.getDataByLanguage(lng);
438
+ const n = data && Object.keys(data) || [];
439
+ return !!n.find(v => data[v] && Object.keys(data[v]).length > 0);
440
+ }
441
+ toJSON() {
442
+ return this.data;
443
+ }
444
+ }
445
+
446
+ var postProcessor = {
447
+ processors: {},
448
+ addPostProcessor(module) {
449
+ this.processors[module.name] = module;
450
+ },
451
+ handle(processors, value, key, options, translator) {
452
+ processors.forEach(processor => {
453
+ if (this.processors[processor]) value = this.processors[processor].process(value, key, options, translator);
454
+ });
455
+ return value;
456
+ }
457
+ };
458
+
459
+ const checkedLoadedFor = {};
460
+ class Translator extends EventEmitter {
461
+ constructor(services) {
462
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
463
+ super();
464
+ copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
465
+ this.options = options;
466
+ if (this.options.keySeparator === undefined) {
467
+ this.options.keySeparator = '.';
468
+ }
469
+ this.logger = baseLogger.create('translator');
470
+ }
471
+ changeLanguage(lng) {
472
+ if (lng) this.language = lng;
473
+ }
474
+ exists(key) {
475
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
476
+ interpolation: {}
477
+ };
478
+ if (key === undefined || key === null) {
479
+ return false;
480
+ }
481
+ const resolved = this.resolve(key, options);
482
+ return resolved && resolved.res !== undefined;
483
+ }
484
+ extractFromKey(key, options) {
485
+ let nsSeparator = options.nsSeparator !== undefined ? options.nsSeparator : this.options.nsSeparator;
486
+ if (nsSeparator === undefined) nsSeparator = ':';
487
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
488
+ let namespaces = options.ns || this.options.defaultNS || [];
489
+ const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
490
+ const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !options.keySeparator && !this.options.userDefinedNsSeparator && !options.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
491
+ if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
492
+ const m = key.match(this.interpolator.nestingRegexp);
493
+ if (m && m.length > 0) {
494
+ return {
495
+ key,
496
+ namespaces
497
+ };
498
+ }
499
+ const parts = key.split(nsSeparator);
500
+ if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
501
+ key = parts.join(keySeparator);
502
+ }
503
+ if (isString(namespaces)) namespaces = [namespaces];
504
+ return {
505
+ key,
506
+ namespaces
507
+ };
508
+ }
509
+ translate(keys, options, lastKey) {
510
+ if (typeof options !== 'object' && this.options.overloadTranslationOptionHandler) {
511
+ options = this.options.overloadTranslationOptionHandler(arguments);
512
+ }
513
+ if (typeof options === 'object') options = {
514
+ ...options
515
+ };
516
+ if (!options) options = {};
517
+ if (keys === undefined || keys === null) return '';
518
+ if (!Array.isArray(keys)) keys = [String(keys)];
519
+ const returnDetails = options.returnDetails !== undefined ? options.returnDetails : this.options.returnDetails;
520
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
521
+ const {
522
+ key,
523
+ namespaces
524
+ } = this.extractFromKey(keys[keys.length - 1], options);
525
+ const namespace = namespaces[namespaces.length - 1];
526
+ const lng = options.lng || this.language;
527
+ const appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
528
+ if (lng && lng.toLowerCase() === 'cimode') {
529
+ if (appendNamespaceToCIMode) {
530
+ const nsSeparator = options.nsSeparator || this.options.nsSeparator;
531
+ if (returnDetails) {
532
+ return {
533
+ res: `${namespace}${nsSeparator}${key}`,
534
+ usedKey: key,
535
+ exactUsedKey: key,
536
+ usedLng: lng,
537
+ usedNS: namespace,
538
+ usedParams: this.getUsedParamsDetails(options)
539
+ };
540
+ }
541
+ return `${namespace}${nsSeparator}${key}`;
542
+ }
543
+ if (returnDetails) {
544
+ return {
545
+ res: key,
546
+ usedKey: key,
547
+ exactUsedKey: key,
548
+ usedLng: lng,
549
+ usedNS: namespace,
550
+ usedParams: this.getUsedParamsDetails(options)
551
+ };
552
+ }
553
+ return key;
554
+ }
555
+ const resolved = this.resolve(keys, options);
556
+ let res = resolved && resolved.res;
557
+ const resUsedKey = resolved && resolved.usedKey || key;
558
+ const resExactUsedKey = resolved && resolved.exactUsedKey || key;
559
+ const resType = Object.prototype.toString.apply(res);
560
+ const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
561
+ const joinArrays = options.joinArrays !== undefined ? options.joinArrays : this.options.joinArrays;
562
+ const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
563
+ const handleAsObject = !isString(res) && typeof res !== 'boolean' && typeof res !== 'number';
564
+ if (handleAsObjectInI18nFormat && res && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(res))) {
565
+ if (!options.returnObjects && !this.options.returnObjects) {
566
+ if (!this.options.returnedObjectHandler) {
567
+ this.logger.warn('accessing an object - but returnObjects options is not enabled!');
568
+ }
569
+ const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, res, {
570
+ ...options,
571
+ ns: namespaces
572
+ }) : `key '${key} (${this.language})' returned an object instead of string.`;
573
+ if (returnDetails) {
574
+ resolved.res = r;
575
+ resolved.usedParams = this.getUsedParamsDetails(options);
576
+ return resolved;
577
+ }
578
+ return r;
579
+ }
580
+ if (keySeparator) {
581
+ const resTypeIsArray = Array.isArray(res);
582
+ const copy = resTypeIsArray ? [] : {};
583
+ const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
584
+ for (const m in res) {
585
+ if (Object.prototype.hasOwnProperty.call(res, m)) {
586
+ const deepKey = `${newKeyToUse}${keySeparator}${m}`;
587
+ copy[m] = this.translate(deepKey, {
588
+ ...options,
589
+ ...{
590
+ joinArrays: false,
591
+ ns: namespaces
592
+ }
593
+ });
594
+ if (copy[m] === deepKey) copy[m] = res[m];
595
+ }
596
+ }
597
+ res = copy;
598
+ }
599
+ } else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
600
+ res = res.join(joinArrays);
601
+ if (res) res = this.extendTranslation(res, keys, options, lastKey);
602
+ } else {
603
+ let usedDefault = false;
604
+ let usedKey = false;
605
+ const needsPluralHandling = options.count !== undefined && !isString(options.count);
606
+ const hasDefaultValue = Translator.hasDefaultValue(options);
607
+ const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, options.count, options) : '';
608
+ const defaultValueSuffixOrdinalFallback = options.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, options.count, {
609
+ ordinal: false
610
+ }) : '';
611
+ const needsZeroSuffixLookup = needsPluralHandling && !options.ordinal && options.count === 0 && this.pluralResolver.shouldUseIntlApi();
612
+ const defaultValue = needsZeroSuffixLookup && options[`defaultValue${this.options.pluralSeparator}zero`] || options[`defaultValue${defaultValueSuffix}`] || options[`defaultValue${defaultValueSuffixOrdinalFallback}`] || options.defaultValue;
613
+ if (!this.isValidLookup(res) && hasDefaultValue) {
614
+ usedDefault = true;
615
+ res = defaultValue;
616
+ }
617
+ if (!this.isValidLookup(res)) {
618
+ usedKey = true;
619
+ res = key;
620
+ }
621
+ const missingKeyNoValueFallbackToKey = options.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
622
+ const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
623
+ const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
624
+ if (usedKey || usedDefault || updateMissing) {
625
+ this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
626
+ if (keySeparator) {
627
+ const fk = this.resolve(key, {
628
+ ...options,
629
+ keySeparator: false
630
+ });
631
+ if (fk && fk.res) this.logger.warn('Seems the loaded translations were in flat JSON format instead of nested. Either set keySeparator: false on init or make sure your translations are published in nested format.');
632
+ }
633
+ let lngs = [];
634
+ const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, options.lng || this.language);
635
+ if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
636
+ for (let i = 0; i < fallbackLngs.length; i++) {
637
+ lngs.push(fallbackLngs[i]);
638
+ }
639
+ } else if (this.options.saveMissingTo === 'all') {
640
+ lngs = this.languageUtils.toResolveHierarchy(options.lng || this.language);
641
+ } else {
642
+ lngs.push(options.lng || this.language);
643
+ }
644
+ const send = (l, k, specificDefaultValue) => {
645
+ const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
646
+ if (this.options.missingKeyHandler) {
647
+ this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, options);
648
+ } else if (this.backendConnector && this.backendConnector.saveMissing) {
649
+ this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, options);
650
+ }
651
+ this.emit('missingKey', l, namespace, k, res);
652
+ };
653
+ if (this.options.saveMissing) {
654
+ if (this.options.saveMissingPlurals && needsPluralHandling) {
655
+ lngs.forEach(language => {
656
+ const suffixes = this.pluralResolver.getSuffixes(language, options);
657
+ if (needsZeroSuffixLookup && options[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
658
+ suffixes.push(`${this.options.pluralSeparator}zero`);
659
+ }
660
+ suffixes.forEach(suffix => {
661
+ send([language], key + suffix, options[`defaultValue${suffix}`] || defaultValue);
662
+ });
663
+ });
664
+ } else {
665
+ send(lngs, key, defaultValue);
666
+ }
667
+ }
668
+ }
669
+ res = this.extendTranslation(res, keys, options, resolved, lastKey);
670
+ if (usedKey && res === key && this.options.appendNamespaceToMissingKey) res = `${namespace}:${key}`;
671
+ if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
672
+ if (this.options.compatibilityAPI !== 'v1') {
673
+ res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}:${key}` : key, usedDefault ? res : undefined);
674
+ } else {
675
+ res = this.options.parseMissingKeyHandler(res);
676
+ }
677
+ }
678
+ }
679
+ if (returnDetails) {
680
+ resolved.res = res;
681
+ resolved.usedParams = this.getUsedParamsDetails(options);
682
+ return resolved;
683
+ }
684
+ return res;
685
+ }
686
+ extendTranslation(res, key, options, resolved, lastKey) {
687
+ var _this = this;
688
+ if (this.i18nFormat && this.i18nFormat.parse) {
689
+ res = this.i18nFormat.parse(res, {
690
+ ...this.options.interpolation.defaultVariables,
691
+ ...options
692
+ }, options.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
693
+ resolved
694
+ });
695
+ } else if (!options.skipInterpolation) {
696
+ if (options.interpolation) this.interpolator.init({
697
+ ...options,
698
+ ...{
699
+ interpolation: {
700
+ ...this.options.interpolation,
701
+ ...options.interpolation
702
+ }
703
+ }
704
+ });
705
+ const skipOnVariables = isString(res) && (options && options.interpolation && options.interpolation.skipOnVariables !== undefined ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
706
+ let nestBef;
707
+ if (skipOnVariables) {
708
+ const nb = res.match(this.interpolator.nestingRegexp);
709
+ nestBef = nb && nb.length;
710
+ }
711
+ let data = options.replace && !isString(options.replace) ? options.replace : options;
712
+ if (this.options.interpolation.defaultVariables) data = {
713
+ ...this.options.interpolation.defaultVariables,
714
+ ...data
715
+ };
716
+ res = this.interpolator.interpolate(res, data, options.lng || this.language || resolved.usedLng, options);
717
+ if (skipOnVariables) {
718
+ const na = res.match(this.interpolator.nestingRegexp);
719
+ const nestAft = na && na.length;
720
+ if (nestBef < nestAft) options.nest = false;
721
+ }
722
+ if (!options.lng && this.options.compatibilityAPI !== 'v1' && resolved && resolved.res) options.lng = this.language || resolved.usedLng;
723
+ if (options.nest !== false) res = this.interpolator.nest(res, function () {
724
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
725
+ args[_key] = arguments[_key];
726
+ }
727
+ if (lastKey && lastKey[0] === args[0] && !options.context) {
728
+ _this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
729
+ return null;
730
+ }
731
+ return _this.translate(...args, key);
732
+ }, options);
733
+ if (options.interpolation) this.interpolator.reset();
734
+ }
735
+ const postProcess = options.postProcess || this.options.postProcess;
736
+ const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
737
+ if (res !== undefined && res !== null && postProcessorNames && postProcessorNames.length && options.applyPostProcessor !== false) {
738
+ res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
739
+ i18nResolved: {
740
+ ...resolved,
741
+ usedParams: this.getUsedParamsDetails(options)
742
+ },
743
+ ...options
744
+ } : options, this);
745
+ }
746
+ return res;
747
+ }
748
+ resolve(keys) {
749
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
750
+ let found;
751
+ let usedKey;
752
+ let exactUsedKey;
753
+ let usedLng;
754
+ let usedNS;
755
+ if (isString(keys)) keys = [keys];
756
+ keys.forEach(k => {
757
+ if (this.isValidLookup(found)) return;
758
+ const extracted = this.extractFromKey(k, options);
759
+ const key = extracted.key;
760
+ usedKey = key;
761
+ let namespaces = extracted.namespaces;
762
+ if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
763
+ const needsPluralHandling = options.count !== undefined && !isString(options.count);
764
+ const needsZeroSuffixLookup = needsPluralHandling && !options.ordinal && options.count === 0 && this.pluralResolver.shouldUseIntlApi();
765
+ const needsContextHandling = options.context !== undefined && (isString(options.context) || typeof options.context === 'number') && options.context !== '';
766
+ const codes = options.lngs ? options.lngs : this.languageUtils.toResolveHierarchy(options.lng || this.language, options.fallbackLng);
767
+ namespaces.forEach(ns => {
768
+ if (this.isValidLookup(found)) return;
769
+ usedNS = ns;
770
+ if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils && this.utils.hasLoadedNamespace && !this.utils.hasLoadedNamespace(usedNS)) {
771
+ checkedLoadedFor[`${codes[0]}-${ns}`] = true;
772
+ this.logger.warn(`key "${usedKey}" for languages "${codes.join(', ')}" won't get resolved as namespace "${usedNS}" was not yet loaded`, 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
773
+ }
774
+ codes.forEach(code => {
775
+ if (this.isValidLookup(found)) return;
776
+ usedLng = code;
777
+ const finalKeys = [key];
778
+ if (this.i18nFormat && this.i18nFormat.addLookupKeys) {
779
+ this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, options);
780
+ } else {
781
+ let pluralSuffix;
782
+ if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, options.count, options);
783
+ const zeroSuffix = `${this.options.pluralSeparator}zero`;
784
+ const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
785
+ if (needsPluralHandling) {
786
+ finalKeys.push(key + pluralSuffix);
787
+ if (options.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
788
+ finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
789
+ }
790
+ if (needsZeroSuffixLookup) {
791
+ finalKeys.push(key + zeroSuffix);
792
+ }
793
+ }
794
+ if (needsContextHandling) {
795
+ const contextKey = `${key}${this.options.contextSeparator}${options.context}`;
796
+ finalKeys.push(contextKey);
797
+ if (needsPluralHandling) {
798
+ finalKeys.push(contextKey + pluralSuffix);
799
+ if (options.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
800
+ finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
801
+ }
802
+ if (needsZeroSuffixLookup) {
803
+ finalKeys.push(contextKey + zeroSuffix);
804
+ }
805
+ }
806
+ }
807
+ }
808
+ let possibleKey;
809
+ while (possibleKey = finalKeys.pop()) {
810
+ if (!this.isValidLookup(found)) {
811
+ exactUsedKey = possibleKey;
812
+ found = this.getResource(code, ns, possibleKey, options);
813
+ }
814
+ }
815
+ });
816
+ });
817
+ });
818
+ return {
819
+ res: found,
820
+ usedKey,
821
+ exactUsedKey,
822
+ usedLng,
823
+ usedNS
824
+ };
825
+ }
826
+ isValidLookup(res) {
827
+ return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
828
+ }
829
+ getResource(code, ns, key) {
830
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
831
+ if (this.i18nFormat && this.i18nFormat.getResource) return this.i18nFormat.getResource(code, ns, key, options);
832
+ return this.resourceStore.getResource(code, ns, key, options);
833
+ }
834
+ getUsedParamsDetails() {
835
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
836
+ const optionsKeys = ['defaultValue', 'ordinal', 'context', 'replace', 'lng', 'lngs', 'fallbackLng', 'ns', 'keySeparator', 'nsSeparator', 'returnObjects', 'returnDetails', 'joinArrays', 'postProcess', 'interpolation'];
837
+ const useOptionsReplaceForData = options.replace && !isString(options.replace);
838
+ let data = useOptionsReplaceForData ? options.replace : options;
839
+ if (useOptionsReplaceForData && typeof options.count !== 'undefined') {
840
+ data.count = options.count;
841
+ }
842
+ if (this.options.interpolation.defaultVariables) {
843
+ data = {
844
+ ...this.options.interpolation.defaultVariables,
845
+ ...data
846
+ };
847
+ }
848
+ if (!useOptionsReplaceForData) {
849
+ data = {
850
+ ...data
851
+ };
852
+ for (const key of optionsKeys) {
853
+ delete data[key];
854
+ }
855
+ }
856
+ return data;
857
+ }
858
+ static hasDefaultValue(options) {
859
+ const prefix = 'defaultValue';
860
+ for (const option in options) {
861
+ if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && undefined !== options[option]) {
862
+ return true;
863
+ }
864
+ }
865
+ return false;
866
+ }
867
+ }
868
+
869
+ const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1);
870
+ class LanguageUtil {
871
+ constructor(options) {
872
+ this.options = options;
873
+ this.supportedLngs = this.options.supportedLngs || false;
874
+ this.logger = baseLogger.create('languageUtils');
875
+ }
876
+ getScriptPartFromCode(code) {
877
+ code = getCleanedCode(code);
878
+ if (!code || code.indexOf('-') < 0) return null;
879
+ const p = code.split('-');
880
+ if (p.length === 2) return null;
881
+ p.pop();
882
+ if (p[p.length - 1].toLowerCase() === 'x') return null;
883
+ return this.formatLanguageCode(p.join('-'));
884
+ }
885
+ getLanguagePartFromCode(code) {
886
+ code = getCleanedCode(code);
887
+ if (!code || code.indexOf('-') < 0) return code;
888
+ const p = code.split('-');
889
+ return this.formatLanguageCode(p[0]);
890
+ }
891
+ formatLanguageCode(code) {
892
+ if (isString(code) && code.indexOf('-') > -1) {
893
+ if (typeof Intl !== 'undefined' && typeof Intl.getCanonicalLocales !== 'undefined') {
894
+ try {
895
+ let formattedCode = Intl.getCanonicalLocales(code)[0];
896
+ if (formattedCode && this.options.lowerCaseLng) {
897
+ formattedCode = formattedCode.toLowerCase();
898
+ }
899
+ if (formattedCode) return formattedCode;
900
+ } catch (e) {}
901
+ }
902
+ const specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];
903
+ let p = code.split('-');
904
+ if (this.options.lowerCaseLng) {
905
+ p = p.map(part => part.toLowerCase());
906
+ } else if (p.length === 2) {
907
+ p[0] = p[0].toLowerCase();
908
+ p[1] = p[1].toUpperCase();
909
+ if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
910
+ } else if (p.length === 3) {
911
+ p[0] = p[0].toLowerCase();
912
+ if (p[1].length === 2) p[1] = p[1].toUpperCase();
913
+ if (p[0] !== 'sgn' && p[2].length === 2) p[2] = p[2].toUpperCase();
914
+ if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
915
+ if (specialCases.indexOf(p[2].toLowerCase()) > -1) p[2] = capitalize(p[2].toLowerCase());
916
+ }
917
+ return p.join('-');
918
+ }
919
+ return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
920
+ }
921
+ isSupportedCode(code) {
922
+ if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
923
+ code = this.getLanguagePartFromCode(code);
924
+ }
925
+ return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
926
+ }
927
+ getBestMatchFromCodes(codes) {
928
+ if (!codes) return null;
929
+ let found;
930
+ codes.forEach(code => {
931
+ if (found) return;
932
+ const cleanedLng = this.formatLanguageCode(code);
933
+ if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
934
+ });
935
+ if (!found && this.options.supportedLngs) {
936
+ codes.forEach(code => {
937
+ if (found) return;
938
+ const lngOnly = this.getLanguagePartFromCode(code);
939
+ if (this.isSupportedCode(lngOnly)) return found = lngOnly;
940
+ found = this.options.supportedLngs.find(supportedLng => {
941
+ if (supportedLng === lngOnly) return supportedLng;
942
+ if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
943
+ if (supportedLng.indexOf('-') > 0 && lngOnly.indexOf('-') < 0 && supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly) return supportedLng;
944
+ if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
945
+ });
946
+ });
947
+ }
948
+ if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
949
+ return found;
950
+ }
951
+ getFallbackCodes(fallbacks, code) {
952
+ if (!fallbacks) return [];
953
+ if (typeof fallbacks === 'function') fallbacks = fallbacks(code);
954
+ if (isString(fallbacks)) fallbacks = [fallbacks];
955
+ if (Array.isArray(fallbacks)) return fallbacks;
956
+ if (!code) return fallbacks.default || [];
957
+ let found = fallbacks[code];
958
+ if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
959
+ if (!found) found = fallbacks[this.formatLanguageCode(code)];
960
+ if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
961
+ if (!found) found = fallbacks.default;
962
+ return found || [];
963
+ }
964
+ toResolveHierarchy(code, fallbackCode) {
965
+ const fallbackCodes = this.getFallbackCodes(fallbackCode || this.options.fallbackLng || [], code);
966
+ const codes = [];
967
+ const addCode = c => {
968
+ if (!c) return;
969
+ if (this.isSupportedCode(c)) {
970
+ codes.push(c);
971
+ } else {
972
+ this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
973
+ }
974
+ };
975
+ if (isString(code) && (code.indexOf('-') > -1 || code.indexOf('_') > -1)) {
976
+ if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
977
+ if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
978
+ if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
979
+ } else if (isString(code)) {
980
+ addCode(this.formatLanguageCode(code));
981
+ }
982
+ fallbackCodes.forEach(fc => {
983
+ if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
984
+ });
985
+ return codes;
986
+ }
987
+ }
988
+
989
+ let sets = [{
990
+ lngs: ['ach', 'ak', 'am', 'arn', 'br', 'fil', 'gun', 'ln', 'mfe', 'mg', 'mi', 'oc', 'pt', 'pt-BR', 'tg', 'tl', 'ti', 'tr', 'uz', 'wa'],
991
+ nr: [1, 2],
992
+ fc: 1
993
+ }, {
994
+ lngs: ['af', 'an', 'ast', 'az', 'bg', 'bn', 'ca', 'da', 'de', 'dev', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fi', 'fo', 'fur', 'fy', 'gl', 'gu', 'ha', 'hi', 'hu', 'hy', 'ia', 'it', 'kk', 'kn', 'ku', 'lb', 'mai', 'ml', 'mn', 'mr', 'nah', 'nap', 'nb', 'ne', 'nl', 'nn', 'no', 'nso', 'pa', 'pap', 'pms', 'ps', 'pt-PT', 'rm', 'sco', 'se', 'si', 'so', 'son', 'sq', 'sv', 'sw', 'ta', 'te', 'tk', 'ur', 'yo'],
995
+ nr: [1, 2],
996
+ fc: 2
997
+ }, {
998
+ lngs: ['ay', 'bo', 'cgg', 'fa', 'ht', 'id', 'ja', 'jbo', 'ka', 'km', 'ko', 'ky', 'lo', 'ms', 'sah', 'su', 'th', 'tt', 'ug', 'vi', 'wo', 'zh'],
999
+ nr: [1],
1000
+ fc: 3
1001
+ }, {
1002
+ lngs: ['be', 'bs', 'cnr', 'dz', 'hr', 'ru', 'sr', 'uk'],
1003
+ nr: [1, 2, 5],
1004
+ fc: 4
1005
+ }, {
1006
+ lngs: ['ar'],
1007
+ nr: [0, 1, 2, 3, 11, 100],
1008
+ fc: 5
1009
+ }, {
1010
+ lngs: ['cs', 'sk'],
1011
+ nr: [1, 2, 5],
1012
+ fc: 6
1013
+ }, {
1014
+ lngs: ['csb', 'pl'],
1015
+ nr: [1, 2, 5],
1016
+ fc: 7
1017
+ }, {
1018
+ lngs: ['cy'],
1019
+ nr: [1, 2, 3, 8],
1020
+ fc: 8
1021
+ }, {
1022
+ lngs: ['fr'],
1023
+ nr: [1, 2],
1024
+ fc: 9
1025
+ }, {
1026
+ lngs: ['ga'],
1027
+ nr: [1, 2, 3, 7, 11],
1028
+ fc: 10
1029
+ }, {
1030
+ lngs: ['gd'],
1031
+ nr: [1, 2, 3, 20],
1032
+ fc: 11
1033
+ }, {
1034
+ lngs: ['is'],
1035
+ nr: [1, 2],
1036
+ fc: 12
1037
+ }, {
1038
+ lngs: ['jv'],
1039
+ nr: [0, 1],
1040
+ fc: 13
1041
+ }, {
1042
+ lngs: ['kw'],
1043
+ nr: [1, 2, 3, 4],
1044
+ fc: 14
1045
+ }, {
1046
+ lngs: ['lt'],
1047
+ nr: [1, 2, 10],
1048
+ fc: 15
1049
+ }, {
1050
+ lngs: ['lv'],
1051
+ nr: [1, 2, 0],
1052
+ fc: 16
1053
+ }, {
1054
+ lngs: ['mk'],
1055
+ nr: [1, 2],
1056
+ fc: 17
1057
+ }, {
1058
+ lngs: ['mnk'],
1059
+ nr: [0, 1, 2],
1060
+ fc: 18
1061
+ }, {
1062
+ lngs: ['mt'],
1063
+ nr: [1, 2, 11, 20],
1064
+ fc: 19
1065
+ }, {
1066
+ lngs: ['or'],
1067
+ nr: [2, 1],
1068
+ fc: 2
1069
+ }, {
1070
+ lngs: ['ro'],
1071
+ nr: [1, 2, 20],
1072
+ fc: 20
1073
+ }, {
1074
+ lngs: ['sl'],
1075
+ nr: [5, 1, 2, 3],
1076
+ fc: 21
1077
+ }, {
1078
+ lngs: ['he', 'iw'],
1079
+ nr: [1, 2, 20, 21],
1080
+ fc: 22
1081
+ }];
1082
+ let _rulesPluralsTypes = {
1083
+ 1: n => Number(n > 1),
1084
+ 2: n => Number(n != 1),
1085
+ 3: n => 0,
1086
+ 4: n => Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2),
1087
+ 5: n => Number(n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5),
1088
+ 6: n => Number(n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2),
1089
+ 7: n => Number(n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2),
1090
+ 8: n => Number(n == 1 ? 0 : n == 2 ? 1 : n != 8 && n != 11 ? 2 : 3),
1091
+ 9: n => Number(n >= 2),
1092
+ 10: n => Number(n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4),
1093
+ 11: n => Number(n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 2 && n < 20 ? 2 : 3),
1094
+ 12: n => Number(n % 10 != 1 || n % 100 == 11),
1095
+ 13: n => Number(n !== 0),
1096
+ 14: n => Number(n == 1 ? 0 : n == 2 ? 1 : n == 3 ? 2 : 3),
1097
+ 15: n => Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2),
1098
+ 16: n => Number(n % 10 == 1 && n % 100 != 11 ? 0 : n !== 0 ? 1 : 2),
1099
+ 17: n => Number(n == 1 || n % 10 == 1 && n % 100 != 11 ? 0 : 1),
1100
+ 18: n => Number(n == 0 ? 0 : n == 1 ? 1 : 2),
1101
+ 19: n => Number(n == 1 ? 0 : n == 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3),
1102
+ 20: n => Number(n == 1 ? 0 : n == 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2),
1103
+ 21: n => Number(n % 100 == 1 ? 1 : n % 100 == 2 ? 2 : n % 100 == 3 || n % 100 == 4 ? 3 : 0),
1104
+ 22: n => Number(n == 1 ? 0 : n == 2 ? 1 : (n < 0 || n > 10) && n % 10 == 0 ? 2 : 3)
1105
+ };
1106
+ const nonIntlVersions = ['v1', 'v2', 'v3'];
1107
+ const intlVersions = ['v4'];
1108
+ const suffixesOrder = {
1109
+ zero: 0,
1110
+ one: 1,
1111
+ two: 2,
1112
+ few: 3,
1113
+ many: 4,
1114
+ other: 5
1115
+ };
1116
+ const createRules = () => {
1117
+ const rules = {};
1118
+ sets.forEach(set => {
1119
+ set.lngs.forEach(l => {
1120
+ rules[l] = {
1121
+ numbers: set.nr,
1122
+ plurals: _rulesPluralsTypes[set.fc]
1123
+ };
1124
+ });
1125
+ });
1126
+ return rules;
1127
+ };
1128
+ class PluralResolver {
1129
+ constructor(languageUtils) {
1130
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1131
+ this.languageUtils = languageUtils;
1132
+ this.options = options;
1133
+ this.logger = baseLogger.create('pluralResolver');
1134
+ if ((!this.options.compatibilityJSON || intlVersions.includes(this.options.compatibilityJSON)) && (typeof Intl === 'undefined' || !Intl.PluralRules)) {
1135
+ this.options.compatibilityJSON = 'v3';
1136
+ this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
1137
+ }
1138
+ this.rules = createRules();
1139
+ this.pluralRulesCache = {};
1140
+ }
1141
+ addRule(lng, obj) {
1142
+ this.rules[lng] = obj;
1143
+ }
1144
+ clearCache() {
1145
+ this.pluralRulesCache = {};
1146
+ }
1147
+ getRule(code) {
1148
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1149
+ if (this.shouldUseIntlApi()) {
1150
+ try {
1151
+ const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
1152
+ const type = options.ordinal ? 'ordinal' : 'cardinal';
1153
+ const cacheKey = JSON.stringify({
1154
+ cleanedCode,
1155
+ type
1156
+ });
1157
+ if (cacheKey in this.pluralRulesCache) {
1158
+ return this.pluralRulesCache[cacheKey];
1159
+ }
1160
+ const rule = new Intl.PluralRules(cleanedCode, {
1161
+ type
1162
+ });
1163
+ this.pluralRulesCache[cacheKey] = rule;
1164
+ return rule;
1165
+ } catch (err) {
1166
+ return;
1167
+ }
1168
+ }
1169
+ return this.rules[code] || this.rules[this.languageUtils.getLanguagePartFromCode(code)];
1170
+ }
1171
+ needsPlural(code) {
1172
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1173
+ const rule = this.getRule(code, options);
1174
+ if (this.shouldUseIntlApi()) {
1175
+ return rule && rule.resolvedOptions().pluralCategories.length > 1;
1176
+ }
1177
+ return rule && rule.numbers.length > 1;
1178
+ }
1179
+ getPluralFormsOfKey(code, key) {
1180
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1181
+ return this.getSuffixes(code, options).map(suffix => `${key}${suffix}`);
1182
+ }
1183
+ getSuffixes(code) {
1184
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1185
+ const rule = this.getRule(code, options);
1186
+ if (!rule) {
1187
+ return [];
1188
+ }
1189
+ if (this.shouldUseIntlApi()) {
1190
+ return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
1191
+ }
1192
+ return rule.numbers.map(number => this.getSuffix(code, number, options));
1193
+ }
1194
+ getSuffix(code, count) {
1195
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1196
+ const rule = this.getRule(code, options);
1197
+ if (rule) {
1198
+ if (this.shouldUseIntlApi()) {
1199
+ return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
1200
+ }
1201
+ return this.getSuffixRetroCompatible(rule, count);
1202
+ }
1203
+ this.logger.warn(`no plural rule found for: ${code}`);
1204
+ return '';
1205
+ }
1206
+ getSuffixRetroCompatible(rule, count) {
1207
+ const idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
1208
+ let suffix = rule.numbers[idx];
1209
+ if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
1210
+ if (suffix === 2) {
1211
+ suffix = 'plural';
1212
+ } else if (suffix === 1) {
1213
+ suffix = '';
1214
+ }
1215
+ }
1216
+ const returnSuffix = () => this.options.prepend && suffix.toString() ? this.options.prepend + suffix.toString() : suffix.toString();
1217
+ if (this.options.compatibilityJSON === 'v1') {
1218
+ if (suffix === 1) return '';
1219
+ if (typeof suffix === 'number') return `_plural_${suffix.toString()}`;
1220
+ return returnSuffix();
1221
+ } else if (this.options.compatibilityJSON === 'v2') {
1222
+ return returnSuffix();
1223
+ } else if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
1224
+ return returnSuffix();
1225
+ }
1226
+ return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
1227
+ }
1228
+ shouldUseIntlApi() {
1229
+ return !nonIntlVersions.includes(this.options.compatibilityJSON);
1230
+ }
1231
+ }
1232
+
1233
+ const deepFindWithDefaults = function (data, defaultData, key) {
1234
+ let keySeparator = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '.';
1235
+ let ignoreJSONStructure = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
1236
+ let path = getPathWithDefaults(data, defaultData, key);
1237
+ if (!path && ignoreJSONStructure && isString(key)) {
1238
+ path = deepFind(data, key, keySeparator);
1239
+ if (path === undefined) path = deepFind(defaultData, key, keySeparator);
1240
+ }
1241
+ return path;
1242
+ };
1243
+ const regexSafe = val => val.replace(/\$/g, '$$$$');
1244
+ class Interpolator {
1245
+ constructor() {
1246
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1247
+ this.logger = baseLogger.create('interpolator');
1248
+ this.options = options;
1249
+ this.format = options.interpolation && options.interpolation.format || (value => value);
1250
+ this.init(options);
1251
+ }
1252
+ init() {
1253
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1254
+ if (!options.interpolation) options.interpolation = {
1255
+ escapeValue: true
1256
+ };
1257
+ const {
1258
+ escape: escape$1,
1259
+ escapeValue,
1260
+ useRawValueToEscape,
1261
+ prefix,
1262
+ prefixEscaped,
1263
+ suffix,
1264
+ suffixEscaped,
1265
+ formatSeparator,
1266
+ unescapeSuffix,
1267
+ unescapePrefix,
1268
+ nestingPrefix,
1269
+ nestingPrefixEscaped,
1270
+ nestingSuffix,
1271
+ nestingSuffixEscaped,
1272
+ nestingOptionsSeparator,
1273
+ maxReplaces,
1274
+ alwaysFormat
1275
+ } = options.interpolation;
1276
+ this.escape = escape$1 !== undefined ? escape$1 : escape;
1277
+ this.escapeValue = escapeValue !== undefined ? escapeValue : true;
1278
+ this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
1279
+ this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || '{{';
1280
+ this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || '}}';
1281
+ this.formatSeparator = formatSeparator || ',';
1282
+ this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix || '-';
1283
+ this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix || '';
1284
+ this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape('$t(');
1285
+ this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(')');
1286
+ this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
1287
+ this.maxReplaces = maxReplaces || 1000;
1288
+ this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
1289
+ this.resetRegExp();
1290
+ }
1291
+ reset() {
1292
+ if (this.options) this.init(this.options);
1293
+ }
1294
+ resetRegExp() {
1295
+ const getOrResetRegExp = (existingRegExp, pattern) => {
1296
+ if (existingRegExp && existingRegExp.source === pattern) {
1297
+ existingRegExp.lastIndex = 0;
1298
+ return existingRegExp;
1299
+ }
1300
+ return new RegExp(pattern, 'g');
1301
+ };
1302
+ this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
1303
+ this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
1304
+ this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}(.+?)${this.nestingSuffix}`);
1305
+ }
1306
+ interpolate(str, data, lng, options) {
1307
+ let match;
1308
+ let value;
1309
+ let replaces;
1310
+ const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
1311
+ const handleFormat = key => {
1312
+ if (key.indexOf(this.formatSeparator) < 0) {
1313
+ const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
1314
+ return this.alwaysFormat ? this.format(path, undefined, lng, {
1315
+ ...options,
1316
+ ...data,
1317
+ interpolationkey: key
1318
+ }) : path;
1319
+ }
1320
+ const p = key.split(this.formatSeparator);
1321
+ const k = p.shift().trim();
1322
+ const f = p.join(this.formatSeparator).trim();
1323
+ return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
1324
+ ...options,
1325
+ ...data,
1326
+ interpolationkey: k
1327
+ });
1328
+ };
1329
+ this.resetRegExp();
1330
+ const missingInterpolationHandler = options && options.missingInterpolationHandler || this.options.missingInterpolationHandler;
1331
+ const skipOnVariables = options && options.interpolation && options.interpolation.skipOnVariables !== undefined ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
1332
+ const todos = [{
1333
+ regex: this.regexpUnescape,
1334
+ safeValue: val => regexSafe(val)
1335
+ }, {
1336
+ regex: this.regexp,
1337
+ safeValue: val => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
1338
+ }];
1339
+ todos.forEach(todo => {
1340
+ replaces = 0;
1341
+ while (match = todo.regex.exec(str)) {
1342
+ const matchedVar = match[1].trim();
1343
+ value = handleFormat(matchedVar);
1344
+ if (value === undefined) {
1345
+ if (typeof missingInterpolationHandler === 'function') {
1346
+ const temp = missingInterpolationHandler(str, match, options);
1347
+ value = isString(temp) ? temp : '';
1348
+ } else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
1349
+ value = '';
1350
+ } else if (skipOnVariables) {
1351
+ value = match[0];
1352
+ continue;
1353
+ } else {
1354
+ this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
1355
+ value = '';
1356
+ }
1357
+ } else if (!isString(value) && !this.useRawValueToEscape) {
1358
+ value = makeString(value);
1359
+ }
1360
+ const safeValue = todo.safeValue(value);
1361
+ str = str.replace(match[0], safeValue);
1362
+ if (skipOnVariables) {
1363
+ todo.regex.lastIndex += value.length;
1364
+ todo.regex.lastIndex -= match[0].length;
1365
+ } else {
1366
+ todo.regex.lastIndex = 0;
1367
+ }
1368
+ replaces++;
1369
+ if (replaces >= this.maxReplaces) {
1370
+ break;
1371
+ }
1372
+ }
1373
+ });
1374
+ return str;
1375
+ }
1376
+ nest(str, fc) {
1377
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1378
+ let match;
1379
+ let value;
1380
+ let clonedOptions;
1381
+ const handleHasOptions = (key, inheritedOptions) => {
1382
+ const sep = this.nestingOptionsSeparator;
1383
+ if (key.indexOf(sep) < 0) return key;
1384
+ const c = key.split(new RegExp(`${sep}[ ]*{`));
1385
+ let optionsString = `{${c[1]}`;
1386
+ key = c[0];
1387
+ optionsString = this.interpolate(optionsString, clonedOptions);
1388
+ const matchedSingleQuotes = optionsString.match(/'/g);
1389
+ const matchedDoubleQuotes = optionsString.match(/"/g);
1390
+ if (matchedSingleQuotes && matchedSingleQuotes.length % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
1391
+ optionsString = optionsString.replace(/'/g, '"');
1392
+ }
1393
+ try {
1394
+ clonedOptions = JSON.parse(optionsString);
1395
+ if (inheritedOptions) clonedOptions = {
1396
+ ...inheritedOptions,
1397
+ ...clonedOptions
1398
+ };
1399
+ } catch (e) {
1400
+ this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
1401
+ return `${key}${sep}${optionsString}`;
1402
+ }
1403
+ if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
1404
+ return key;
1405
+ };
1406
+ while (match = this.nestingRegexp.exec(str)) {
1407
+ let formatters = [];
1408
+ clonedOptions = {
1409
+ ...options
1410
+ };
1411
+ clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
1412
+ clonedOptions.applyPostProcessor = false;
1413
+ delete clonedOptions.defaultValue;
1414
+ let doReduce = false;
1415
+ if (match[0].indexOf(this.formatSeparator) !== -1 && !/{.*}/.test(match[1])) {
1416
+ const r = match[1].split(this.formatSeparator).map(elem => elem.trim());
1417
+ match[1] = r.shift();
1418
+ formatters = r;
1419
+ doReduce = true;
1420
+ }
1421
+ value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
1422
+ if (value && match[0] === str && !isString(value)) return value;
1423
+ if (!isString(value)) value = makeString(value);
1424
+ if (!value) {
1425
+ this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
1426
+ value = '';
1427
+ }
1428
+ if (doReduce) {
1429
+ value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
1430
+ ...options,
1431
+ interpolationkey: match[1].trim()
1432
+ }), value.trim());
1433
+ }
1434
+ str = str.replace(match[0], value);
1435
+ this.regexp.lastIndex = 0;
1436
+ }
1437
+ return str;
1438
+ }
1439
+ }
1440
+
1441
+ const parseFormatStr = formatStr => {
1442
+ let formatName = formatStr.toLowerCase().trim();
1443
+ const formatOptions = {};
1444
+ if (formatStr.indexOf('(') > -1) {
1445
+ const p = formatStr.split('(');
1446
+ formatName = p[0].toLowerCase().trim();
1447
+ const optStr = p[1].substring(0, p[1].length - 1);
1448
+ if (formatName === 'currency' && optStr.indexOf(':') < 0) {
1449
+ if (!formatOptions.currency) formatOptions.currency = optStr.trim();
1450
+ } else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
1451
+ if (!formatOptions.range) formatOptions.range = optStr.trim();
1452
+ } else {
1453
+ const opts = optStr.split(';');
1454
+ opts.forEach(opt => {
1455
+ if (opt) {
1456
+ const [key, ...rest] = opt.split(':');
1457
+ const val = rest.join(':').trim().replace(/^'+|'+$/g, '');
1458
+ const trimmedKey = key.trim();
1459
+ if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
1460
+ if (val === 'false') formatOptions[trimmedKey] = false;
1461
+ if (val === 'true') formatOptions[trimmedKey] = true;
1462
+ if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
1463
+ }
1464
+ });
1465
+ }
1466
+ }
1467
+ return {
1468
+ formatName,
1469
+ formatOptions
1470
+ };
1471
+ };
1472
+ const createCachedFormatter = fn => {
1473
+ const cache = {};
1474
+ return (val, lng, options) => {
1475
+ let optForCache = options;
1476
+ if (options && options.interpolationkey && options.formatParams && options.formatParams[options.interpolationkey] && options[options.interpolationkey]) {
1477
+ optForCache = {
1478
+ ...optForCache,
1479
+ [options.interpolationkey]: undefined
1480
+ };
1481
+ }
1482
+ const key = lng + JSON.stringify(optForCache);
1483
+ let formatter = cache[key];
1484
+ if (!formatter) {
1485
+ formatter = fn(getCleanedCode(lng), options);
1486
+ cache[key] = formatter;
1487
+ }
1488
+ return formatter(val);
1489
+ };
1490
+ };
1491
+ class Formatter {
1492
+ constructor() {
1493
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1494
+ this.logger = baseLogger.create('formatter');
1495
+ this.options = options;
1496
+ this.formats = {
1497
+ number: createCachedFormatter((lng, opt) => {
1498
+ const formatter = new Intl.NumberFormat(lng, {
1499
+ ...opt
1500
+ });
1501
+ return val => formatter.format(val);
1502
+ }),
1503
+ currency: createCachedFormatter((lng, opt) => {
1504
+ const formatter = new Intl.NumberFormat(lng, {
1505
+ ...opt,
1506
+ style: 'currency'
1507
+ });
1508
+ return val => formatter.format(val);
1509
+ }),
1510
+ datetime: createCachedFormatter((lng, opt) => {
1511
+ const formatter = new Intl.DateTimeFormat(lng, {
1512
+ ...opt
1513
+ });
1514
+ return val => formatter.format(val);
1515
+ }),
1516
+ relativetime: createCachedFormatter((lng, opt) => {
1517
+ const formatter = new Intl.RelativeTimeFormat(lng, {
1518
+ ...opt
1519
+ });
1520
+ return val => formatter.format(val, opt.range || 'day');
1521
+ }),
1522
+ list: createCachedFormatter((lng, opt) => {
1523
+ const formatter = new Intl.ListFormat(lng, {
1524
+ ...opt
1525
+ });
1526
+ return val => formatter.format(val);
1527
+ })
1528
+ };
1529
+ this.init(options);
1530
+ }
1531
+ init(services) {
1532
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
1533
+ interpolation: {}
1534
+ };
1535
+ this.formatSeparator = options.interpolation.formatSeparator || ',';
1536
+ }
1537
+ add(name, fc) {
1538
+ this.formats[name.toLowerCase().trim()] = fc;
1539
+ }
1540
+ addCached(name, fc) {
1541
+ this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
1542
+ }
1543
+ format(value, format, lng) {
1544
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1545
+ const formats = format.split(this.formatSeparator);
1546
+ if (formats.length > 1 && formats[0].indexOf('(') > 1 && formats[0].indexOf(')') < 0 && formats.find(f => f.indexOf(')') > -1)) {
1547
+ const lastIndex = formats.findIndex(f => f.indexOf(')') > -1);
1548
+ formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
1549
+ }
1550
+ const result = formats.reduce((mem, f) => {
1551
+ const {
1552
+ formatName,
1553
+ formatOptions
1554
+ } = parseFormatStr(f);
1555
+ if (this.formats[formatName]) {
1556
+ let formatted = mem;
1557
+ try {
1558
+ const valOptions = options && options.formatParams && options.formatParams[options.interpolationkey] || {};
1559
+ const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
1560
+ formatted = this.formats[formatName](mem, l, {
1561
+ ...formatOptions,
1562
+ ...options,
1563
+ ...valOptions
1564
+ });
1565
+ } catch (error) {
1566
+ this.logger.warn(error);
1567
+ }
1568
+ return formatted;
1569
+ } else {
1570
+ this.logger.warn(`there was no format function for ${formatName}`);
1571
+ }
1572
+ return mem;
1573
+ }, value);
1574
+ return result;
1575
+ }
1576
+ }
1577
+
1578
+ const removePending = (q, name) => {
1579
+ if (q.pending[name] !== undefined) {
1580
+ delete q.pending[name];
1581
+ q.pendingCount--;
1582
+ }
1583
+ };
1584
+ class Connector extends EventEmitter {
1585
+ constructor(backend, store, services) {
1586
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1587
+ super();
1588
+ this.backend = backend;
1589
+ this.store = store;
1590
+ this.services = services;
1591
+ this.languageUtils = services.languageUtils;
1592
+ this.options = options;
1593
+ this.logger = baseLogger.create('backendConnector');
1594
+ this.waitingReads = [];
1595
+ this.maxParallelReads = options.maxParallelReads || 10;
1596
+ this.readingCalls = 0;
1597
+ this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
1598
+ this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
1599
+ this.state = {};
1600
+ this.queue = [];
1601
+ if (this.backend && this.backend.init) {
1602
+ this.backend.init(services, options.backend, options);
1603
+ }
1604
+ }
1605
+ queueLoad(languages, namespaces, options, callback) {
1606
+ const toLoad = {};
1607
+ const pending = {};
1608
+ const toLoadLanguages = {};
1609
+ const toLoadNamespaces = {};
1610
+ languages.forEach(lng => {
1611
+ let hasAllNamespaces = true;
1612
+ namespaces.forEach(ns => {
1613
+ const name = `${lng}|${ns}`;
1614
+ if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
1615
+ this.state[name] = 2;
1616
+ } else if (this.state[name] < 0) ; else if (this.state[name] === 1) {
1617
+ if (pending[name] === undefined) pending[name] = true;
1618
+ } else {
1619
+ this.state[name] = 1;
1620
+ hasAllNamespaces = false;
1621
+ if (pending[name] === undefined) pending[name] = true;
1622
+ if (toLoad[name] === undefined) toLoad[name] = true;
1623
+ if (toLoadNamespaces[ns] === undefined) toLoadNamespaces[ns] = true;
1624
+ }
1625
+ });
1626
+ if (!hasAllNamespaces) toLoadLanguages[lng] = true;
1627
+ });
1628
+ if (Object.keys(toLoad).length || Object.keys(pending).length) {
1629
+ this.queue.push({
1630
+ pending,
1631
+ pendingCount: Object.keys(pending).length,
1632
+ loaded: {},
1633
+ errors: [],
1634
+ callback
1635
+ });
1636
+ }
1637
+ return {
1638
+ toLoad: Object.keys(toLoad),
1639
+ pending: Object.keys(pending),
1640
+ toLoadLanguages: Object.keys(toLoadLanguages),
1641
+ toLoadNamespaces: Object.keys(toLoadNamespaces)
1642
+ };
1643
+ }
1644
+ loaded(name, err, data) {
1645
+ const s = name.split('|');
1646
+ const lng = s[0];
1647
+ const ns = s[1];
1648
+ if (err) this.emit('failedLoading', lng, ns, err);
1649
+ if (!err && data) {
1650
+ this.store.addResourceBundle(lng, ns, data, undefined, undefined, {
1651
+ skipCopy: true
1652
+ });
1653
+ }
1654
+ this.state[name] = err ? -1 : 2;
1655
+ if (err && data) this.state[name] = 0;
1656
+ const loaded = {};
1657
+ this.queue.forEach(q => {
1658
+ pushPath(q.loaded, [lng], ns);
1659
+ removePending(q, name);
1660
+ if (err) q.errors.push(err);
1661
+ if (q.pendingCount === 0 && !q.done) {
1662
+ Object.keys(q.loaded).forEach(l => {
1663
+ if (!loaded[l]) loaded[l] = {};
1664
+ const loadedKeys = q.loaded[l];
1665
+ if (loadedKeys.length) {
1666
+ loadedKeys.forEach(n => {
1667
+ if (loaded[l][n] === undefined) loaded[l][n] = true;
1668
+ });
1669
+ }
1670
+ });
1671
+ q.done = true;
1672
+ if (q.errors.length) {
1673
+ q.callback(q.errors);
1674
+ } else {
1675
+ q.callback();
1676
+ }
1677
+ }
1678
+ });
1679
+ this.emit('loaded', loaded);
1680
+ this.queue = this.queue.filter(q => !q.done);
1681
+ }
1682
+ read(lng, ns, fcName) {
1683
+ let tried = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
1684
+ let wait = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : this.retryTimeout;
1685
+ let callback = arguments.length > 5 ? arguments[5] : undefined;
1686
+ if (!lng.length) return callback(null, {});
1687
+ if (this.readingCalls >= this.maxParallelReads) {
1688
+ this.waitingReads.push({
1689
+ lng,
1690
+ ns,
1691
+ fcName,
1692
+ tried,
1693
+ wait,
1694
+ callback
1695
+ });
1696
+ return;
1697
+ }
1698
+ this.readingCalls++;
1699
+ const resolver = (err, data) => {
1700
+ this.readingCalls--;
1701
+ if (this.waitingReads.length > 0) {
1702
+ const next = this.waitingReads.shift();
1703
+ this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
1704
+ }
1705
+ if (err && data && tried < this.maxRetries) {
1706
+ setTimeout(() => {
1707
+ this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
1708
+ }, wait);
1709
+ return;
1710
+ }
1711
+ callback(err, data);
1712
+ };
1713
+ const fc = this.backend[fcName].bind(this.backend);
1714
+ if (fc.length === 2) {
1715
+ try {
1716
+ const r = fc(lng, ns);
1717
+ if (r && typeof r.then === 'function') {
1718
+ r.then(data => resolver(null, data)).catch(resolver);
1719
+ } else {
1720
+ resolver(null, r);
1721
+ }
1722
+ } catch (err) {
1723
+ resolver(err);
1724
+ }
1725
+ return;
1726
+ }
1727
+ return fc(lng, ns, resolver);
1728
+ }
1729
+ prepareLoading(languages, namespaces) {
1730
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1731
+ let callback = arguments.length > 3 ? arguments[3] : undefined;
1732
+ if (!this.backend) {
1733
+ this.logger.warn('No backend was added via i18next.use. Will not load resources.');
1734
+ return callback && callback();
1735
+ }
1736
+ if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
1737
+ if (isString(namespaces)) namespaces = [namespaces];
1738
+ const toLoad = this.queueLoad(languages, namespaces, options, callback);
1739
+ if (!toLoad.toLoad.length) {
1740
+ if (!toLoad.pending.length) callback();
1741
+ return null;
1742
+ }
1743
+ toLoad.toLoad.forEach(name => {
1744
+ this.loadOne(name);
1745
+ });
1746
+ }
1747
+ load(languages, namespaces, callback) {
1748
+ this.prepareLoading(languages, namespaces, {}, callback);
1749
+ }
1750
+ reload(languages, namespaces, callback) {
1751
+ this.prepareLoading(languages, namespaces, {
1752
+ reload: true
1753
+ }, callback);
1754
+ }
1755
+ loadOne(name) {
1756
+ let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
1757
+ const s = name.split('|');
1758
+ const lng = s[0];
1759
+ const ns = s[1];
1760
+ this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
1761
+ if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
1762
+ if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
1763
+ this.loaded(name, err, data);
1764
+ });
1765
+ }
1766
+ saveMissing(languages, namespace, key, fallbackValue, isUpdate) {
1767
+ let options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
1768
+ let clb = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : () => {};
1769
+ if (this.services.utils && this.services.utils.hasLoadedNamespace && !this.services.utils.hasLoadedNamespace(namespace)) {
1770
+ this.logger.warn(`did not save key "${key}" as the namespace "${namespace}" was not yet loaded`, 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
1771
+ return;
1772
+ }
1773
+ if (key === undefined || key === null || key === '') return;
1774
+ if (this.backend && this.backend.create) {
1775
+ const opts = {
1776
+ ...options,
1777
+ isUpdate
1778
+ };
1779
+ const fc = this.backend.create.bind(this.backend);
1780
+ if (fc.length < 6) {
1781
+ try {
1782
+ let r;
1783
+ if (fc.length === 5) {
1784
+ r = fc(languages, namespace, key, fallbackValue, opts);
1785
+ } else {
1786
+ r = fc(languages, namespace, key, fallbackValue);
1787
+ }
1788
+ if (r && typeof r.then === 'function') {
1789
+ r.then(data => clb(null, data)).catch(clb);
1790
+ } else {
1791
+ clb(null, r);
1792
+ }
1793
+ } catch (err) {
1794
+ clb(err);
1795
+ }
1796
+ } else {
1797
+ fc(languages, namespace, key, fallbackValue, clb, opts);
1798
+ }
1799
+ }
1800
+ if (!languages || !languages[0]) return;
1801
+ this.store.addResource(languages[0], namespace, key, fallbackValue);
1802
+ }
1803
+ }
1804
+
1805
+ const get = () => ({
1806
+ debug: false,
1807
+ initImmediate: true,
1808
+ ns: ['translation'],
1809
+ defaultNS: ['translation'],
1810
+ fallbackLng: ['dev'],
1811
+ fallbackNS: false,
1812
+ supportedLngs: false,
1813
+ nonExplicitSupportedLngs: false,
1814
+ load: 'all',
1815
+ preload: false,
1816
+ simplifyPluralSuffix: true,
1817
+ keySeparator: '.',
1818
+ nsSeparator: ':',
1819
+ pluralSeparator: '_',
1820
+ contextSeparator: '_',
1821
+ partialBundledLanguages: false,
1822
+ saveMissing: false,
1823
+ updateMissing: false,
1824
+ saveMissingTo: 'fallback',
1825
+ saveMissingPlurals: true,
1826
+ missingKeyHandler: false,
1827
+ missingInterpolationHandler: false,
1828
+ postProcess: false,
1829
+ postProcessPassResolved: false,
1830
+ returnNull: false,
1831
+ returnEmptyString: true,
1832
+ returnObjects: false,
1833
+ joinArrays: false,
1834
+ returnedObjectHandler: false,
1835
+ parseMissingKeyHandler: false,
1836
+ appendNamespaceToMissingKey: false,
1837
+ appendNamespaceToCIMode: false,
1838
+ overloadTranslationOptionHandler: args => {
1839
+ let ret = {};
1840
+ if (typeof args[1] === 'object') ret = args[1];
1841
+ if (isString(args[1])) ret.defaultValue = args[1];
1842
+ if (isString(args[2])) ret.tDescription = args[2];
1843
+ if (typeof args[2] === 'object' || typeof args[3] === 'object') {
1844
+ const options = args[3] || args[2];
1845
+ Object.keys(options).forEach(key => {
1846
+ ret[key] = options[key];
1847
+ });
1848
+ }
1849
+ return ret;
1850
+ },
1851
+ interpolation: {
1852
+ escapeValue: true,
1853
+ format: value => value,
1854
+ prefix: '{{',
1855
+ suffix: '}}',
1856
+ formatSeparator: ',',
1857
+ unescapePrefix: '-',
1858
+ nestingPrefix: '$t(',
1859
+ nestingSuffix: ')',
1860
+ nestingOptionsSeparator: ',',
1861
+ maxReplaces: 1000,
1862
+ skipOnVariables: true
1863
+ }
1864
+ });
1865
+ const transformOptions = options => {
1866
+ if (isString(options.ns)) options.ns = [options.ns];
1867
+ if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
1868
+ if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
1869
+ if (options.supportedLngs && options.supportedLngs.indexOf('cimode') < 0) {
1870
+ options.supportedLngs = options.supportedLngs.concat(['cimode']);
1871
+ }
1872
+ return options;
1873
+ };
1874
+
1875
+ const noop = () => {};
1876
+ const bindMemberFunctions = inst => {
1877
+ const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
1878
+ mems.forEach(mem => {
1879
+ if (typeof inst[mem] === 'function') {
1880
+ inst[mem] = inst[mem].bind(inst);
1881
+ }
1882
+ });
1883
+ };
1884
+ class I18n extends EventEmitter {
1885
+ constructor() {
1886
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1887
+ let callback = arguments.length > 1 ? arguments[1] : undefined;
1888
+ super();
1889
+ this.options = transformOptions(options);
1890
+ this.services = {};
1891
+ this.logger = baseLogger;
1892
+ this.modules = {
1893
+ external: []
1894
+ };
1895
+ bindMemberFunctions(this);
1896
+ if (callback && !this.isInitialized && !options.isClone) {
1897
+ if (!this.options.initImmediate) {
1898
+ this.init(options, callback);
1899
+ return this;
1900
+ }
1901
+ setTimeout(() => {
1902
+ this.init(options, callback);
1903
+ }, 0);
1904
+ }
1905
+ }
1906
+ init() {
1907
+ var _this = this;
1908
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1909
+ let callback = arguments.length > 1 ? arguments[1] : undefined;
1910
+ this.isInitializing = true;
1911
+ if (typeof options === 'function') {
1912
+ callback = options;
1913
+ options = {};
1914
+ }
1915
+ if (!options.defaultNS && options.defaultNS !== false && options.ns) {
1916
+ if (isString(options.ns)) {
1917
+ options.defaultNS = options.ns;
1918
+ } else if (options.ns.indexOf('translation') < 0) {
1919
+ options.defaultNS = options.ns[0];
1920
+ }
1921
+ }
1922
+ const defOpts = get();
1923
+ this.options = {
1924
+ ...defOpts,
1925
+ ...this.options,
1926
+ ...transformOptions(options)
1927
+ };
1928
+ if (this.options.compatibilityAPI !== 'v1') {
1929
+ this.options.interpolation = {
1930
+ ...defOpts.interpolation,
1931
+ ...this.options.interpolation
1932
+ };
1933
+ }
1934
+ if (options.keySeparator !== undefined) {
1935
+ this.options.userDefinedKeySeparator = options.keySeparator;
1936
+ }
1937
+ if (options.nsSeparator !== undefined) {
1938
+ this.options.userDefinedNsSeparator = options.nsSeparator;
1939
+ }
1940
+ const createClassOnDemand = ClassOrObject => {
1941
+ if (!ClassOrObject) return null;
1942
+ if (typeof ClassOrObject === 'function') return new ClassOrObject();
1943
+ return ClassOrObject;
1944
+ };
1945
+ if (!this.options.isClone) {
1946
+ if (this.modules.logger) {
1947
+ baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
1948
+ } else {
1949
+ baseLogger.init(null, this.options);
1950
+ }
1951
+ let formatter;
1952
+ if (this.modules.formatter) {
1953
+ formatter = this.modules.formatter;
1954
+ } else if (typeof Intl !== 'undefined') {
1955
+ formatter = Formatter;
1956
+ }
1957
+ const lu = new LanguageUtil(this.options);
1958
+ this.store = new ResourceStore(this.options.resources, this.options);
1959
+ const s = this.services;
1960
+ s.logger = baseLogger;
1961
+ s.resourceStore = this.store;
1962
+ s.languageUtils = lu;
1963
+ s.pluralResolver = new PluralResolver(lu, {
1964
+ prepend: this.options.pluralSeparator,
1965
+ compatibilityJSON: this.options.compatibilityJSON,
1966
+ simplifyPluralSuffix: this.options.simplifyPluralSuffix
1967
+ });
1968
+ if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
1969
+ s.formatter = createClassOnDemand(formatter);
1970
+ s.formatter.init(s, this.options);
1971
+ this.options.interpolation.format = s.formatter.format.bind(s.formatter);
1972
+ }
1973
+ s.interpolator = new Interpolator(this.options);
1974
+ s.utils = {
1975
+ hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
1976
+ };
1977
+ s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
1978
+ s.backendConnector.on('*', function (event) {
1979
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1980
+ args[_key - 1] = arguments[_key];
1981
+ }
1982
+ _this.emit(event, ...args);
1983
+ });
1984
+ if (this.modules.languageDetector) {
1985
+ s.languageDetector = createClassOnDemand(this.modules.languageDetector);
1986
+ if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
1987
+ }
1988
+ if (this.modules.i18nFormat) {
1989
+ s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
1990
+ if (s.i18nFormat.init) s.i18nFormat.init(this);
1991
+ }
1992
+ this.translator = new Translator(this.services, this.options);
1993
+ this.translator.on('*', function (event) {
1994
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
1995
+ args[_key2 - 1] = arguments[_key2];
1996
+ }
1997
+ _this.emit(event, ...args);
1998
+ });
1999
+ this.modules.external.forEach(m => {
2000
+ if (m.init) m.init(this);
2001
+ });
2002
+ }
2003
+ this.format = this.options.interpolation.format;
2004
+ if (!callback) callback = noop;
2005
+ if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
2006
+ const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2007
+ if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
2008
+ }
2009
+ if (!this.services.languageDetector && !this.options.lng) {
2010
+ this.logger.warn('init: no languageDetector is used and no lng is defined');
2011
+ }
2012
+ const storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
2013
+ storeApi.forEach(fcName => {
2014
+ this[fcName] = function () {
2015
+ return _this.store[fcName](...arguments);
2016
+ };
2017
+ });
2018
+ const storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
2019
+ storeApiChained.forEach(fcName => {
2020
+ this[fcName] = function () {
2021
+ _this.store[fcName](...arguments);
2022
+ return _this;
2023
+ };
2024
+ });
2025
+ const deferred = defer();
2026
+ const load = () => {
2027
+ const finish = (err, t) => {
2028
+ this.isInitializing = false;
2029
+ if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
2030
+ this.isInitialized = true;
2031
+ if (!this.options.isClone) this.logger.log('initialized', this.options);
2032
+ this.emit('initialized', this.options);
2033
+ deferred.resolve(t);
2034
+ callback(err, t);
2035
+ };
2036
+ if (this.languages && this.options.compatibilityAPI !== 'v1' && !this.isInitialized) return finish(null, this.t.bind(this));
2037
+ this.changeLanguage(this.options.lng, finish);
2038
+ };
2039
+ if (this.options.resources || !this.options.initImmediate) {
2040
+ load();
2041
+ } else {
2042
+ setTimeout(load, 0);
2043
+ }
2044
+ return deferred;
2045
+ }
2046
+ loadResources(language) {
2047
+ let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
2048
+ let usedCallback = callback;
2049
+ const usedLng = isString(language) ? language : this.language;
2050
+ if (typeof language === 'function') usedCallback = language;
2051
+ if (!this.options.resources || this.options.partialBundledLanguages) {
2052
+ if (usedLng && usedLng.toLowerCase() === 'cimode' && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
2053
+ const toLoad = [];
2054
+ const append = lng => {
2055
+ if (!lng) return;
2056
+ if (lng === 'cimode') return;
2057
+ const lngs = this.services.languageUtils.toResolveHierarchy(lng);
2058
+ lngs.forEach(l => {
2059
+ if (l === 'cimode') return;
2060
+ if (toLoad.indexOf(l) < 0) toLoad.push(l);
2061
+ });
2062
+ };
2063
+ if (!usedLng) {
2064
+ const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2065
+ fallbacks.forEach(l => append(l));
2066
+ } else {
2067
+ append(usedLng);
2068
+ }
2069
+ if (this.options.preload) {
2070
+ this.options.preload.forEach(l => append(l));
2071
+ }
2072
+ this.services.backendConnector.load(toLoad, this.options.ns, e => {
2073
+ if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
2074
+ usedCallback(e);
2075
+ });
2076
+ } else {
2077
+ usedCallback(null);
2078
+ }
2079
+ }
2080
+ reloadResources(lngs, ns, callback) {
2081
+ const deferred = defer();
2082
+ if (typeof lngs === 'function') {
2083
+ callback = lngs;
2084
+ lngs = undefined;
2085
+ }
2086
+ if (typeof ns === 'function') {
2087
+ callback = ns;
2088
+ ns = undefined;
2089
+ }
2090
+ if (!lngs) lngs = this.languages;
2091
+ if (!ns) ns = this.options.ns;
2092
+ if (!callback) callback = noop;
2093
+ this.services.backendConnector.reload(lngs, ns, err => {
2094
+ deferred.resolve();
2095
+ callback(err);
2096
+ });
2097
+ return deferred;
2098
+ }
2099
+ use(module) {
2100
+ if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
2101
+ if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
2102
+ if (module.type === 'backend') {
2103
+ this.modules.backend = module;
2104
+ }
2105
+ if (module.type === 'logger' || module.log && module.warn && module.error) {
2106
+ this.modules.logger = module;
2107
+ }
2108
+ if (module.type === 'languageDetector') {
2109
+ this.modules.languageDetector = module;
2110
+ }
2111
+ if (module.type === 'i18nFormat') {
2112
+ this.modules.i18nFormat = module;
2113
+ }
2114
+ if (module.type === 'postProcessor') {
2115
+ postProcessor.addPostProcessor(module);
2116
+ }
2117
+ if (module.type === 'formatter') {
2118
+ this.modules.formatter = module;
2119
+ }
2120
+ if (module.type === '3rdParty') {
2121
+ this.modules.external.push(module);
2122
+ }
2123
+ return this;
2124
+ }
2125
+ setResolvedLanguage(l) {
2126
+ if (!l || !this.languages) return;
2127
+ if (['cimode', 'dev'].indexOf(l) > -1) return;
2128
+ for (let li = 0; li < this.languages.length; li++) {
2129
+ const lngInLngs = this.languages[li];
2130
+ if (['cimode', 'dev'].indexOf(lngInLngs) > -1) continue;
2131
+ if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
2132
+ this.resolvedLanguage = lngInLngs;
2133
+ break;
2134
+ }
2135
+ }
2136
+ }
2137
+ changeLanguage(lng, callback) {
2138
+ var _this2 = this;
2139
+ this.isLanguageChangingTo = lng;
2140
+ const deferred = defer();
2141
+ this.emit('languageChanging', lng);
2142
+ const setLngProps = l => {
2143
+ this.language = l;
2144
+ this.languages = this.services.languageUtils.toResolveHierarchy(l);
2145
+ this.resolvedLanguage = undefined;
2146
+ this.setResolvedLanguage(l);
2147
+ };
2148
+ const done = (err, l) => {
2149
+ if (l) {
2150
+ setLngProps(l);
2151
+ this.translator.changeLanguage(l);
2152
+ this.isLanguageChangingTo = undefined;
2153
+ this.emit('languageChanged', l);
2154
+ this.logger.log('languageChanged', l);
2155
+ } else {
2156
+ this.isLanguageChangingTo = undefined;
2157
+ }
2158
+ deferred.resolve(function () {
2159
+ return _this2.t(...arguments);
2160
+ });
2161
+ if (callback) callback(err, function () {
2162
+ return _this2.t(...arguments);
2163
+ });
2164
+ };
2165
+ const setLng = lngs => {
2166
+ if (!lng && !lngs && this.services.languageDetector) lngs = [];
2167
+ const l = isString(lngs) ? lngs : this.services.languageUtils.getBestMatchFromCodes(lngs);
2168
+ if (l) {
2169
+ if (!this.language) {
2170
+ setLngProps(l);
2171
+ }
2172
+ if (!this.translator.language) this.translator.changeLanguage(l);
2173
+ if (this.services.languageDetector && this.services.languageDetector.cacheUserLanguage) this.services.languageDetector.cacheUserLanguage(l);
2174
+ }
2175
+ this.loadResources(l, err => {
2176
+ done(err, l);
2177
+ });
2178
+ };
2179
+ if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
2180
+ setLng(this.services.languageDetector.detect());
2181
+ } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
2182
+ if (this.services.languageDetector.detect.length === 0) {
2183
+ this.services.languageDetector.detect().then(setLng);
2184
+ } else {
2185
+ this.services.languageDetector.detect(setLng);
2186
+ }
2187
+ } else {
2188
+ setLng(lng);
2189
+ }
2190
+ return deferred;
2191
+ }
2192
+ getFixedT(lng, ns, keyPrefix) {
2193
+ var _this3 = this;
2194
+ const fixedT = function (key, opts) {
2195
+ let options;
2196
+ if (typeof opts !== 'object') {
2197
+ for (var _len3 = arguments.length, rest = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
2198
+ rest[_key3 - 2] = arguments[_key3];
2199
+ }
2200
+ options = _this3.options.overloadTranslationOptionHandler([key, opts].concat(rest));
2201
+ } else {
2202
+ options = {
2203
+ ...opts
2204
+ };
2205
+ }
2206
+ options.lng = options.lng || fixedT.lng;
2207
+ options.lngs = options.lngs || fixedT.lngs;
2208
+ options.ns = options.ns || fixedT.ns;
2209
+ if (options.keyPrefix !== '') options.keyPrefix = options.keyPrefix || keyPrefix || fixedT.keyPrefix;
2210
+ const keySeparator = _this3.options.keySeparator || '.';
2211
+ let resultKey;
2212
+ if (options.keyPrefix && Array.isArray(key)) {
2213
+ resultKey = key.map(k => `${options.keyPrefix}${keySeparator}${k}`);
2214
+ } else {
2215
+ resultKey = options.keyPrefix ? `${options.keyPrefix}${keySeparator}${key}` : key;
2216
+ }
2217
+ return _this3.t(resultKey, options);
2218
+ };
2219
+ if (isString(lng)) {
2220
+ fixedT.lng = lng;
2221
+ } else {
2222
+ fixedT.lngs = lng;
2223
+ }
2224
+ fixedT.ns = ns;
2225
+ fixedT.keyPrefix = keyPrefix;
2226
+ return fixedT;
2227
+ }
2228
+ t() {
2229
+ return this.translator && this.translator.translate(...arguments);
2230
+ }
2231
+ exists() {
2232
+ return this.translator && this.translator.exists(...arguments);
2233
+ }
2234
+ setDefaultNamespace(ns) {
2235
+ this.options.defaultNS = ns;
2236
+ }
2237
+ hasLoadedNamespace(ns) {
2238
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2239
+ if (!this.isInitialized) {
2240
+ this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
2241
+ return false;
2242
+ }
2243
+ if (!this.languages || !this.languages.length) {
2244
+ this.logger.warn('hasLoadedNamespace: i18n.languages were undefined or empty', this.languages);
2245
+ return false;
2246
+ }
2247
+ const lng = options.lng || this.resolvedLanguage || this.languages[0];
2248
+ const fallbackLng = this.options ? this.options.fallbackLng : false;
2249
+ const lastLng = this.languages[this.languages.length - 1];
2250
+ if (lng.toLowerCase() === 'cimode') return true;
2251
+ const loadNotPending = (l, n) => {
2252
+ const loadState = this.services.backendConnector.state[`${l}|${n}`];
2253
+ return loadState === -1 || loadState === 0 || loadState === 2;
2254
+ };
2255
+ if (options.precheck) {
2256
+ const preResult = options.precheck(this, loadNotPending);
2257
+ if (preResult !== undefined) return preResult;
2258
+ }
2259
+ if (this.hasResourceBundle(lng, ns)) return true;
2260
+ if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
2261
+ if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
2262
+ return false;
2263
+ }
2264
+ loadNamespaces(ns, callback) {
2265
+ const deferred = defer();
2266
+ if (!this.options.ns) {
2267
+ if (callback) callback();
2268
+ return Promise.resolve();
2269
+ }
2270
+ if (isString(ns)) ns = [ns];
2271
+ ns.forEach(n => {
2272
+ if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
2273
+ });
2274
+ this.loadResources(err => {
2275
+ deferred.resolve();
2276
+ if (callback) callback(err);
2277
+ });
2278
+ return deferred;
2279
+ }
2280
+ loadLanguages(lngs, callback) {
2281
+ const deferred = defer();
2282
+ if (isString(lngs)) lngs = [lngs];
2283
+ const preloaded = this.options.preload || [];
2284
+ const newLngs = lngs.filter(lng => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
2285
+ if (!newLngs.length) {
2286
+ if (callback) callback();
2287
+ return Promise.resolve();
2288
+ }
2289
+ this.options.preload = preloaded.concat(newLngs);
2290
+ this.loadResources(err => {
2291
+ deferred.resolve();
2292
+ if (callback) callback(err);
2293
+ });
2294
+ return deferred;
2295
+ }
2296
+ dir(lng) {
2297
+ if (!lng) lng = this.resolvedLanguage || (this.languages && this.languages.length > 0 ? this.languages[0] : this.language);
2298
+ if (!lng) return 'rtl';
2299
+ const rtlLngs = ['ar', 'shu', 'sqr', 'ssh', 'xaa', 'yhd', 'yud', 'aao', 'abh', 'abv', 'acm', 'acq', 'acw', 'acx', 'acy', 'adf', 'ads', 'aeb', 'aec', 'afb', 'ajp', 'apc', 'apd', 'arb', 'arq', 'ars', 'ary', 'arz', 'auz', 'avl', 'ayh', 'ayl', 'ayn', 'ayp', 'bbz', 'pga', 'he', 'iw', 'ps', 'pbt', 'pbu', 'pst', 'prp', 'prd', 'ug', 'ur', 'ydd', 'yds', 'yih', 'ji', 'yi', 'hbo', 'men', 'xmn', 'fa', 'jpr', 'peo', 'pes', 'prs', 'dv', 'sam', 'ckb'];
2300
+ const languageUtils = this.services && this.services.languageUtils || new LanguageUtil(get());
2301
+ return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf('-arab') > 1 ? 'rtl' : 'ltr';
2302
+ }
2303
+ static createInstance() {
2304
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2305
+ let callback = arguments.length > 1 ? arguments[1] : undefined;
2306
+ return new I18n(options, callback);
2307
+ }
2308
+ cloneInstance() {
2309
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2310
+ let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
2311
+ const forkResourceStore = options.forkResourceStore;
2312
+ if (forkResourceStore) delete options.forkResourceStore;
2313
+ const mergedOptions = {
2314
+ ...this.options,
2315
+ ...options,
2316
+ ...{
2317
+ isClone: true
2318
+ }
2319
+ };
2320
+ const clone = new I18n(mergedOptions);
2321
+ if (options.debug !== undefined || options.prefix !== undefined) {
2322
+ clone.logger = clone.logger.clone(options);
2323
+ }
2324
+ const membersToCopy = ['store', 'services', 'language'];
2325
+ membersToCopy.forEach(m => {
2326
+ clone[m] = this[m];
2327
+ });
2328
+ clone.services = {
2329
+ ...this.services
2330
+ };
2331
+ clone.services.utils = {
2332
+ hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2333
+ };
2334
+ if (forkResourceStore) {
2335
+ clone.store = new ResourceStore(this.store.data, mergedOptions);
2336
+ clone.services.resourceStore = clone.store;
2337
+ }
2338
+ clone.translator = new Translator(clone.services, mergedOptions);
2339
+ clone.translator.on('*', function (event) {
2340
+ for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
2341
+ args[_key4 - 1] = arguments[_key4];
2342
+ }
2343
+ clone.emit(event, ...args);
2344
+ });
2345
+ clone.init(mergedOptions, callback);
2346
+ clone.translator.options = mergedOptions;
2347
+ clone.translator.backendConnector.services.utils = {
2348
+ hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2349
+ };
2350
+ return clone;
2351
+ }
2352
+ toJSON() {
2353
+ return {
2354
+ options: this.options,
2355
+ store: this.store,
2356
+ language: this.language,
2357
+ languages: this.languages,
2358
+ resolvedLanguage: this.resolvedLanguage
2359
+ };
2360
+ }
2361
+ }
2362
+ const instance = I18n.createInstance();
2363
+ instance.createInstance = I18n.createInstance;
2364
+
2365
+ instance.createInstance;
2366
+ instance.dir;
2367
+ instance.init;
2368
+ instance.loadResources;
2369
+ instance.reloadResources;
2370
+ instance.use;
2371
+ instance.changeLanguage;
2372
+ instance.getFixedT;
2373
+ instance.t;
2374
+ instance.exists;
2375
+ instance.setDefaultNamespace;
2376
+ instance.hasLoadedNamespace;
2377
+ instance.loadNamespaces;
2378
+ instance.loadLanguages;
2379
+
2380
+ export default instance;