react-i18next 16.0.0 → 16.1.0

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.
Files changed (34) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/amd/react-i18next.js +3028 -844
  3. package/dist/amd/react-i18next.min.js +1 -1
  4. package/dist/commonjs/IcuTrans.js +35 -0
  5. package/dist/commonjs/IcuTransUtils/TranslationParserError.js +18 -0
  6. package/dist/commonjs/IcuTransUtils/htmlEntityDecoder.js +218 -0
  7. package/dist/commonjs/IcuTransUtils/index.js +49 -0
  8. package/dist/commonjs/IcuTransUtils/renderTranslation.js +114 -0
  9. package/dist/commonjs/IcuTransUtils/tokenizer.js +58 -0
  10. package/dist/commonjs/IcuTransWithoutContext.js +56 -0
  11. package/dist/commonjs/TransWithoutContext.js +2 -1
  12. package/dist/es/IcuTrans.js +29 -0
  13. package/dist/es/IcuTransUtils/TranslationParserError.js +11 -0
  14. package/dist/es/IcuTransUtils/htmlEntityDecoder.js +211 -0
  15. package/dist/es/IcuTransUtils/index.js +4 -0
  16. package/dist/es/IcuTransUtils/renderTranslation.js +106 -0
  17. package/dist/es/IcuTransUtils/tokenizer.js +51 -0
  18. package/dist/es/IcuTransWithoutContext.js +49 -0
  19. package/dist/es/TransWithoutContext.js +2 -1
  20. package/dist/es/package.json +1 -1
  21. package/dist/umd/react-i18next.js +3031 -847
  22. package/dist/umd/react-i18next.min.js +1 -1
  23. package/icu.macro.js +170 -48
  24. package/package.json +6 -3
  25. package/react-i18next.js +3031 -847
  26. package/react-i18next.min.js +1 -1
  27. package/src/IcuTrans.js +103 -0
  28. package/src/IcuTransUtils/TranslationParserError.js +24 -0
  29. package/src/IcuTransUtils/htmlEntityDecoder.js +264 -0
  30. package/src/IcuTransUtils/index.js +4 -0
  31. package/src/IcuTransUtils/renderTranslation.js +215 -0
  32. package/src/IcuTransUtils/tokenizer.js +78 -0
  33. package/src/IcuTransWithoutContext.js +146 -0
  34. package/src/TransWithoutContext.js +5 -1
@@ -1,867 +1,3051 @@
1
1
  define(['exports', 'react'], (function (exports, react) { 'use strict';
2
2
 
3
- function getDefaultExportFromCjs (x) {
4
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5
- }
3
+ const isString$1 = obj => typeof obj === 'string';
4
+ const defer = () => {
5
+ let res;
6
+ let rej;
7
+ const promise = new Promise((resolve, reject) => {
8
+ res = resolve;
9
+ rej = reject;
10
+ });
11
+ promise.resolve = res;
12
+ promise.reject = rej;
13
+ return promise;
14
+ };
15
+ const makeString = object => {
16
+ if (object == null) return '';
17
+ return '' + object;
18
+ };
19
+ const copy = (a, s, t) => {
20
+ a.forEach(m => {
21
+ if (s[m]) t[m] = s[m];
22
+ });
23
+ };
24
+ const lastOfPathSeparatorRegExp = /###/g;
25
+ const cleanKey = key => key && key.indexOf('###') > -1 ? key.replace(lastOfPathSeparatorRegExp, '.') : key;
26
+ const canNotTraverseDeeper = object => !object || isString$1(object);
27
+ const getLastOfPath = (object, path, Empty) => {
28
+ const stack = !isString$1(path) ? path : path.split('.');
29
+ let stackIndex = 0;
30
+ while (stackIndex < stack.length - 1) {
31
+ if (canNotTraverseDeeper(object)) return {};
32
+ const key = cleanKey(stack[stackIndex]);
33
+ if (!object[key] && Empty) object[key] = new Empty();
34
+ if (Object.prototype.hasOwnProperty.call(object, key)) {
35
+ object = object[key];
36
+ } else {
37
+ object = {};
38
+ }
39
+ ++stackIndex;
40
+ }
41
+ if (canNotTraverseDeeper(object)) return {};
42
+ return {
43
+ obj: object,
44
+ k: cleanKey(stack[stackIndex])
45
+ };
46
+ };
47
+ const setPath = (object, path, newValue) => {
48
+ const {
49
+ obj,
50
+ k
51
+ } = getLastOfPath(object, path, Object);
52
+ if (obj !== undefined || path.length === 1) {
53
+ obj[k] = newValue;
54
+ return;
55
+ }
56
+ let e = path[path.length - 1];
57
+ let p = path.slice(0, path.length - 1);
58
+ let last = getLastOfPath(object, p, Object);
59
+ while (last.obj === undefined && p.length) {
60
+ e = `${p[p.length - 1]}.${e}`;
61
+ p = p.slice(0, p.length - 1);
62
+ last = getLastOfPath(object, p, Object);
63
+ if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
64
+ last.obj = undefined;
65
+ }
66
+ }
67
+ last.obj[`${last.k}.${e}`] = newValue;
68
+ };
69
+ const pushPath = (object, path, newValue, concat) => {
70
+ const {
71
+ obj,
72
+ k
73
+ } = getLastOfPath(object, path, Object);
74
+ obj[k] = obj[k] || [];
75
+ obj[k].push(newValue);
76
+ };
77
+ const getPath = (object, path) => {
78
+ const {
79
+ obj,
80
+ k
81
+ } = getLastOfPath(object, path);
82
+ if (!obj) return undefined;
83
+ if (!Object.prototype.hasOwnProperty.call(obj, k)) return undefined;
84
+ return obj[k];
85
+ };
86
+ const getPathWithDefaults = (data, defaultData, key) => {
87
+ const value = getPath(data, key);
88
+ if (value !== undefined) {
89
+ return value;
90
+ }
91
+ return getPath(defaultData, key);
92
+ };
93
+ const deepExtend = (target, source, overwrite) => {
94
+ for (const prop in source) {
95
+ if (prop !== '__proto__' && prop !== 'constructor') {
96
+ if (prop in target) {
97
+ if (isString$1(target[prop]) || target[prop] instanceof String || isString$1(source[prop]) || source[prop] instanceof String) {
98
+ if (overwrite) target[prop] = source[prop];
99
+ } else {
100
+ deepExtend(target[prop], source[prop], overwrite);
101
+ }
102
+ } else {
103
+ target[prop] = source[prop];
104
+ }
105
+ }
106
+ }
107
+ return target;
108
+ };
109
+ const regexEscape = str => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
110
+ var _entityMap = {
111
+ '&': '&amp;',
112
+ '<': '&lt;',
113
+ '>': '&gt;',
114
+ '"': '&quot;',
115
+ "'": '&#39;',
116
+ '/': '&#x2F;'
117
+ };
118
+ const escape = data => {
119
+ if (isString$1(data)) {
120
+ return data.replace(/[&<>"'\/]/g, s => _entityMap[s]);
121
+ }
122
+ return data;
123
+ };
124
+ class RegExpCache {
125
+ constructor(capacity) {
126
+ this.capacity = capacity;
127
+ this.regExpMap = new Map();
128
+ this.regExpQueue = [];
129
+ }
130
+ getRegExp(pattern) {
131
+ const regExpFromCache = this.regExpMap.get(pattern);
132
+ if (regExpFromCache !== undefined) {
133
+ return regExpFromCache;
134
+ }
135
+ const regExpNew = new RegExp(pattern);
136
+ if (this.regExpQueue.length === this.capacity) {
137
+ this.regExpMap.delete(this.regExpQueue.shift());
138
+ }
139
+ this.regExpMap.set(pattern, regExpNew);
140
+ this.regExpQueue.push(pattern);
141
+ return regExpNew;
142
+ }
143
+ }
144
+ const chars = [' ', ',', '?', '!', ';'];
145
+ const looksLikeObjectPathRegExpCache = new RegExpCache(20);
146
+ const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
147
+ nsSeparator = nsSeparator || '';
148
+ keySeparator = keySeparator || '';
149
+ const possibleChars = chars.filter(c => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
150
+ if (possibleChars.length === 0) return true;
151
+ const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map(c => c === '?' ? '\\?' : c).join('|')})`);
152
+ let matched = !r.test(key);
153
+ if (!matched) {
154
+ const ki = key.indexOf(keySeparator);
155
+ if (ki > 0 && !r.test(key.substring(0, ki))) {
156
+ matched = true;
157
+ }
158
+ }
159
+ return matched;
160
+ };
161
+ const deepFind = (obj, path, keySeparator = '.') => {
162
+ if (!obj) return undefined;
163
+ if (obj[path]) {
164
+ if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
165
+ return obj[path];
166
+ }
167
+ const tokens = path.split(keySeparator);
168
+ let current = obj;
169
+ for (let i = 0; i < tokens.length;) {
170
+ if (!current || typeof current !== 'object') {
171
+ return undefined;
172
+ }
173
+ let next;
174
+ let nextPath = '';
175
+ for (let j = i; j < tokens.length; ++j) {
176
+ if (j !== i) {
177
+ nextPath += keySeparator;
178
+ }
179
+ nextPath += tokens[j];
180
+ next = current[nextPath];
181
+ if (next !== undefined) {
182
+ if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
183
+ continue;
184
+ }
185
+ i += j - i + 1;
186
+ break;
187
+ }
188
+ }
189
+ current = next;
190
+ }
191
+ return current;
192
+ };
193
+ const getCleanedCode = code => code?.replace('_', '-');
194
+ const consoleLogger = {
195
+ type: 'logger',
196
+ log(args) {
197
+ this.output('log', args);
198
+ },
199
+ warn(args) {
200
+ this.output('warn', args);
201
+ },
202
+ error(args) {
203
+ this.output('error', args);
204
+ },
205
+ output(type, args) {
206
+ console?.[type]?.apply?.(console, args);
207
+ }
208
+ };
209
+ class Logger {
210
+ constructor(concreteLogger, options = {}) {
211
+ this.init(concreteLogger, options);
212
+ }
213
+ init(concreteLogger, options = {}) {
214
+ this.prefix = options.prefix || 'i18next:';
215
+ this.logger = concreteLogger || consoleLogger;
216
+ this.options = options;
217
+ this.debug = options.debug;
218
+ }
219
+ log(...args) {
220
+ return this.forward(args, 'log', '', true);
221
+ }
222
+ warn(...args) {
223
+ return this.forward(args, 'warn', '', true);
224
+ }
225
+ error(...args) {
226
+ return this.forward(args, 'error', '');
227
+ }
228
+ deprecate(...args) {
229
+ return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
230
+ }
231
+ forward(args, lvl, prefix, debugOnly) {
232
+ if (debugOnly && !this.debug) return null;
233
+ if (isString$1(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
234
+ return this.logger[lvl](args);
235
+ }
236
+ create(moduleName) {
237
+ return new Logger(this.logger, {
238
+ ...{
239
+ prefix: `${this.prefix}:${moduleName}:`
240
+ },
241
+ ...this.options
242
+ });
243
+ }
244
+ clone(options) {
245
+ options = options || this.options;
246
+ options.prefix = options.prefix || this.prefix;
247
+ return new Logger(this.logger, options);
248
+ }
249
+ }
250
+ var baseLogger = new Logger();
251
+ class EventEmitter {
252
+ constructor() {
253
+ this.observers = {};
254
+ }
255
+ on(events, listener) {
256
+ events.split(' ').forEach(event => {
257
+ if (!this.observers[event]) this.observers[event] = new Map();
258
+ const numListeners = this.observers[event].get(listener) || 0;
259
+ this.observers[event].set(listener, numListeners + 1);
260
+ });
261
+ return this;
262
+ }
263
+ off(event, listener) {
264
+ if (!this.observers[event]) return;
265
+ if (!listener) {
266
+ delete this.observers[event];
267
+ return;
268
+ }
269
+ this.observers[event].delete(listener);
270
+ }
271
+ emit(event, ...args) {
272
+ if (this.observers[event]) {
273
+ const cloned = Array.from(this.observers[event].entries());
274
+ cloned.forEach(([observer, numTimesAdded]) => {
275
+ for (let i = 0; i < numTimesAdded; i++) {
276
+ observer(...args);
277
+ }
278
+ });
279
+ }
280
+ if (this.observers['*']) {
281
+ const cloned = Array.from(this.observers['*'].entries());
282
+ cloned.forEach(([observer, numTimesAdded]) => {
283
+ for (let i = 0; i < numTimesAdded; i++) {
284
+ observer.apply(observer, [event, ...args]);
285
+ }
286
+ });
287
+ }
288
+ }
289
+ }
290
+ class ResourceStore extends EventEmitter {
291
+ constructor(data, options = {
292
+ ns: ['translation'],
293
+ defaultNS: 'translation'
294
+ }) {
295
+ super();
296
+ this.data = data || {};
297
+ this.options = options;
298
+ if (this.options.keySeparator === undefined) {
299
+ this.options.keySeparator = '.';
300
+ }
301
+ if (this.options.ignoreJSONStructure === undefined) {
302
+ this.options.ignoreJSONStructure = true;
303
+ }
304
+ }
305
+ addNamespaces(ns) {
306
+ if (this.options.ns.indexOf(ns) < 0) {
307
+ this.options.ns.push(ns);
308
+ }
309
+ }
310
+ removeNamespaces(ns) {
311
+ const index = this.options.ns.indexOf(ns);
312
+ if (index > -1) {
313
+ this.options.ns.splice(index, 1);
314
+ }
315
+ }
316
+ getResource(lng, ns, key, options = {}) {
317
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
318
+ const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
319
+ let path;
320
+ if (lng.indexOf('.') > -1) {
321
+ path = lng.split('.');
322
+ } else {
323
+ path = [lng, ns];
324
+ if (key) {
325
+ if (Array.isArray(key)) {
326
+ path.push(...key);
327
+ } else if (isString$1(key) && keySeparator) {
328
+ path.push(...key.split(keySeparator));
329
+ } else {
330
+ path.push(key);
331
+ }
332
+ }
333
+ }
334
+ const result = getPath(this.data, path);
335
+ if (!result && !ns && !key && lng.indexOf('.') > -1) {
336
+ lng = path[0];
337
+ ns = path[1];
338
+ key = path.slice(2).join('.');
339
+ }
340
+ if (result || !ignoreJSONStructure || !isString$1(key)) return result;
341
+ return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
342
+ }
343
+ addResource(lng, ns, key, value, options = {
344
+ silent: false
345
+ }) {
346
+ const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
347
+ let path = [lng, ns];
348
+ if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
349
+ if (lng.indexOf('.') > -1) {
350
+ path = lng.split('.');
351
+ value = ns;
352
+ ns = path[1];
353
+ }
354
+ this.addNamespaces(ns);
355
+ setPath(this.data, path, value);
356
+ if (!options.silent) this.emit('added', lng, ns, key, value);
357
+ }
358
+ addResources(lng, ns, resources, options = {
359
+ silent: false
360
+ }) {
361
+ for (const m in resources) {
362
+ if (isString$1(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
363
+ silent: true
364
+ });
365
+ }
366
+ if (!options.silent) this.emit('added', lng, ns, resources);
367
+ }
368
+ addResourceBundle(lng, ns, resources, deep, overwrite, options = {
369
+ silent: false,
370
+ skipCopy: false
371
+ }) {
372
+ let path = [lng, ns];
373
+ if (lng.indexOf('.') > -1) {
374
+ path = lng.split('.');
375
+ deep = resources;
376
+ resources = ns;
377
+ ns = path[1];
378
+ }
379
+ this.addNamespaces(ns);
380
+ let pack = getPath(this.data, path) || {};
381
+ if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
382
+ if (deep) {
383
+ deepExtend(pack, resources, overwrite);
384
+ } else {
385
+ pack = {
386
+ ...pack,
387
+ ...resources
388
+ };
389
+ }
390
+ setPath(this.data, path, pack);
391
+ if (!options.silent) this.emit('added', lng, ns, resources);
392
+ }
393
+ removeResourceBundle(lng, ns) {
394
+ if (this.hasResourceBundle(lng, ns)) {
395
+ delete this.data[lng][ns];
396
+ }
397
+ this.removeNamespaces(ns);
398
+ this.emit('removed', lng, ns);
399
+ }
400
+ hasResourceBundle(lng, ns) {
401
+ return this.getResource(lng, ns) !== undefined;
402
+ }
403
+ getResourceBundle(lng, ns) {
404
+ if (!ns) ns = this.options.defaultNS;
405
+ return this.getResource(lng, ns);
406
+ }
407
+ getDataByLanguage(lng) {
408
+ return this.data[lng];
409
+ }
410
+ hasLanguageSomeTranslations(lng) {
411
+ const data = this.getDataByLanguage(lng);
412
+ const n = data && Object.keys(data) || [];
413
+ return !!n.find(v => data[v] && Object.keys(data[v]).length > 0);
414
+ }
415
+ toJSON() {
416
+ return this.data;
417
+ }
418
+ }
419
+ var postProcessor = {
420
+ processors: {},
421
+ addPostProcessor(module) {
422
+ this.processors[module.name] = module;
423
+ },
424
+ handle(processors, value, key, options, translator) {
425
+ processors.forEach(processor => {
426
+ value = this.processors[processor]?.process(value, key, options, translator) ?? value;
427
+ });
428
+ return value;
429
+ }
430
+ };
431
+ const PATH_KEY = Symbol('i18next/PATH_KEY');
432
+ function createProxy() {
433
+ const state = [];
434
+ const handler = Object.create(null);
435
+ let proxy;
436
+ handler.get = (target, key) => {
437
+ proxy?.revoke?.();
438
+ if (key === PATH_KEY) return state;
439
+ state.push(key);
440
+ proxy = Proxy.revocable(target, handler);
441
+ return proxy.proxy;
442
+ };
443
+ return Proxy.revocable(Object.create(null), handler).proxy;
444
+ }
445
+ function keysFromSelector(selector, opts) {
446
+ const {
447
+ [PATH_KEY]: path
448
+ } = selector(createProxy());
449
+ return path.join(opts?.keySeparator ?? '.');
450
+ }
451
+ const checkedLoadedFor = {};
452
+ const shouldHandleAsObject = res => !isString$1(res) && typeof res !== 'boolean' && typeof res !== 'number';
453
+ class Translator extends EventEmitter {
454
+ constructor(services, options = {}) {
455
+ super();
456
+ copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
457
+ this.options = options;
458
+ if (this.options.keySeparator === undefined) {
459
+ this.options.keySeparator = '.';
460
+ }
461
+ this.logger = baseLogger.create('translator');
462
+ }
463
+ changeLanguage(lng) {
464
+ if (lng) this.language = lng;
465
+ }
466
+ exists(key, o = {
467
+ interpolation: {}
468
+ }) {
469
+ const opt = {
470
+ ...o
471
+ };
472
+ if (key == null) return false;
473
+ const resolved = this.resolve(key, opt);
474
+ return resolved?.res !== undefined;
475
+ }
476
+ extractFromKey(key, opt) {
477
+ let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
478
+ if (nsSeparator === undefined) nsSeparator = ':';
479
+ const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
480
+ let namespaces = opt.ns || this.options.defaultNS || [];
481
+ const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
482
+ const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
483
+ if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
484
+ const m = key.match(this.interpolator.nestingRegexp);
485
+ if (m && m.length > 0) {
486
+ return {
487
+ key,
488
+ namespaces: isString$1(namespaces) ? [namespaces] : namespaces
489
+ };
490
+ }
491
+ const parts = key.split(nsSeparator);
492
+ if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
493
+ key = parts.join(keySeparator);
494
+ }
495
+ return {
496
+ key,
497
+ namespaces: isString$1(namespaces) ? [namespaces] : namespaces
498
+ };
499
+ }
500
+ translate(keys, o, lastKey) {
501
+ let opt = typeof o === 'object' ? {
502
+ ...o
503
+ } : o;
504
+ if (typeof opt !== 'object' && this.options.overloadTranslationOptionHandler) {
505
+ opt = this.options.overloadTranslationOptionHandler(arguments);
506
+ }
507
+ if (typeof opt === 'object') opt = {
508
+ ...opt
509
+ };
510
+ if (!opt) opt = {};
511
+ if (keys == null) return '';
512
+ if (typeof keys === 'function') keys = keysFromSelector(keys, {
513
+ ...this.options,
514
+ ...opt
515
+ });
516
+ if (!Array.isArray(keys)) keys = [String(keys)];
517
+ const returnDetails = opt.returnDetails !== undefined ? opt.returnDetails : this.options.returnDetails;
518
+ const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
519
+ const {
520
+ key,
521
+ namespaces
522
+ } = this.extractFromKey(keys[keys.length - 1], opt);
523
+ const namespace = namespaces[namespaces.length - 1];
524
+ let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
525
+ if (nsSeparator === undefined) nsSeparator = ':';
526
+ const lng = opt.lng || this.language;
527
+ const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
528
+ if (lng?.toLowerCase() === 'cimode') {
529
+ if (appendNamespaceToCIMode) {
530
+ if (returnDetails) {
531
+ return {
532
+ res: `${namespace}${nsSeparator}${key}`,
533
+ usedKey: key,
534
+ exactUsedKey: key,
535
+ usedLng: lng,
536
+ usedNS: namespace,
537
+ usedParams: this.getUsedParamsDetails(opt)
538
+ };
539
+ }
540
+ return `${namespace}${nsSeparator}${key}`;
541
+ }
542
+ if (returnDetails) {
543
+ return {
544
+ res: key,
545
+ usedKey: key,
546
+ exactUsedKey: key,
547
+ usedLng: lng,
548
+ usedNS: namespace,
549
+ usedParams: this.getUsedParamsDetails(opt)
550
+ };
551
+ }
552
+ return key;
553
+ }
554
+ const resolved = this.resolve(keys, opt);
555
+ let res = resolved?.res;
556
+ const resUsedKey = resolved?.usedKey || key;
557
+ const resExactUsedKey = resolved?.exactUsedKey || key;
558
+ const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
559
+ const joinArrays = opt.joinArrays !== undefined ? opt.joinArrays : this.options.joinArrays;
560
+ const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
561
+ const needsPluralHandling = opt.count !== undefined && !isString$1(opt.count);
562
+ const hasDefaultValue = Translator.hasDefaultValue(opt);
563
+ const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : '';
564
+ const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
565
+ ordinal: false
566
+ }) : '';
567
+ const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
568
+ const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
569
+ let resForObjHndl = res;
570
+ if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
571
+ resForObjHndl = defaultValue;
572
+ }
573
+ const handleAsObject = shouldHandleAsObject(resForObjHndl);
574
+ const resType = Object.prototype.toString.apply(resForObjHndl);
575
+ if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString$1(joinArrays) && Array.isArray(resForObjHndl))) {
576
+ if (!opt.returnObjects && !this.options.returnObjects) {
577
+ if (!this.options.returnedObjectHandler) {
578
+ this.logger.warn('accessing an object - but returnObjects options is not enabled!');
579
+ }
580
+ const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
581
+ ...opt,
582
+ ns: namespaces
583
+ }) : `key '${key} (${this.language})' returned an object instead of string.`;
584
+ if (returnDetails) {
585
+ resolved.res = r;
586
+ resolved.usedParams = this.getUsedParamsDetails(opt);
587
+ return resolved;
588
+ }
589
+ return r;
590
+ }
591
+ if (keySeparator) {
592
+ const resTypeIsArray = Array.isArray(resForObjHndl);
593
+ const copy = resTypeIsArray ? [] : {};
594
+ const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
595
+ for (const m in resForObjHndl) {
596
+ if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
597
+ const deepKey = `${newKeyToUse}${keySeparator}${m}`;
598
+ if (hasDefaultValue && !res) {
599
+ copy[m] = this.translate(deepKey, {
600
+ ...opt,
601
+ defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : undefined,
602
+ ...{
603
+ joinArrays: false,
604
+ ns: namespaces
605
+ }
606
+ });
607
+ } else {
608
+ copy[m] = this.translate(deepKey, {
609
+ ...opt,
610
+ ...{
611
+ joinArrays: false,
612
+ ns: namespaces
613
+ }
614
+ });
615
+ }
616
+ if (copy[m] === deepKey) copy[m] = resForObjHndl[m];
617
+ }
618
+ }
619
+ res = copy;
620
+ }
621
+ } else if (handleAsObjectInI18nFormat && isString$1(joinArrays) && Array.isArray(res)) {
622
+ res = res.join(joinArrays);
623
+ if (res) res = this.extendTranslation(res, keys, opt, lastKey);
624
+ } else {
625
+ let usedDefault = false;
626
+ let usedKey = false;
627
+ if (!this.isValidLookup(res) && hasDefaultValue) {
628
+ usedDefault = true;
629
+ res = defaultValue;
630
+ }
631
+ if (!this.isValidLookup(res)) {
632
+ usedKey = true;
633
+ res = key;
634
+ }
635
+ const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
636
+ const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
637
+ const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
638
+ if (usedKey || usedDefault || updateMissing) {
639
+ this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
640
+ if (keySeparator) {
641
+ const fk = this.resolve(key, {
642
+ ...opt,
643
+ keySeparator: false
644
+ });
645
+ 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.');
646
+ }
647
+ let lngs = [];
648
+ const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
649
+ if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
650
+ for (let i = 0; i < fallbackLngs.length; i++) {
651
+ lngs.push(fallbackLngs[i]);
652
+ }
653
+ } else if (this.options.saveMissingTo === 'all') {
654
+ lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
655
+ } else {
656
+ lngs.push(opt.lng || this.language);
657
+ }
658
+ const send = (l, k, specificDefaultValue) => {
659
+ const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
660
+ if (this.options.missingKeyHandler) {
661
+ this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
662
+ } else if (this.backendConnector?.saveMissing) {
663
+ this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
664
+ }
665
+ this.emit('missingKey', l, namespace, k, res);
666
+ };
667
+ if (this.options.saveMissing) {
668
+ if (this.options.saveMissingPlurals && needsPluralHandling) {
669
+ lngs.forEach(language => {
670
+ const suffixes = this.pluralResolver.getSuffixes(language, opt);
671
+ if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
672
+ suffixes.push(`${this.options.pluralSeparator}zero`);
673
+ }
674
+ suffixes.forEach(suffix => {
675
+ send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
676
+ });
677
+ });
678
+ } else {
679
+ send(lngs, key, defaultValue);
680
+ }
681
+ }
682
+ }
683
+ res = this.extendTranslation(res, keys, opt, resolved, lastKey);
684
+ if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
685
+ res = `${namespace}${nsSeparator}${key}`;
686
+ }
687
+ if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
688
+ res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : undefined, opt);
689
+ }
690
+ }
691
+ if (returnDetails) {
692
+ resolved.res = res;
693
+ resolved.usedParams = this.getUsedParamsDetails(opt);
694
+ return resolved;
695
+ }
696
+ return res;
697
+ }
698
+ extendTranslation(res, key, opt, resolved, lastKey) {
699
+ if (this.i18nFormat?.parse) {
700
+ res = this.i18nFormat.parse(res, {
701
+ ...this.options.interpolation.defaultVariables,
702
+ ...opt
703
+ }, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
704
+ resolved
705
+ });
706
+ } else if (!opt.skipInterpolation) {
707
+ if (opt.interpolation) this.interpolator.init({
708
+ ...opt,
709
+ ...{
710
+ interpolation: {
711
+ ...this.options.interpolation,
712
+ ...opt.interpolation
713
+ }
714
+ }
715
+ });
716
+ const skipOnVariables = isString$1(res) && (opt?.interpolation?.skipOnVariables !== undefined ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
717
+ let nestBef;
718
+ if (skipOnVariables) {
719
+ const nb = res.match(this.interpolator.nestingRegexp);
720
+ nestBef = nb && nb.length;
721
+ }
722
+ let data = opt.replace && !isString$1(opt.replace) ? opt.replace : opt;
723
+ if (this.options.interpolation.defaultVariables) data = {
724
+ ...this.options.interpolation.defaultVariables,
725
+ ...data
726
+ };
727
+ res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
728
+ if (skipOnVariables) {
729
+ const na = res.match(this.interpolator.nestingRegexp);
730
+ const nestAft = na && na.length;
731
+ if (nestBef < nestAft) opt.nest = false;
732
+ }
733
+ if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
734
+ if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
735
+ if (lastKey?.[0] === args[0] && !opt.context) {
736
+ this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
737
+ return null;
738
+ }
739
+ return this.translate(...args, key);
740
+ }, opt);
741
+ if (opt.interpolation) this.interpolator.reset();
742
+ }
743
+ const postProcess = opt.postProcess || this.options.postProcess;
744
+ const postProcessorNames = isString$1(postProcess) ? [postProcess] : postProcess;
745
+ if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
746
+ res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
747
+ i18nResolved: {
748
+ ...resolved,
749
+ usedParams: this.getUsedParamsDetails(opt)
750
+ },
751
+ ...opt
752
+ } : opt, this);
753
+ }
754
+ return res;
755
+ }
756
+ resolve(keys, opt = {}) {
757
+ let found;
758
+ let usedKey;
759
+ let exactUsedKey;
760
+ let usedLng;
761
+ let usedNS;
762
+ if (isString$1(keys)) keys = [keys];
763
+ keys.forEach(k => {
764
+ if (this.isValidLookup(found)) return;
765
+ const extracted = this.extractFromKey(k, opt);
766
+ const key = extracted.key;
767
+ usedKey = key;
768
+ let namespaces = extracted.namespaces;
769
+ if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
770
+ const needsPluralHandling = opt.count !== undefined && !isString$1(opt.count);
771
+ const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
772
+ const needsContextHandling = opt.context !== undefined && (isString$1(opt.context) || typeof opt.context === 'number') && opt.context !== '';
773
+ const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
774
+ namespaces.forEach(ns => {
775
+ if (this.isValidLookup(found)) return;
776
+ usedNS = ns;
777
+ if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
778
+ checkedLoadedFor[`${codes[0]}-${ns}`] = true;
779
+ 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!!!');
780
+ }
781
+ codes.forEach(code => {
782
+ if (this.isValidLookup(found)) return;
783
+ usedLng = code;
784
+ const finalKeys = [key];
785
+ if (this.i18nFormat?.addLookupKeys) {
786
+ this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
787
+ } else {
788
+ let pluralSuffix;
789
+ if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
790
+ const zeroSuffix = `${this.options.pluralSeparator}zero`;
791
+ const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
792
+ if (needsPluralHandling) {
793
+ if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
794
+ finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
795
+ }
796
+ finalKeys.push(key + pluralSuffix);
797
+ if (needsZeroSuffixLookup) {
798
+ finalKeys.push(key + zeroSuffix);
799
+ }
800
+ }
801
+ if (needsContextHandling) {
802
+ const contextKey = `${key}${this.options.contextSeparator || '_'}${opt.context}`;
803
+ finalKeys.push(contextKey);
804
+ if (needsPluralHandling) {
805
+ if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
806
+ finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
807
+ }
808
+ finalKeys.push(contextKey + pluralSuffix);
809
+ if (needsZeroSuffixLookup) {
810
+ finalKeys.push(contextKey + zeroSuffix);
811
+ }
812
+ }
813
+ }
814
+ }
815
+ let possibleKey;
816
+ while (possibleKey = finalKeys.pop()) {
817
+ if (!this.isValidLookup(found)) {
818
+ exactUsedKey = possibleKey;
819
+ found = this.getResource(code, ns, possibleKey, opt);
820
+ }
821
+ }
822
+ });
823
+ });
824
+ });
825
+ return {
826
+ res: found,
827
+ usedKey,
828
+ exactUsedKey,
829
+ usedLng,
830
+ usedNS
831
+ };
832
+ }
833
+ isValidLookup(res) {
834
+ return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
835
+ }
836
+ getResource(code, ns, key, options = {}) {
837
+ if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
838
+ return this.resourceStore.getResource(code, ns, key, options);
839
+ }
840
+ getUsedParamsDetails(options = {}) {
841
+ const optionsKeys = ['defaultValue', 'ordinal', 'context', 'replace', 'lng', 'lngs', 'fallbackLng', 'ns', 'keySeparator', 'nsSeparator', 'returnObjects', 'returnDetails', 'joinArrays', 'postProcess', 'interpolation'];
842
+ const useOptionsReplaceForData = options.replace && !isString$1(options.replace);
843
+ let data = useOptionsReplaceForData ? options.replace : options;
844
+ if (useOptionsReplaceForData && typeof options.count !== 'undefined') {
845
+ data.count = options.count;
846
+ }
847
+ if (this.options.interpolation.defaultVariables) {
848
+ data = {
849
+ ...this.options.interpolation.defaultVariables,
850
+ ...data
851
+ };
852
+ }
853
+ if (!useOptionsReplaceForData) {
854
+ data = {
855
+ ...data
856
+ };
857
+ for (const key of optionsKeys) {
858
+ delete data[key];
859
+ }
860
+ }
861
+ return data;
862
+ }
863
+ static hasDefaultValue(options) {
864
+ const prefix = 'defaultValue';
865
+ for (const option in options) {
866
+ if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && undefined !== options[option]) {
867
+ return true;
868
+ }
869
+ }
870
+ return false;
871
+ }
872
+ }
873
+ class LanguageUtil {
874
+ constructor(options) {
875
+ this.options = options;
876
+ this.supportedLngs = this.options.supportedLngs || false;
877
+ this.logger = baseLogger.create('languageUtils');
878
+ }
879
+ getScriptPartFromCode(code) {
880
+ code = getCleanedCode(code);
881
+ if (!code || code.indexOf('-') < 0) return null;
882
+ const p = code.split('-');
883
+ if (p.length === 2) return null;
884
+ p.pop();
885
+ if (p[p.length - 1].toLowerCase() === 'x') return null;
886
+ return this.formatLanguageCode(p.join('-'));
887
+ }
888
+ getLanguagePartFromCode(code) {
889
+ code = getCleanedCode(code);
890
+ if (!code || code.indexOf('-') < 0) return code;
891
+ const p = code.split('-');
892
+ return this.formatLanguageCode(p[0]);
893
+ }
894
+ formatLanguageCode(code) {
895
+ if (isString$1(code) && code.indexOf('-') > -1) {
896
+ let formattedCode;
897
+ try {
898
+ formattedCode = Intl.getCanonicalLocales(code)[0];
899
+ } catch (e) {}
900
+ if (formattedCode && this.options.lowerCaseLng) {
901
+ formattedCode = formattedCode.toLowerCase();
902
+ }
903
+ if (formattedCode) return formattedCode;
904
+ if (this.options.lowerCaseLng) {
905
+ return code.toLowerCase();
906
+ }
907
+ return code;
908
+ }
909
+ return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
910
+ }
911
+ isSupportedCode(code) {
912
+ if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
913
+ code = this.getLanguagePartFromCode(code);
914
+ }
915
+ return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
916
+ }
917
+ getBestMatchFromCodes(codes) {
918
+ if (!codes) return null;
919
+ let found;
920
+ codes.forEach(code => {
921
+ if (found) return;
922
+ const cleanedLng = this.formatLanguageCode(code);
923
+ if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
924
+ });
925
+ if (!found && this.options.supportedLngs) {
926
+ codes.forEach(code => {
927
+ if (found) return;
928
+ const lngScOnly = this.getScriptPartFromCode(code);
929
+ if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
930
+ const lngOnly = this.getLanguagePartFromCode(code);
931
+ if (this.isSupportedCode(lngOnly)) return found = lngOnly;
932
+ found = this.options.supportedLngs.find(supportedLng => {
933
+ if (supportedLng === lngOnly) return supportedLng;
934
+ if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
935
+ if (supportedLng.indexOf('-') > 0 && lngOnly.indexOf('-') < 0 && supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly) return supportedLng;
936
+ if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
937
+ });
938
+ });
939
+ }
940
+ if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
941
+ return found;
942
+ }
943
+ getFallbackCodes(fallbacks, code) {
944
+ if (!fallbacks) return [];
945
+ if (typeof fallbacks === 'function') fallbacks = fallbacks(code);
946
+ if (isString$1(fallbacks)) fallbacks = [fallbacks];
947
+ if (Array.isArray(fallbacks)) return fallbacks;
948
+ if (!code) return fallbacks.default || [];
949
+ let found = fallbacks[code];
950
+ if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
951
+ if (!found) found = fallbacks[this.formatLanguageCode(code)];
952
+ if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
953
+ if (!found) found = fallbacks.default;
954
+ return found || [];
955
+ }
956
+ toResolveHierarchy(code, fallbackCode) {
957
+ const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
958
+ const codes = [];
959
+ const addCode = c => {
960
+ if (!c) return;
961
+ if (this.isSupportedCode(c)) {
962
+ codes.push(c);
963
+ } else {
964
+ this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
965
+ }
966
+ };
967
+ if (isString$1(code) && (code.indexOf('-') > -1 || code.indexOf('_') > -1)) {
968
+ if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
969
+ if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
970
+ if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
971
+ } else if (isString$1(code)) {
972
+ addCode(this.formatLanguageCode(code));
973
+ }
974
+ fallbackCodes.forEach(fc => {
975
+ if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
976
+ });
977
+ return codes;
978
+ }
979
+ }
980
+ const suffixesOrder = {
981
+ zero: 0,
982
+ one: 1,
983
+ two: 2,
984
+ few: 3,
985
+ many: 4,
986
+ other: 5
987
+ };
988
+ const dummyRule = {
989
+ select: count => count === 1 ? 'one' : 'other',
990
+ resolvedOptions: () => ({
991
+ pluralCategories: ['one', 'other']
992
+ })
993
+ };
994
+ class PluralResolver {
995
+ constructor(languageUtils, options = {}) {
996
+ this.languageUtils = languageUtils;
997
+ this.options = options;
998
+ this.logger = baseLogger.create('pluralResolver');
999
+ this.pluralRulesCache = {};
1000
+ }
1001
+ addRule(lng, obj) {
1002
+ this.rules[lng] = obj;
1003
+ }
1004
+ clearCache() {
1005
+ this.pluralRulesCache = {};
1006
+ }
1007
+ getRule(code, options = {}) {
1008
+ const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
1009
+ const type = options.ordinal ? 'ordinal' : 'cardinal';
1010
+ const cacheKey = JSON.stringify({
1011
+ cleanedCode,
1012
+ type
1013
+ });
1014
+ if (cacheKey in this.pluralRulesCache) {
1015
+ return this.pluralRulesCache[cacheKey];
1016
+ }
1017
+ let rule;
1018
+ try {
1019
+ rule = new Intl.PluralRules(cleanedCode, {
1020
+ type
1021
+ });
1022
+ } catch (err) {
1023
+ if (!Intl) {
1024
+ this.logger.error('No Intl support, please use an Intl polyfill!');
1025
+ return dummyRule;
1026
+ }
1027
+ if (!code.match(/-|_/)) return dummyRule;
1028
+ const lngPart = this.languageUtils.getLanguagePartFromCode(code);
1029
+ rule = this.getRule(lngPart, options);
1030
+ }
1031
+ this.pluralRulesCache[cacheKey] = rule;
1032
+ return rule;
1033
+ }
1034
+ needsPlural(code, options = {}) {
1035
+ let rule = this.getRule(code, options);
1036
+ if (!rule) rule = this.getRule('dev', options);
1037
+ return rule?.resolvedOptions().pluralCategories.length > 1;
1038
+ }
1039
+ getPluralFormsOfKey(code, key, options = {}) {
1040
+ return this.getSuffixes(code, options).map(suffix => `${key}${suffix}`);
1041
+ }
1042
+ getSuffixes(code, options = {}) {
1043
+ let rule = this.getRule(code, options);
1044
+ if (!rule) rule = this.getRule('dev', options);
1045
+ if (!rule) return [];
1046
+ return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
1047
+ }
1048
+ getSuffix(code, count, options = {}) {
1049
+ const rule = this.getRule(code, options);
1050
+ if (rule) {
1051
+ return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
1052
+ }
1053
+ this.logger.warn(`no plural rule found for: ${code}`);
1054
+ return this.getSuffix('dev', count, options);
1055
+ }
1056
+ }
1057
+ const deepFindWithDefaults = (data, defaultData, key, keySeparator = '.', ignoreJSONStructure = true) => {
1058
+ let path = getPathWithDefaults(data, defaultData, key);
1059
+ if (!path && ignoreJSONStructure && isString$1(key)) {
1060
+ path = deepFind(data, key, keySeparator);
1061
+ if (path === undefined) path = deepFind(defaultData, key, keySeparator);
1062
+ }
1063
+ return path;
1064
+ };
1065
+ const regexSafe = val => val.replace(/\$/g, '$$$$');
1066
+ class Interpolator {
1067
+ constructor(options = {}) {
1068
+ this.logger = baseLogger.create('interpolator');
1069
+ this.options = options;
1070
+ this.format = options?.interpolation?.format || (value => value);
1071
+ this.init(options);
1072
+ }
1073
+ init(options = {}) {
1074
+ if (!options.interpolation) options.interpolation = {
1075
+ escapeValue: true
1076
+ };
1077
+ const {
1078
+ escape: escape$1,
1079
+ escapeValue,
1080
+ useRawValueToEscape,
1081
+ prefix,
1082
+ prefixEscaped,
1083
+ suffix,
1084
+ suffixEscaped,
1085
+ formatSeparator,
1086
+ unescapeSuffix,
1087
+ unescapePrefix,
1088
+ nestingPrefix,
1089
+ nestingPrefixEscaped,
1090
+ nestingSuffix,
1091
+ nestingSuffixEscaped,
1092
+ nestingOptionsSeparator,
1093
+ maxReplaces,
1094
+ alwaysFormat
1095
+ } = options.interpolation;
1096
+ this.escape = escape$1 !== undefined ? escape$1 : escape;
1097
+ this.escapeValue = escapeValue !== undefined ? escapeValue : true;
1098
+ this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
1099
+ this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || '{{';
1100
+ this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || '}}';
1101
+ this.formatSeparator = formatSeparator || ',';
1102
+ this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix || '-';
1103
+ this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix || '';
1104
+ this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape('$t(');
1105
+ this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(')');
1106
+ this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
1107
+ this.maxReplaces = maxReplaces || 1000;
1108
+ this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
1109
+ this.resetRegExp();
1110
+ }
1111
+ reset() {
1112
+ if (this.options) this.init(this.options);
1113
+ }
1114
+ resetRegExp() {
1115
+ const getOrResetRegExp = (existingRegExp, pattern) => {
1116
+ if (existingRegExp?.source === pattern) {
1117
+ existingRegExp.lastIndex = 0;
1118
+ return existingRegExp;
1119
+ }
1120
+ return new RegExp(pattern, 'g');
1121
+ };
1122
+ this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
1123
+ this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
1124
+ this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}((?:[^()"']+|"[^"]*"|'[^']*'|\\((?:[^()]|"[^"]*"|'[^']*')*\\))*?)${this.nestingSuffix}`);
1125
+ }
1126
+ interpolate(str, data, lng, options) {
1127
+ let match;
1128
+ let value;
1129
+ let replaces;
1130
+ const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
1131
+ const handleFormat = key => {
1132
+ if (key.indexOf(this.formatSeparator) < 0) {
1133
+ const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
1134
+ return this.alwaysFormat ? this.format(path, undefined, lng, {
1135
+ ...options,
1136
+ ...data,
1137
+ interpolationkey: key
1138
+ }) : path;
1139
+ }
1140
+ const p = key.split(this.formatSeparator);
1141
+ const k = p.shift().trim();
1142
+ const f = p.join(this.formatSeparator).trim();
1143
+ return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
1144
+ ...options,
1145
+ ...data,
1146
+ interpolationkey: k
1147
+ });
1148
+ };
1149
+ this.resetRegExp();
1150
+ const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
1151
+ const skipOnVariables = options?.interpolation?.skipOnVariables !== undefined ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
1152
+ const todos = [{
1153
+ regex: this.regexpUnescape,
1154
+ safeValue: val => regexSafe(val)
1155
+ }, {
1156
+ regex: this.regexp,
1157
+ safeValue: val => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
1158
+ }];
1159
+ todos.forEach(todo => {
1160
+ replaces = 0;
1161
+ while (match = todo.regex.exec(str)) {
1162
+ const matchedVar = match[1].trim();
1163
+ value = handleFormat(matchedVar);
1164
+ if (value === undefined) {
1165
+ if (typeof missingInterpolationHandler === 'function') {
1166
+ const temp = missingInterpolationHandler(str, match, options);
1167
+ value = isString$1(temp) ? temp : '';
1168
+ } else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
1169
+ value = '';
1170
+ } else if (skipOnVariables) {
1171
+ value = match[0];
1172
+ continue;
1173
+ } else {
1174
+ this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
1175
+ value = '';
1176
+ }
1177
+ } else if (!isString$1(value) && !this.useRawValueToEscape) {
1178
+ value = makeString(value);
1179
+ }
1180
+ const safeValue = todo.safeValue(value);
1181
+ str = str.replace(match[0], safeValue);
1182
+ if (skipOnVariables) {
1183
+ todo.regex.lastIndex += value.length;
1184
+ todo.regex.lastIndex -= match[0].length;
1185
+ } else {
1186
+ todo.regex.lastIndex = 0;
1187
+ }
1188
+ replaces++;
1189
+ if (replaces >= this.maxReplaces) {
1190
+ break;
1191
+ }
1192
+ }
1193
+ });
1194
+ return str;
1195
+ }
1196
+ nest(str, fc, options = {}) {
1197
+ let match;
1198
+ let value;
1199
+ let clonedOptions;
1200
+ const handleHasOptions = (key, inheritedOptions) => {
1201
+ const sep = this.nestingOptionsSeparator;
1202
+ if (key.indexOf(sep) < 0) return key;
1203
+ const c = key.split(new RegExp(`${sep}[ ]*{`));
1204
+ let optionsString = `{${c[1]}`;
1205
+ key = c[0];
1206
+ optionsString = this.interpolate(optionsString, clonedOptions);
1207
+ const matchedSingleQuotes = optionsString.match(/'/g);
1208
+ const matchedDoubleQuotes = optionsString.match(/"/g);
1209
+ if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
1210
+ optionsString = optionsString.replace(/'/g, '"');
1211
+ }
1212
+ try {
1213
+ clonedOptions = JSON.parse(optionsString);
1214
+ if (inheritedOptions) clonedOptions = {
1215
+ ...inheritedOptions,
1216
+ ...clonedOptions
1217
+ };
1218
+ } catch (e) {
1219
+ this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
1220
+ return `${key}${sep}${optionsString}`;
1221
+ }
1222
+ if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
1223
+ return key;
1224
+ };
1225
+ while (match = this.nestingRegexp.exec(str)) {
1226
+ let formatters = [];
1227
+ clonedOptions = {
1228
+ ...options
1229
+ };
1230
+ clonedOptions = clonedOptions.replace && !isString$1(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
1231
+ clonedOptions.applyPostProcessor = false;
1232
+ delete clonedOptions.defaultValue;
1233
+ const keyEndIndex = /{.*}/.test(match[1]) ? match[1].lastIndexOf('}') + 1 : match[1].indexOf(this.formatSeparator);
1234
+ if (keyEndIndex !== -1) {
1235
+ formatters = match[1].slice(keyEndIndex).split(this.formatSeparator).map(elem => elem.trim()).filter(Boolean);
1236
+ match[1] = match[1].slice(0, keyEndIndex);
1237
+ }
1238
+ value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
1239
+ if (value && match[0] === str && !isString$1(value)) return value;
1240
+ if (!isString$1(value)) value = makeString(value);
1241
+ if (!value) {
1242
+ this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
1243
+ value = '';
1244
+ }
1245
+ if (formatters.length) {
1246
+ value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
1247
+ ...options,
1248
+ interpolationkey: match[1].trim()
1249
+ }), value.trim());
1250
+ }
1251
+ str = str.replace(match[0], value);
1252
+ this.regexp.lastIndex = 0;
1253
+ }
1254
+ return str;
1255
+ }
1256
+ }
1257
+ const parseFormatStr = formatStr => {
1258
+ let formatName = formatStr.toLowerCase().trim();
1259
+ const formatOptions = {};
1260
+ if (formatStr.indexOf('(') > -1) {
1261
+ const p = formatStr.split('(');
1262
+ formatName = p[0].toLowerCase().trim();
1263
+ const optStr = p[1].substring(0, p[1].length - 1);
1264
+ if (formatName === 'currency' && optStr.indexOf(':') < 0) {
1265
+ if (!formatOptions.currency) formatOptions.currency = optStr.trim();
1266
+ } else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
1267
+ if (!formatOptions.range) formatOptions.range = optStr.trim();
1268
+ } else {
1269
+ const opts = optStr.split(';');
1270
+ opts.forEach(opt => {
1271
+ if (opt) {
1272
+ const [key, ...rest] = opt.split(':');
1273
+ const val = rest.join(':').trim().replace(/^'+|'+$/g, '');
1274
+ const trimmedKey = key.trim();
1275
+ if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
1276
+ if (val === 'false') formatOptions[trimmedKey] = false;
1277
+ if (val === 'true') formatOptions[trimmedKey] = true;
1278
+ if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
1279
+ }
1280
+ });
1281
+ }
1282
+ }
1283
+ return {
1284
+ formatName,
1285
+ formatOptions
1286
+ };
1287
+ };
1288
+ const createCachedFormatter = fn => {
1289
+ const cache = {};
1290
+ return (v, l, o) => {
1291
+ let optForCache = o;
1292
+ if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
1293
+ optForCache = {
1294
+ ...optForCache,
1295
+ [o.interpolationkey]: undefined
1296
+ };
1297
+ }
1298
+ const key = l + JSON.stringify(optForCache);
1299
+ let frm = cache[key];
1300
+ if (!frm) {
1301
+ frm = fn(getCleanedCode(l), o);
1302
+ cache[key] = frm;
1303
+ }
1304
+ return frm(v);
1305
+ };
1306
+ };
1307
+ const createNonCachedFormatter = fn => (v, l, o) => fn(getCleanedCode(l), o)(v);
1308
+ class Formatter {
1309
+ constructor(options = {}) {
1310
+ this.logger = baseLogger.create('formatter');
1311
+ this.options = options;
1312
+ this.init(options);
1313
+ }
1314
+ init(services, options = {
1315
+ interpolation: {}
1316
+ }) {
1317
+ this.formatSeparator = options.interpolation.formatSeparator || ',';
1318
+ const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
1319
+ this.formats = {
1320
+ number: cf((lng, opt) => {
1321
+ const formatter = new Intl.NumberFormat(lng, {
1322
+ ...opt
1323
+ });
1324
+ return val => formatter.format(val);
1325
+ }),
1326
+ currency: cf((lng, opt) => {
1327
+ const formatter = new Intl.NumberFormat(lng, {
1328
+ ...opt,
1329
+ style: 'currency'
1330
+ });
1331
+ return val => formatter.format(val);
1332
+ }),
1333
+ datetime: cf((lng, opt) => {
1334
+ const formatter = new Intl.DateTimeFormat(lng, {
1335
+ ...opt
1336
+ });
1337
+ return val => formatter.format(val);
1338
+ }),
1339
+ relativetime: cf((lng, opt) => {
1340
+ const formatter = new Intl.RelativeTimeFormat(lng, {
1341
+ ...opt
1342
+ });
1343
+ return val => formatter.format(val, opt.range || 'day');
1344
+ }),
1345
+ list: cf((lng, opt) => {
1346
+ const formatter = new Intl.ListFormat(lng, {
1347
+ ...opt
1348
+ });
1349
+ return val => formatter.format(val);
1350
+ })
1351
+ };
1352
+ }
1353
+ add(name, fc) {
1354
+ this.formats[name.toLowerCase().trim()] = fc;
1355
+ }
1356
+ addCached(name, fc) {
1357
+ this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
1358
+ }
1359
+ format(value, format, lng, options = {}) {
1360
+ const formats = format.split(this.formatSeparator);
1361
+ if (formats.length > 1 && formats[0].indexOf('(') > 1 && formats[0].indexOf(')') < 0 && formats.find(f => f.indexOf(')') > -1)) {
1362
+ const lastIndex = formats.findIndex(f => f.indexOf(')') > -1);
1363
+ formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
1364
+ }
1365
+ const result = formats.reduce((mem, f) => {
1366
+ const {
1367
+ formatName,
1368
+ formatOptions
1369
+ } = parseFormatStr(f);
1370
+ if (this.formats[formatName]) {
1371
+ let formatted = mem;
1372
+ try {
1373
+ const valOptions = options?.formatParams?.[options.interpolationkey] || {};
1374
+ const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
1375
+ formatted = this.formats[formatName](mem, l, {
1376
+ ...formatOptions,
1377
+ ...options,
1378
+ ...valOptions
1379
+ });
1380
+ } catch (error) {
1381
+ this.logger.warn(error);
1382
+ }
1383
+ return formatted;
1384
+ } else {
1385
+ this.logger.warn(`there was no format function for ${formatName}`);
1386
+ }
1387
+ return mem;
1388
+ }, value);
1389
+ return result;
1390
+ }
1391
+ }
1392
+ const removePending = (q, name) => {
1393
+ if (q.pending[name] !== undefined) {
1394
+ delete q.pending[name];
1395
+ q.pendingCount--;
1396
+ }
1397
+ };
1398
+ class Connector extends EventEmitter {
1399
+ constructor(backend, store, services, options = {}) {
1400
+ super();
1401
+ this.backend = backend;
1402
+ this.store = store;
1403
+ this.services = services;
1404
+ this.languageUtils = services.languageUtils;
1405
+ this.options = options;
1406
+ this.logger = baseLogger.create('backendConnector');
1407
+ this.waitingReads = [];
1408
+ this.maxParallelReads = options.maxParallelReads || 10;
1409
+ this.readingCalls = 0;
1410
+ this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
1411
+ this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
1412
+ this.state = {};
1413
+ this.queue = [];
1414
+ this.backend?.init?.(services, options.backend, options);
1415
+ }
1416
+ queueLoad(languages, namespaces, options, callback) {
1417
+ const toLoad = {};
1418
+ const pending = {};
1419
+ const toLoadLanguages = {};
1420
+ const toLoadNamespaces = {};
1421
+ languages.forEach(lng => {
1422
+ let hasAllNamespaces = true;
1423
+ namespaces.forEach(ns => {
1424
+ const name = `${lng}|${ns}`;
1425
+ if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
1426
+ this.state[name] = 2;
1427
+ } else if (this.state[name] < 0) ;else if (this.state[name] === 1) {
1428
+ if (pending[name] === undefined) pending[name] = true;
1429
+ } else {
1430
+ this.state[name] = 1;
1431
+ hasAllNamespaces = false;
1432
+ if (pending[name] === undefined) pending[name] = true;
1433
+ if (toLoad[name] === undefined) toLoad[name] = true;
1434
+ if (toLoadNamespaces[ns] === undefined) toLoadNamespaces[ns] = true;
1435
+ }
1436
+ });
1437
+ if (!hasAllNamespaces) toLoadLanguages[lng] = true;
1438
+ });
1439
+ if (Object.keys(toLoad).length || Object.keys(pending).length) {
1440
+ this.queue.push({
1441
+ pending,
1442
+ pendingCount: Object.keys(pending).length,
1443
+ loaded: {},
1444
+ errors: [],
1445
+ callback
1446
+ });
1447
+ }
1448
+ return {
1449
+ toLoad: Object.keys(toLoad),
1450
+ pending: Object.keys(pending),
1451
+ toLoadLanguages: Object.keys(toLoadLanguages),
1452
+ toLoadNamespaces: Object.keys(toLoadNamespaces)
1453
+ };
1454
+ }
1455
+ loaded(name, err, data) {
1456
+ const s = name.split('|');
1457
+ const lng = s[0];
1458
+ const ns = s[1];
1459
+ if (err) this.emit('failedLoading', lng, ns, err);
1460
+ if (!err && data) {
1461
+ this.store.addResourceBundle(lng, ns, data, undefined, undefined, {
1462
+ skipCopy: true
1463
+ });
1464
+ }
1465
+ this.state[name] = err ? -1 : 2;
1466
+ if (err && data) this.state[name] = 0;
1467
+ const loaded = {};
1468
+ this.queue.forEach(q => {
1469
+ pushPath(q.loaded, [lng], ns);
1470
+ removePending(q, name);
1471
+ if (err) q.errors.push(err);
1472
+ if (q.pendingCount === 0 && !q.done) {
1473
+ Object.keys(q.loaded).forEach(l => {
1474
+ if (!loaded[l]) loaded[l] = {};
1475
+ const loadedKeys = q.loaded[l];
1476
+ if (loadedKeys.length) {
1477
+ loadedKeys.forEach(n => {
1478
+ if (loaded[l][n] === undefined) loaded[l][n] = true;
1479
+ });
1480
+ }
1481
+ });
1482
+ q.done = true;
1483
+ if (q.errors.length) {
1484
+ q.callback(q.errors);
1485
+ } else {
1486
+ q.callback();
1487
+ }
1488
+ }
1489
+ });
1490
+ this.emit('loaded', loaded);
1491
+ this.queue = this.queue.filter(q => !q.done);
1492
+ }
1493
+ read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
1494
+ if (!lng.length) return callback(null, {});
1495
+ if (this.readingCalls >= this.maxParallelReads) {
1496
+ this.waitingReads.push({
1497
+ lng,
1498
+ ns,
1499
+ fcName,
1500
+ tried,
1501
+ wait,
1502
+ callback
1503
+ });
1504
+ return;
1505
+ }
1506
+ this.readingCalls++;
1507
+ const resolver = (err, data) => {
1508
+ this.readingCalls--;
1509
+ if (this.waitingReads.length > 0) {
1510
+ const next = this.waitingReads.shift();
1511
+ this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
1512
+ }
1513
+ if (err && data && tried < this.maxRetries) {
1514
+ setTimeout(() => {
1515
+ this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
1516
+ }, wait);
1517
+ return;
1518
+ }
1519
+ callback(err, data);
1520
+ };
1521
+ const fc = this.backend[fcName].bind(this.backend);
1522
+ if (fc.length === 2) {
1523
+ try {
1524
+ const r = fc(lng, ns);
1525
+ if (r && typeof r.then === 'function') {
1526
+ r.then(data => resolver(null, data)).catch(resolver);
1527
+ } else {
1528
+ resolver(null, r);
1529
+ }
1530
+ } catch (err) {
1531
+ resolver(err);
1532
+ }
1533
+ return;
1534
+ }
1535
+ return fc(lng, ns, resolver);
1536
+ }
1537
+ prepareLoading(languages, namespaces, options = {}, callback) {
1538
+ if (!this.backend) {
1539
+ this.logger.warn('No backend was added via i18next.use. Will not load resources.');
1540
+ return callback && callback();
1541
+ }
1542
+ if (isString$1(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
1543
+ if (isString$1(namespaces)) namespaces = [namespaces];
1544
+ const toLoad = this.queueLoad(languages, namespaces, options, callback);
1545
+ if (!toLoad.toLoad.length) {
1546
+ if (!toLoad.pending.length) callback();
1547
+ return null;
1548
+ }
1549
+ toLoad.toLoad.forEach(name => {
1550
+ this.loadOne(name);
1551
+ });
1552
+ }
1553
+ load(languages, namespaces, callback) {
1554
+ this.prepareLoading(languages, namespaces, {}, callback);
1555
+ }
1556
+ reload(languages, namespaces, callback) {
1557
+ this.prepareLoading(languages, namespaces, {
1558
+ reload: true
1559
+ }, callback);
1560
+ }
1561
+ loadOne(name, prefix = '') {
1562
+ const s = name.split('|');
1563
+ const lng = s[0];
1564
+ const ns = s[1];
1565
+ this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
1566
+ if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
1567
+ if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
1568
+ this.loaded(name, err, data);
1569
+ });
1570
+ }
1571
+ saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
1572
+ if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
1573
+ 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!!!');
1574
+ return;
1575
+ }
1576
+ if (key === undefined || key === null || key === '') return;
1577
+ if (this.backend?.create) {
1578
+ const opts = {
1579
+ ...options,
1580
+ isUpdate
1581
+ };
1582
+ const fc = this.backend.create.bind(this.backend);
1583
+ if (fc.length < 6) {
1584
+ try {
1585
+ let r;
1586
+ if (fc.length === 5) {
1587
+ r = fc(languages, namespace, key, fallbackValue, opts);
1588
+ } else {
1589
+ r = fc(languages, namespace, key, fallbackValue);
1590
+ }
1591
+ if (r && typeof r.then === 'function') {
1592
+ r.then(data => clb(null, data)).catch(clb);
1593
+ } else {
1594
+ clb(null, r);
1595
+ }
1596
+ } catch (err) {
1597
+ clb(err);
1598
+ }
1599
+ } else {
1600
+ fc(languages, namespace, key, fallbackValue, clb, opts);
1601
+ }
1602
+ }
1603
+ if (!languages || !languages[0]) return;
1604
+ this.store.addResource(languages[0], namespace, key, fallbackValue);
1605
+ }
1606
+ }
1607
+ const get = () => ({
1608
+ debug: false,
1609
+ initAsync: true,
1610
+ ns: ['translation'],
1611
+ defaultNS: ['translation'],
1612
+ fallbackLng: ['dev'],
1613
+ fallbackNS: false,
1614
+ supportedLngs: false,
1615
+ nonExplicitSupportedLngs: false,
1616
+ load: 'all',
1617
+ preload: false,
1618
+ simplifyPluralSuffix: true,
1619
+ keySeparator: '.',
1620
+ nsSeparator: ':',
1621
+ pluralSeparator: '_',
1622
+ contextSeparator: '_',
1623
+ partialBundledLanguages: false,
1624
+ saveMissing: false,
1625
+ updateMissing: false,
1626
+ saveMissingTo: 'fallback',
1627
+ saveMissingPlurals: true,
1628
+ missingKeyHandler: false,
1629
+ missingInterpolationHandler: false,
1630
+ postProcess: false,
1631
+ postProcessPassResolved: false,
1632
+ returnNull: false,
1633
+ returnEmptyString: true,
1634
+ returnObjects: false,
1635
+ joinArrays: false,
1636
+ returnedObjectHandler: false,
1637
+ parseMissingKeyHandler: false,
1638
+ appendNamespaceToMissingKey: false,
1639
+ appendNamespaceToCIMode: false,
1640
+ overloadTranslationOptionHandler: args => {
1641
+ let ret = {};
1642
+ if (typeof args[1] === 'object') ret = args[1];
1643
+ if (isString$1(args[1])) ret.defaultValue = args[1];
1644
+ if (isString$1(args[2])) ret.tDescription = args[2];
1645
+ if (typeof args[2] === 'object' || typeof args[3] === 'object') {
1646
+ const options = args[3] || args[2];
1647
+ Object.keys(options).forEach(key => {
1648
+ ret[key] = options[key];
1649
+ });
1650
+ }
1651
+ return ret;
1652
+ },
1653
+ interpolation: {
1654
+ escapeValue: true,
1655
+ format: value => value,
1656
+ prefix: '{{',
1657
+ suffix: '}}',
1658
+ formatSeparator: ',',
1659
+ unescapePrefix: '-',
1660
+ nestingPrefix: '$t(',
1661
+ nestingSuffix: ')',
1662
+ nestingOptionsSeparator: ',',
1663
+ maxReplaces: 1000,
1664
+ skipOnVariables: true
1665
+ },
1666
+ cacheInBuiltFormats: true
1667
+ });
1668
+ const transformOptions = options => {
1669
+ if (isString$1(options.ns)) options.ns = [options.ns];
1670
+ if (isString$1(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
1671
+ if (isString$1(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
1672
+ if (options.supportedLngs?.indexOf?.('cimode') < 0) {
1673
+ options.supportedLngs = options.supportedLngs.concat(['cimode']);
1674
+ }
1675
+ if (typeof options.initImmediate === 'boolean') options.initAsync = options.initImmediate;
1676
+ return options;
1677
+ };
1678
+ const noop = () => {};
1679
+ const bindMemberFunctions = inst => {
1680
+ const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
1681
+ mems.forEach(mem => {
1682
+ if (typeof inst[mem] === 'function') {
1683
+ inst[mem] = inst[mem].bind(inst);
1684
+ }
1685
+ });
1686
+ };
1687
+ class I18n extends EventEmitter {
1688
+ constructor(options = {}, callback) {
1689
+ super();
1690
+ this.options = transformOptions(options);
1691
+ this.services = {};
1692
+ this.logger = baseLogger;
1693
+ this.modules = {
1694
+ external: []
1695
+ };
1696
+ bindMemberFunctions(this);
1697
+ if (callback && !this.isInitialized && !options.isClone) {
1698
+ if (!this.options.initAsync) {
1699
+ this.init(options, callback);
1700
+ return this;
1701
+ }
1702
+ setTimeout(() => {
1703
+ this.init(options, callback);
1704
+ }, 0);
1705
+ }
1706
+ }
1707
+ init(options = {}, callback) {
1708
+ this.isInitializing = true;
1709
+ if (typeof options === 'function') {
1710
+ callback = options;
1711
+ options = {};
1712
+ }
1713
+ if (options.defaultNS == null && options.ns) {
1714
+ if (isString$1(options.ns)) {
1715
+ options.defaultNS = options.ns;
1716
+ } else if (options.ns.indexOf('translation') < 0) {
1717
+ options.defaultNS = options.ns[0];
1718
+ }
1719
+ }
1720
+ const defOpts = get();
1721
+ this.options = {
1722
+ ...defOpts,
1723
+ ...this.options,
1724
+ ...transformOptions(options)
1725
+ };
1726
+ this.options.interpolation = {
1727
+ ...defOpts.interpolation,
1728
+ ...this.options.interpolation
1729
+ };
1730
+ if (options.keySeparator !== undefined) {
1731
+ this.options.userDefinedKeySeparator = options.keySeparator;
1732
+ }
1733
+ if (options.nsSeparator !== undefined) {
1734
+ this.options.userDefinedNsSeparator = options.nsSeparator;
1735
+ }
1736
+ const createClassOnDemand = ClassOrObject => {
1737
+ if (!ClassOrObject) return null;
1738
+ if (typeof ClassOrObject === 'function') return new ClassOrObject();
1739
+ return ClassOrObject;
1740
+ };
1741
+ if (!this.options.isClone) {
1742
+ if (this.modules.logger) {
1743
+ baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
1744
+ } else {
1745
+ baseLogger.init(null, this.options);
1746
+ }
1747
+ let formatter;
1748
+ if (this.modules.formatter) {
1749
+ formatter = this.modules.formatter;
1750
+ } else {
1751
+ formatter = Formatter;
1752
+ }
1753
+ const lu = new LanguageUtil(this.options);
1754
+ this.store = new ResourceStore(this.options.resources, this.options);
1755
+ const s = this.services;
1756
+ s.logger = baseLogger;
1757
+ s.resourceStore = this.store;
1758
+ s.languageUtils = lu;
1759
+ s.pluralResolver = new PluralResolver(lu, {
1760
+ prepend: this.options.pluralSeparator,
1761
+ simplifyPluralSuffix: this.options.simplifyPluralSuffix
1762
+ });
1763
+ const usingLegacyFormatFunction = this.options.interpolation.format && this.options.interpolation.format !== defOpts.interpolation.format;
1764
+ if (usingLegacyFormatFunction) {
1765
+ this.logger.deprecate(`init: you are still using the legacy format function, please use the new approach: https://www.i18next.com/translation-function/formatting`);
1766
+ }
1767
+ if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
1768
+ s.formatter = createClassOnDemand(formatter);
1769
+ if (s.formatter.init) s.formatter.init(s, this.options);
1770
+ this.options.interpolation.format = s.formatter.format.bind(s.formatter);
1771
+ }
1772
+ s.interpolator = new Interpolator(this.options);
1773
+ s.utils = {
1774
+ hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
1775
+ };
1776
+ s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
1777
+ s.backendConnector.on('*', (event, ...args) => {
1778
+ this.emit(event, ...args);
1779
+ });
1780
+ if (this.modules.languageDetector) {
1781
+ s.languageDetector = createClassOnDemand(this.modules.languageDetector);
1782
+ if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
1783
+ }
1784
+ if (this.modules.i18nFormat) {
1785
+ s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
1786
+ if (s.i18nFormat.init) s.i18nFormat.init(this);
1787
+ }
1788
+ this.translator = new Translator(this.services, this.options);
1789
+ this.translator.on('*', (event, ...args) => {
1790
+ this.emit(event, ...args);
1791
+ });
1792
+ this.modules.external.forEach(m => {
1793
+ if (m.init) m.init(this);
1794
+ });
1795
+ }
1796
+ this.format = this.options.interpolation.format;
1797
+ if (!callback) callback = noop;
1798
+ if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
1799
+ const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1800
+ if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
1801
+ }
1802
+ if (!this.services.languageDetector && !this.options.lng) {
1803
+ this.logger.warn('init: no languageDetector is used and no lng is defined');
1804
+ }
1805
+ const storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
1806
+ storeApi.forEach(fcName => {
1807
+ this[fcName] = (...args) => this.store[fcName](...args);
1808
+ });
1809
+ const storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
1810
+ storeApiChained.forEach(fcName => {
1811
+ this[fcName] = (...args) => {
1812
+ this.store[fcName](...args);
1813
+ return this;
1814
+ };
1815
+ });
1816
+ const deferred = defer();
1817
+ const load = () => {
1818
+ const finish = (err, t) => {
1819
+ this.isInitializing = false;
1820
+ if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
1821
+ this.isInitialized = true;
1822
+ if (!this.options.isClone) this.logger.log('initialized', this.options);
1823
+ this.emit('initialized', this.options);
1824
+ deferred.resolve(t);
1825
+ callback(err, t);
1826
+ };
1827
+ if (this.languages && !this.isInitialized) return finish(null, this.t.bind(this));
1828
+ this.changeLanguage(this.options.lng, finish);
1829
+ };
1830
+ if (this.options.resources || !this.options.initAsync) {
1831
+ load();
1832
+ } else {
1833
+ setTimeout(load, 0);
1834
+ }
1835
+ return deferred;
1836
+ }
1837
+ loadResources(language, callback = noop) {
1838
+ let usedCallback = callback;
1839
+ const usedLng = isString$1(language) ? language : this.language;
1840
+ if (typeof language === 'function') usedCallback = language;
1841
+ if (!this.options.resources || this.options.partialBundledLanguages) {
1842
+ if (usedLng?.toLowerCase() === 'cimode' && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
1843
+ const toLoad = [];
1844
+ const append = lng => {
1845
+ if (!lng) return;
1846
+ if (lng === 'cimode') return;
1847
+ const lngs = this.services.languageUtils.toResolveHierarchy(lng);
1848
+ lngs.forEach(l => {
1849
+ if (l === 'cimode') return;
1850
+ if (toLoad.indexOf(l) < 0) toLoad.push(l);
1851
+ });
1852
+ };
1853
+ if (!usedLng) {
1854
+ const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1855
+ fallbacks.forEach(l => append(l));
1856
+ } else {
1857
+ append(usedLng);
1858
+ }
1859
+ this.options.preload?.forEach?.(l => append(l));
1860
+ this.services.backendConnector.load(toLoad, this.options.ns, e => {
1861
+ if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
1862
+ usedCallback(e);
1863
+ });
1864
+ } else {
1865
+ usedCallback(null);
1866
+ }
1867
+ }
1868
+ reloadResources(lngs, ns, callback) {
1869
+ const deferred = defer();
1870
+ if (typeof lngs === 'function') {
1871
+ callback = lngs;
1872
+ lngs = undefined;
1873
+ }
1874
+ if (typeof ns === 'function') {
1875
+ callback = ns;
1876
+ ns = undefined;
1877
+ }
1878
+ if (!lngs) lngs = this.languages;
1879
+ if (!ns) ns = this.options.ns;
1880
+ if (!callback) callback = noop;
1881
+ this.services.backendConnector.reload(lngs, ns, err => {
1882
+ deferred.resolve();
1883
+ callback(err);
1884
+ });
1885
+ return deferred;
1886
+ }
1887
+ use(module) {
1888
+ if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
1889
+ if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
1890
+ if (module.type === 'backend') {
1891
+ this.modules.backend = module;
1892
+ }
1893
+ if (module.type === 'logger' || module.log && module.warn && module.error) {
1894
+ this.modules.logger = module;
1895
+ }
1896
+ if (module.type === 'languageDetector') {
1897
+ this.modules.languageDetector = module;
1898
+ }
1899
+ if (module.type === 'i18nFormat') {
1900
+ this.modules.i18nFormat = module;
1901
+ }
1902
+ if (module.type === 'postProcessor') {
1903
+ postProcessor.addPostProcessor(module);
1904
+ }
1905
+ if (module.type === 'formatter') {
1906
+ this.modules.formatter = module;
1907
+ }
1908
+ if (module.type === '3rdParty') {
1909
+ this.modules.external.push(module);
1910
+ }
1911
+ return this;
1912
+ }
1913
+ setResolvedLanguage(l) {
1914
+ if (!l || !this.languages) return;
1915
+ if (['cimode', 'dev'].indexOf(l) > -1) return;
1916
+ for (let li = 0; li < this.languages.length; li++) {
1917
+ const lngInLngs = this.languages[li];
1918
+ if (['cimode', 'dev'].indexOf(lngInLngs) > -1) continue;
1919
+ if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
1920
+ this.resolvedLanguage = lngInLngs;
1921
+ break;
1922
+ }
1923
+ }
1924
+ if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
1925
+ this.resolvedLanguage = l;
1926
+ this.languages.unshift(l);
1927
+ }
1928
+ }
1929
+ changeLanguage(lng, callback) {
1930
+ this.isLanguageChangingTo = lng;
1931
+ const deferred = defer();
1932
+ this.emit('languageChanging', lng);
1933
+ const setLngProps = l => {
1934
+ this.language = l;
1935
+ this.languages = this.services.languageUtils.toResolveHierarchy(l);
1936
+ this.resolvedLanguage = undefined;
1937
+ this.setResolvedLanguage(l);
1938
+ };
1939
+ const done = (err, l) => {
1940
+ if (l) {
1941
+ if (this.isLanguageChangingTo === lng) {
1942
+ setLngProps(l);
1943
+ this.translator.changeLanguage(l);
1944
+ this.isLanguageChangingTo = undefined;
1945
+ this.emit('languageChanged', l);
1946
+ this.logger.log('languageChanged', l);
1947
+ }
1948
+ } else {
1949
+ this.isLanguageChangingTo = undefined;
1950
+ }
1951
+ deferred.resolve((...args) => this.t(...args));
1952
+ if (callback) callback(err, (...args) => this.t(...args));
1953
+ };
1954
+ const setLng = lngs => {
1955
+ if (!lng && !lngs && this.services.languageDetector) lngs = [];
1956
+ const fl = isString$1(lngs) ? lngs : lngs && lngs[0];
1957
+ const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString$1(lngs) ? [lngs] : lngs);
1958
+ if (l) {
1959
+ if (!this.language) {
1960
+ setLngProps(l);
1961
+ }
1962
+ if (!this.translator.language) this.translator.changeLanguage(l);
1963
+ this.services.languageDetector?.cacheUserLanguage?.(l);
1964
+ }
1965
+ this.loadResources(l, err => {
1966
+ done(err, l);
1967
+ });
1968
+ };
1969
+ if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
1970
+ setLng(this.services.languageDetector.detect());
1971
+ } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
1972
+ if (this.services.languageDetector.detect.length === 0) {
1973
+ this.services.languageDetector.detect().then(setLng);
1974
+ } else {
1975
+ this.services.languageDetector.detect(setLng);
1976
+ }
1977
+ } else {
1978
+ setLng(lng);
1979
+ }
1980
+ return deferred;
1981
+ }
1982
+ getFixedT(lng, ns, keyPrefix) {
1983
+ const fixedT = (key, opts, ...rest) => {
1984
+ let o;
1985
+ if (typeof opts !== 'object') {
1986
+ o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
1987
+ } else {
1988
+ o = {
1989
+ ...opts
1990
+ };
1991
+ }
1992
+ o.lng = o.lng || fixedT.lng;
1993
+ o.lngs = o.lngs || fixedT.lngs;
1994
+ o.ns = o.ns || fixedT.ns;
1995
+ if (o.keyPrefix !== '') o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
1996
+ const keySeparator = this.options.keySeparator || '.';
1997
+ let resultKey;
1998
+ if (o.keyPrefix && Array.isArray(key)) {
1999
+ resultKey = key.map(k => {
2000
+ if (typeof k === 'function') k = keysFromSelector(k, {
2001
+ ...this.options,
2002
+ ...opts
2003
+ });
2004
+ return `${o.keyPrefix}${keySeparator}${k}`;
2005
+ });
2006
+ } else {
2007
+ if (typeof key === 'function') key = keysFromSelector(key, {
2008
+ ...this.options,
2009
+ ...opts
2010
+ });
2011
+ resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
2012
+ }
2013
+ return this.t(resultKey, o);
2014
+ };
2015
+ if (isString$1(lng)) {
2016
+ fixedT.lng = lng;
2017
+ } else {
2018
+ fixedT.lngs = lng;
2019
+ }
2020
+ fixedT.ns = ns;
2021
+ fixedT.keyPrefix = keyPrefix;
2022
+ return fixedT;
2023
+ }
2024
+ t(...args) {
2025
+ return this.translator?.translate(...args);
2026
+ }
2027
+ exists(...args) {
2028
+ return this.translator?.exists(...args);
2029
+ }
2030
+ setDefaultNamespace(ns) {
2031
+ this.options.defaultNS = ns;
2032
+ }
2033
+ hasLoadedNamespace(ns, options = {}) {
2034
+ if (!this.isInitialized) {
2035
+ this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
2036
+ return false;
2037
+ }
2038
+ if (!this.languages || !this.languages.length) {
2039
+ this.logger.warn('hasLoadedNamespace: i18n.languages were undefined or empty', this.languages);
2040
+ return false;
2041
+ }
2042
+ const lng = options.lng || this.resolvedLanguage || this.languages[0];
2043
+ const fallbackLng = this.options ? this.options.fallbackLng : false;
2044
+ const lastLng = this.languages[this.languages.length - 1];
2045
+ if (lng.toLowerCase() === 'cimode') return true;
2046
+ const loadNotPending = (l, n) => {
2047
+ const loadState = this.services.backendConnector.state[`${l}|${n}`];
2048
+ return loadState === -1 || loadState === 0 || loadState === 2;
2049
+ };
2050
+ if (options.precheck) {
2051
+ const preResult = options.precheck(this, loadNotPending);
2052
+ if (preResult !== undefined) return preResult;
2053
+ }
2054
+ if (this.hasResourceBundle(lng, ns)) return true;
2055
+ if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
2056
+ if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
2057
+ return false;
2058
+ }
2059
+ loadNamespaces(ns, callback) {
2060
+ const deferred = defer();
2061
+ if (!this.options.ns) {
2062
+ if (callback) callback();
2063
+ return Promise.resolve();
2064
+ }
2065
+ if (isString$1(ns)) ns = [ns];
2066
+ ns.forEach(n => {
2067
+ if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
2068
+ });
2069
+ this.loadResources(err => {
2070
+ deferred.resolve();
2071
+ if (callback) callback(err);
2072
+ });
2073
+ return deferred;
2074
+ }
2075
+ loadLanguages(lngs, callback) {
2076
+ const deferred = defer();
2077
+ if (isString$1(lngs)) lngs = [lngs];
2078
+ const preloaded = this.options.preload || [];
2079
+ const newLngs = lngs.filter(lng => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
2080
+ if (!newLngs.length) {
2081
+ if (callback) callback();
2082
+ return Promise.resolve();
2083
+ }
2084
+ this.options.preload = preloaded.concat(newLngs);
2085
+ this.loadResources(err => {
2086
+ deferred.resolve();
2087
+ if (callback) callback(err);
2088
+ });
2089
+ return deferred;
2090
+ }
2091
+ dir(lng) {
2092
+ if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
2093
+ if (!lng) return 'rtl';
2094
+ try {
2095
+ const l = new Intl.Locale(lng);
2096
+ if (l && l.getTextInfo) {
2097
+ const ti = l.getTextInfo();
2098
+ if (ti && ti.direction) return ti.direction;
2099
+ }
2100
+ } catch (e) {}
2101
+ 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'];
2102
+ const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
2103
+ if (lng.toLowerCase().indexOf('-latn') > 1) return 'ltr';
2104
+ return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf('-arab') > 1 ? 'rtl' : 'ltr';
2105
+ }
2106
+ static createInstance(options = {}, callback) {
2107
+ return new I18n(options, callback);
2108
+ }
2109
+ cloneInstance(options = {}, callback = noop) {
2110
+ const forkResourceStore = options.forkResourceStore;
2111
+ if (forkResourceStore) delete options.forkResourceStore;
2112
+ const mergedOptions = {
2113
+ ...this.options,
2114
+ ...options,
2115
+ ...{
2116
+ isClone: true
2117
+ }
2118
+ };
2119
+ const clone = new I18n(mergedOptions);
2120
+ if (options.debug !== undefined || options.prefix !== undefined) {
2121
+ clone.logger = clone.logger.clone(options);
2122
+ }
2123
+ const membersToCopy = ['store', 'services', 'language'];
2124
+ membersToCopy.forEach(m => {
2125
+ clone[m] = this[m];
2126
+ });
2127
+ clone.services = {
2128
+ ...this.services
2129
+ };
2130
+ clone.services.utils = {
2131
+ hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2132
+ };
2133
+ if (forkResourceStore) {
2134
+ const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
2135
+ prev[l] = {
2136
+ ...this.store.data[l]
2137
+ };
2138
+ prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
2139
+ acc[n] = {
2140
+ ...prev[l][n]
2141
+ };
2142
+ return acc;
2143
+ }, prev[l]);
2144
+ return prev;
2145
+ }, {});
2146
+ clone.store = new ResourceStore(clonedData, mergedOptions);
2147
+ clone.services.resourceStore = clone.store;
2148
+ }
2149
+ clone.translator = new Translator(clone.services, mergedOptions);
2150
+ clone.translator.on('*', (event, ...args) => {
2151
+ clone.emit(event, ...args);
2152
+ });
2153
+ clone.init(mergedOptions, callback);
2154
+ clone.translator.options = mergedOptions;
2155
+ clone.translator.backendConnector.services.utils = {
2156
+ hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2157
+ };
2158
+ return clone;
2159
+ }
2160
+ toJSON() {
2161
+ return {
2162
+ options: this.options,
2163
+ store: this.store,
2164
+ language: this.language,
2165
+ languages: this.languages,
2166
+ resolvedLanguage: this.resolvedLanguage
2167
+ };
2168
+ }
2169
+ }
2170
+ const instance = I18n.createInstance();
2171
+ instance.createInstance = I18n.createInstance;
2172
+ instance.createInstance;
2173
+ instance.dir;
2174
+ instance.init;
2175
+ instance.loadResources;
2176
+ instance.reloadResources;
2177
+ instance.use;
2178
+ instance.changeLanguage;
2179
+ instance.getFixedT;
2180
+ instance.t;
2181
+ instance.exists;
2182
+ instance.setDefaultNamespace;
2183
+ instance.hasLoadedNamespace;
2184
+ instance.loadNamespaces;
2185
+ instance.loadLanguages;
6
2186
 
7
- var voidElements = {
8
- "area": true,
9
- "base": true,
10
- "br": true,
11
- "col": true,
12
- "embed": true,
13
- "hr": true,
14
- "img": true,
15
- "input": true,
16
- "link": true,
17
- "meta": true,
18
- "param": true,
19
- "source": true,
20
- "track": true,
21
- "wbr": true
22
- };
2187
+ function getDefaultExportFromCjs (x) {
2188
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
2189
+ }
23
2190
 
24
- var e = /*@__PURE__*/getDefaultExportFromCjs(voidElements);
2191
+ var voidElements = {
2192
+ "area": true,
2193
+ "base": true,
2194
+ "br": true,
2195
+ "col": true,
2196
+ "embed": true,
2197
+ "hr": true,
2198
+ "img": true,
2199
+ "input": true,
2200
+ "link": true,
2201
+ "meta": true,
2202
+ "param": true,
2203
+ "source": true,
2204
+ "track": true,
2205
+ "wbr": true
2206
+ };
25
2207
 
26
- var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
27
- function n(n) {
28
- var r = {
29
- type: "tag",
30
- name: "",
31
- voidElement: false,
32
- attrs: {},
33
- children: []
34
- },
35
- i = n.match(/<\/?([^\s]+?)[/\s>]/);
36
- if (i && (r.name = i[1], (e[i[1]] || "/" === n.charAt(n.length - 2)) && (r.voidElement = true), r.name.startsWith("!--"))) {
37
- var s = n.indexOf("--\x3e");
38
- return {
39
- type: "comment",
40
- comment: -1 !== s ? n.slice(4, s) : ""
41
- };
42
- }
43
- for (var a = new RegExp(t), c = null; null !== (c = a.exec(n));) if (c[0].trim()) if (c[1]) {
44
- var o = c[1].trim(),
45
- l = [o, ""];
46
- o.indexOf("=") > -1 && (l = o.split("=")), r.attrs[l[0]] = l[1], a.lastIndex--;
47
- } else c[2] && (r.attrs[c[2]] = c[3].trim().substring(1, c[3].length - 1));
48
- return r;
49
- }
50
- var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,
51
- i = /^\s*$/,
52
- s = Object.create(null);
53
- function a(e, t) {
54
- switch (t.type) {
55
- case "text":
56
- return e + t.content;
57
- case "tag":
58
- return e += "<" + t.name + (t.attrs ? function (e) {
59
- var t = [];
60
- for (var n in e) t.push(n + '="' + e[n] + '"');
61
- return t.length ? " " + t.join(" ") : "";
62
- }(t.attrs) : "") + (t.voidElement ? "/>" : ">"), t.voidElement ? e : e + t.children.reduce(a, "") + "</" + t.name + ">";
63
- case "comment":
64
- return e + "\x3c!--" + t.comment + "--\x3e";
65
- }
66
- }
67
- var c = {
68
- parse: function (e, t) {
69
- t || (t = {}), t.components || (t.components = s);
70
- var a,
71
- c = [],
72
- o = [],
73
- l = -1,
74
- m = false;
75
- if (0 !== e.indexOf("<")) {
76
- var u = e.indexOf("<");
77
- c.push({
78
- type: "text",
79
- content: -1 === u ? e : e.substring(0, u)
80
- });
81
- }
82
- return e.replace(r, function (r, s) {
83
- if (m) {
84
- if (r !== "</" + a.name + ">") return;
85
- m = false;
86
- }
87
- var u,
88
- f = "/" !== r.charAt(1),
89
- h = r.startsWith("\x3c!--"),
90
- p = s + r.length,
91
- d = e.charAt(p);
92
- if (h) {
93
- var v = n(r);
94
- return l < 0 ? (c.push(v), c) : ((u = o[l]).children.push(v), c);
95
- }
96
- if (f && (l++, "tag" === (a = n(r)).type && t.components[a.name] && (a.type = "component", m = true), a.voidElement || m || !d || "<" === d || a.children.push({
97
- type: "text",
98
- content: e.slice(p, e.indexOf("<", p))
99
- }), 0 === l && c.push(a), (u = o[l - 1]) && u.children.push(a), o[l] = a), (!f || a.voidElement) && (l > -1 && (a.voidElement || a.name === r.slice(2, -1)) && (l--, a = -1 === l ? c : o[l]), !m && "<" !== d && d)) {
100
- u = -1 === l ? c : o[l].children;
101
- var x = e.indexOf("<", p),
102
- g = e.slice(p, -1 === x ? void 0 : x);
103
- i.test(g) && (g = " "), (x > -1 && l + u.length >= 0 || " " !== g) && u.push({
104
- type: "text",
105
- content: g
106
- });
107
- }
108
- }), c;
109
- },
110
- stringify: function (e) {
111
- return e.reduce(function (e, t) {
112
- return e + a("", t);
113
- }, "");
114
- }
115
- };
2208
+ var e = /*@__PURE__*/getDefaultExportFromCjs(voidElements);
116
2209
 
117
- const warn = (i18n, code, msg, rest) => {
118
- const args = [msg, {
119
- code,
120
- ...(rest || {})
121
- }];
122
- if (i18n?.services?.logger?.forward) {
123
- return i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
124
- }
125
- if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
126
- if (i18n?.services?.logger?.warn) {
127
- i18n.services.logger.warn(...args);
128
- } else if (console?.warn) {
129
- console.warn(...args);
130
- }
131
- };
132
- const alreadyWarned = {};
133
- const warnOnce = (i18n, code, msg, rest) => {
134
- if (isString(msg) && alreadyWarned[msg]) return;
135
- if (isString(msg)) alreadyWarned[msg] = new Date();
136
- warn(i18n, code, msg, rest);
137
- };
138
- const loadedClb = (i18n, cb) => () => {
139
- if (i18n.isInitialized) {
140
- cb();
141
- } else {
142
- const initialized = () => {
143
- setTimeout(() => {
144
- i18n.off('initialized', initialized);
145
- }, 0);
146
- cb();
147
- };
148
- i18n.on('initialized', initialized);
149
- }
150
- };
151
- const loadNamespaces = (i18n, ns, cb) => {
152
- i18n.loadNamespaces(ns, loadedClb(i18n, cb));
153
- };
154
- const loadLanguages = (i18n, lng, ns, cb) => {
155
- if (isString(ns)) ns = [ns];
156
- if (i18n.options.preload && i18n.options.preload.indexOf(lng) > -1) return loadNamespaces(i18n, ns, cb);
157
- ns.forEach(n => {
158
- if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
159
- });
160
- i18n.loadLanguages(lng, loadedClb(i18n, cb));
161
- };
162
- const hasLoadedNamespace = (ns, i18n, options = {}) => {
163
- if (!i18n.languages || !i18n.languages.length) {
164
- warnOnce(i18n, 'NO_LANGUAGES', 'i18n.languages were undefined or empty', {
165
- languages: i18n.languages
166
- });
167
- return true;
168
- }
169
- return i18n.hasLoadedNamespace(ns, {
170
- lng: options.lng,
171
- precheck: (i18nInstance, loadNotPending) => {
172
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
173
- }
174
- });
175
- };
176
- const getDisplayName = Component => Component.displayName || Component.name || (isString(Component) && Component.length > 0 ? Component : 'Unknown');
177
- const isString = obj => typeof obj === 'string';
178
- const isObject = obj => typeof obj === 'object' && obj !== null;
2210
+ var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
2211
+ function n(n) {
2212
+ var r = {
2213
+ type: "tag",
2214
+ name: "",
2215
+ voidElement: false,
2216
+ attrs: {},
2217
+ children: []
2218
+ },
2219
+ i = n.match(/<\/?([^\s]+?)[/\s>]/);
2220
+ if (i && (r.name = i[1], (e[i[1]] || "/" === n.charAt(n.length - 2)) && (r.voidElement = true), r.name.startsWith("!--"))) {
2221
+ var s = n.indexOf("--\x3e");
2222
+ return {
2223
+ type: "comment",
2224
+ comment: -1 !== s ? n.slice(4, s) : ""
2225
+ };
2226
+ }
2227
+ for (var a = new RegExp(t), c = null; null !== (c = a.exec(n));) if (c[0].trim()) if (c[1]) {
2228
+ var o = c[1].trim(),
2229
+ l = [o, ""];
2230
+ o.indexOf("=") > -1 && (l = o.split("=")), r.attrs[l[0]] = l[1], a.lastIndex--;
2231
+ } else c[2] && (r.attrs[c[2]] = c[3].trim().substring(1, c[3].length - 1));
2232
+ return r;
2233
+ }
2234
+ var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,
2235
+ i = /^\s*$/,
2236
+ s = Object.create(null);
2237
+ function a(e, t) {
2238
+ switch (t.type) {
2239
+ case "text":
2240
+ return e + t.content;
2241
+ case "tag":
2242
+ return e += "<" + t.name + (t.attrs ? function (e) {
2243
+ var t = [];
2244
+ for (var n in e) t.push(n + '="' + e[n] + '"');
2245
+ return t.length ? " " + t.join(" ") : "";
2246
+ }(t.attrs) : "") + (t.voidElement ? "/>" : ">"), t.voidElement ? e : e + t.children.reduce(a, "") + "</" + t.name + ">";
2247
+ case "comment":
2248
+ return e + "\x3c!--" + t.comment + "--\x3e";
2249
+ }
2250
+ }
2251
+ var c = {
2252
+ parse: function (e, t) {
2253
+ t || (t = {}), t.components || (t.components = s);
2254
+ var a,
2255
+ c = [],
2256
+ o = [],
2257
+ l = -1,
2258
+ m = false;
2259
+ if (0 !== e.indexOf("<")) {
2260
+ var u = e.indexOf("<");
2261
+ c.push({
2262
+ type: "text",
2263
+ content: -1 === u ? e : e.substring(0, u)
2264
+ });
2265
+ }
2266
+ return e.replace(r, function (r, s) {
2267
+ if (m) {
2268
+ if (r !== "</" + a.name + ">") return;
2269
+ m = false;
2270
+ }
2271
+ var u,
2272
+ f = "/" !== r.charAt(1),
2273
+ h = r.startsWith("\x3c!--"),
2274
+ p = s + r.length,
2275
+ d = e.charAt(p);
2276
+ if (h) {
2277
+ var v = n(r);
2278
+ return l < 0 ? (c.push(v), c) : ((u = o[l]).children.push(v), c);
2279
+ }
2280
+ if (f && (l++, "tag" === (a = n(r)).type && t.components[a.name] && (a.type = "component", m = true), a.voidElement || m || !d || "<" === d || a.children.push({
2281
+ type: "text",
2282
+ content: e.slice(p, e.indexOf("<", p))
2283
+ }), 0 === l && c.push(a), (u = o[l - 1]) && u.children.push(a), o[l] = a), (!f || a.voidElement) && (l > -1 && (a.voidElement || a.name === r.slice(2, -1)) && (l--, a = -1 === l ? c : o[l]), !m && "<" !== d && d)) {
2284
+ u = -1 === l ? c : o[l].children;
2285
+ var x = e.indexOf("<", p),
2286
+ g = e.slice(p, -1 === x ? void 0 : x);
2287
+ i.test(g) && (g = " "), (x > -1 && l + u.length >= 0 || " " !== g) && u.push({
2288
+ type: "text",
2289
+ content: g
2290
+ });
2291
+ }
2292
+ }), c;
2293
+ },
2294
+ stringify: function (e) {
2295
+ return e.reduce(function (e, t) {
2296
+ return e + a("", t);
2297
+ }, "");
2298
+ }
2299
+ };
179
2300
 
180
- const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
181
- const htmlEntities = {
182
- '&amp;': '&',
183
- '&#38;': '&',
184
- '&lt;': '<',
185
- '&#60;': '<',
186
- '&gt;': '>',
187
- '&#62;': '>',
188
- '&apos;': "'",
189
- '&#39;': "'",
190
- '&quot;': '"',
191
- '&#34;': '"',
192
- '&nbsp;': ' ',
193
- '&#160;': ' ',
194
- '&copy;': '©',
195
- '&#169;': '©',
196
- '&reg;': '®',
197
- '&#174;': '®',
198
- '&hellip;': '…',
199
- '&#8230;': '…',
200
- '&#x2F;': '/',
201
- '&#47;': '/'
202
- };
203
- const unescapeHtmlEntity = m => htmlEntities[m];
204
- const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
2301
+ const warn = (i18n, code, msg, rest) => {
2302
+ const args = [msg, {
2303
+ code,
2304
+ ...(rest || {})
2305
+ }];
2306
+ if (i18n?.services?.logger?.forward) {
2307
+ return i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
2308
+ }
2309
+ if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
2310
+ if (i18n?.services?.logger?.warn) {
2311
+ i18n.services.logger.warn(...args);
2312
+ } else if (console?.warn) {
2313
+ console.warn(...args);
2314
+ }
2315
+ };
2316
+ const alreadyWarned = {};
2317
+ const warnOnce = (i18n, code, msg, rest) => {
2318
+ if (isString(msg) && alreadyWarned[msg]) return;
2319
+ if (isString(msg)) alreadyWarned[msg] = new Date();
2320
+ warn(i18n, code, msg, rest);
2321
+ };
2322
+ const loadedClb = (i18n, cb) => () => {
2323
+ if (i18n.isInitialized) {
2324
+ cb();
2325
+ } else {
2326
+ const initialized = () => {
2327
+ setTimeout(() => {
2328
+ i18n.off('initialized', initialized);
2329
+ }, 0);
2330
+ cb();
2331
+ };
2332
+ i18n.on('initialized', initialized);
2333
+ }
2334
+ };
2335
+ const loadNamespaces = (i18n, ns, cb) => {
2336
+ i18n.loadNamespaces(ns, loadedClb(i18n, cb));
2337
+ };
2338
+ const loadLanguages = (i18n, lng, ns, cb) => {
2339
+ if (isString(ns)) ns = [ns];
2340
+ if (i18n.options.preload && i18n.options.preload.indexOf(lng) > -1) return loadNamespaces(i18n, ns, cb);
2341
+ ns.forEach(n => {
2342
+ if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
2343
+ });
2344
+ i18n.loadLanguages(lng, loadedClb(i18n, cb));
2345
+ };
2346
+ const hasLoadedNamespace = (ns, i18n, options = {}) => {
2347
+ if (!i18n.languages || !i18n.languages.length) {
2348
+ warnOnce(i18n, 'NO_LANGUAGES', 'i18n.languages were undefined or empty', {
2349
+ languages: i18n.languages
2350
+ });
2351
+ return true;
2352
+ }
2353
+ return i18n.hasLoadedNamespace(ns, {
2354
+ lng: options.lng,
2355
+ precheck: (i18nInstance, loadNotPending) => {
2356
+ if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
2357
+ }
2358
+ });
2359
+ };
2360
+ const getDisplayName = Component => Component.displayName || Component.name || (isString(Component) && Component.length > 0 ? Component : 'Unknown');
2361
+ const isString = obj => typeof obj === 'string';
2362
+ const isObject = obj => typeof obj === 'object' && obj !== null;
205
2363
 
206
- let defaultOptions = {
207
- bindI18n: 'languageChanged',
208
- bindI18nStore: '',
209
- transEmptyNodeValue: '',
210
- transSupportBasicHtmlNodes: true,
211
- transWrapTextNodes: '',
212
- transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
213
- useSuspense: true,
214
- unescape
215
- };
216
- const setDefaults = (options = {}) => {
217
- defaultOptions = {
218
- ...defaultOptions,
219
- ...options
220
- };
221
- };
222
- const getDefaults = () => defaultOptions;
2364
+ const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
2365
+ const htmlEntities = {
2366
+ '&amp;': '&',
2367
+ '&#38;': '&',
2368
+ '&lt;': '<',
2369
+ '&#60;': '<',
2370
+ '&gt;': '>',
2371
+ '&#62;': '>',
2372
+ '&apos;': "'",
2373
+ '&#39;': "'",
2374
+ '&quot;': '"',
2375
+ '&#34;': '"',
2376
+ '&nbsp;': ' ',
2377
+ '&#160;': ' ',
2378
+ '&copy;': '©',
2379
+ '&#169;': '©',
2380
+ '&reg;': '®',
2381
+ '&#174;': '®',
2382
+ '&hellip;': '…',
2383
+ '&#8230;': '…',
2384
+ '&#x2F;': '/',
2385
+ '&#47;': '/'
2386
+ };
2387
+ const unescapeHtmlEntity = m => htmlEntities[m];
2388
+ const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
223
2389
 
224
- let i18nInstance;
225
- const setI18n = instance => {
226
- i18nInstance = instance;
227
- };
228
- const getI18n = () => i18nInstance;
2390
+ let defaultOptions = {
2391
+ bindI18n: 'languageChanged',
2392
+ bindI18nStore: '',
2393
+ transEmptyNodeValue: '',
2394
+ transSupportBasicHtmlNodes: true,
2395
+ transWrapTextNodes: '',
2396
+ transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
2397
+ useSuspense: true,
2398
+ unescape
2399
+ };
2400
+ const setDefaults = (options = {}) => {
2401
+ defaultOptions = {
2402
+ ...defaultOptions,
2403
+ ...options
2404
+ };
2405
+ };
2406
+ const getDefaults = () => defaultOptions;
229
2407
 
230
- const hasChildren = (node, checkLength) => {
231
- if (!node) return false;
232
- const base = node.props?.children ?? node.children;
233
- if (checkLength) return base.length > 0;
234
- return !!base;
235
- };
236
- const getChildren = node => {
237
- if (!node) return [];
238
- const children = node.props?.children ?? node.children;
239
- return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
240
- };
241
- const hasValidReactChildren = children => Array.isArray(children) && children.every(react.isValidElement);
242
- const getAsArray = data => Array.isArray(data) ? data : [data];
243
- const mergeProps = (source, target) => {
244
- const newTarget = {
245
- ...target
246
- };
247
- newTarget.props = Object.assign(source.props, target.props);
248
- return newTarget;
249
- };
250
- const nodesToString = (children, i18nOptions, i18n, i18nKey) => {
251
- if (!children) return '';
252
- let stringNode = '';
253
- const childrenArray = getAsArray(children);
254
- const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
255
- childrenArray.forEach((child, childIndex) => {
256
- if (isString(child)) {
257
- stringNode += `${child}`;
258
- return;
259
- }
260
- if (react.isValidElement(child)) {
261
- const {
262
- props,
263
- type
264
- } = child;
265
- const childPropsCount = Object.keys(props).length;
266
- const shouldKeepChild = keepArray.indexOf(type) > -1;
267
- const childChildren = props.children;
268
- if (!childChildren && shouldKeepChild && !childPropsCount) {
269
- stringNode += `<${type}/>`;
270
- return;
271
- }
272
- if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
273
- stringNode += `<${childIndex}></${childIndex}>`;
274
- return;
275
- }
276
- if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
277
- stringNode += `<${type}>${childChildren}</${type}>`;
278
- return;
279
- }
280
- const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey);
281
- stringNode += `<${childIndex}>${content}</${childIndex}>`;
282
- return;
283
- }
284
- if (child === null) {
285
- warn(i18n, 'TRANS_NULL_VALUE', `Passed in a null value as child`, {
286
- i18nKey
287
- });
288
- return;
289
- }
290
- if (isObject(child)) {
291
- const {
292
- format,
293
- ...clone
294
- } = child;
295
- const keys = Object.keys(clone);
296
- if (keys.length === 1) {
297
- const value = format ? `${keys[0]}, ${format}` : keys[0];
298
- stringNode += `{{${value}}}`;
299
- return;
300
- }
301
- warn(i18n, 'TRANS_INVALID_OBJ', `Invalid child - Object should only have keys {{ value, format }} (format is optional).`, {
302
- i18nKey,
303
- child
304
- });
305
- return;
306
- }
307
- warn(i18n, 'TRANS_INVALID_VAR', `Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.`, {
308
- i18nKey,
309
- child
310
- });
311
- });
312
- return stringNode;
313
- };
314
- const renderNodes = (children, knownComponentsMap, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
315
- if (targetString === '') return [];
316
- const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
317
- const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
318
- if (!children && !knownComponentsMap && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
319
- const data = knownComponentsMap ?? {};
320
- const getData = childs => {
321
- const childrenArray = getAsArray(childs);
322
- childrenArray.forEach(child => {
323
- if (isString(child)) return;
324
- if (hasChildren(child)) getData(getChildren(child));else if (isObject(child) && !react.isValidElement(child)) Object.assign(data, child);
325
- });
326
- };
327
- getData(children);
328
- const ast = c.parse(`<0>${targetString}</0>`);
329
- const opts = {
330
- ...data,
331
- ...combinedTOpts
332
- };
333
- const renderInner = (child, node, rootReactNode) => {
334
- const childs = getChildren(child);
335
- const mappedChildren = mapAST(childs, node.children, rootReactNode);
336
- return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
337
- };
338
- const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
339
- if (child.dummy) {
340
- child.children = inner;
341
- mem.push(react.cloneElement(child, {
342
- key: i
343
- }, isVoid ? undefined : inner));
344
- } else {
345
- mem.push(...react.Children.map([child], c => {
346
- const props = {
347
- ...c.props
348
- };
349
- delete props.i18nIsDynamicList;
350
- return react.createElement(c.type, {
351
- ...props,
352
- key: i,
353
- ref: c.props.ref ?? c.ref
354
- }, isVoid ? null : inner);
355
- }));
356
- }
357
- };
358
- const mapAST = (reactNode, astNode, rootReactNode) => {
359
- const reactNodes = getAsArray(reactNode);
360
- const astNodes = getAsArray(astNode);
361
- return astNodes.reduce((mem, node, i) => {
362
- const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
363
- if (node.type === 'tag') {
364
- let tmp = reactNodes[parseInt(node.name, 10)];
365
- if (!tmp && knownComponentsMap) tmp = knownComponentsMap[node.name];
366
- if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
367
- if (!tmp) tmp = {};
368
- const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
369
- props: node.attrs
370
- }, tmp) : tmp;
371
- const isElement = react.isValidElement(child);
372
- const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
373
- const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && isObject(child) && child.dummy && !isElement;
374
- const isKnownComponent = isObject(knownComponentsMap) && Object.hasOwnProperty.call(knownComponentsMap, node.name);
375
- if (isString(child)) {
376
- const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
377
- mem.push(value);
378
- } else if (hasChildren(child) || isValidTranslationWithChildren) {
379
- const inner = renderInner(child, node, rootReactNode);
380
- pushTranslatedJSX(child, inner, mem, i);
381
- } else if (isEmptyTransWithHTML) {
382
- const inner = mapAST(reactNodes, node.children, rootReactNode);
383
- pushTranslatedJSX(child, inner, mem, i);
384
- } else if (Number.isNaN(parseFloat(node.name))) {
385
- if (isKnownComponent) {
386
- const inner = renderInner(child, node, rootReactNode);
387
- pushTranslatedJSX(child, inner, mem, i, node.voidElement);
388
- } else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
389
- if (node.voidElement) {
390
- mem.push(react.createElement(node.name, {
391
- key: `${node.name}-${i}`
392
- }));
393
- } else {
394
- const inner = mapAST(reactNodes, node.children, rootReactNode);
395
- mem.push(react.createElement(node.name, {
396
- key: `${node.name}-${i}`
397
- }, inner));
398
- }
399
- } else if (node.voidElement) {
400
- mem.push(`<${node.name} />`);
401
- } else {
402
- const inner = mapAST(reactNodes, node.children, rootReactNode);
403
- mem.push(`<${node.name}>${inner}</${node.name}>`);
404
- }
405
- } else if (isObject(child) && !isElement) {
406
- const content = node.children[0] ? translationContent : null;
407
- if (content) mem.push(content);
408
- } else {
409
- pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
410
- }
411
- } else if (node.type === 'text') {
412
- const wrapTextNodes = i18nOptions.transWrapTextNodes;
413
- const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
414
- if (wrapTextNodes) {
415
- mem.push(react.createElement(wrapTextNodes, {
416
- key: `${node.name}-${i}`
417
- }, content));
418
- } else {
419
- mem.push(content);
420
- }
421
- }
422
- return mem;
423
- }, []);
424
- };
425
- const result = mapAST([{
426
- dummy: true,
427
- children: children || []
428
- }], ast, getAsArray(children || []));
429
- return getChildren(result[0]);
430
- };
431
- const fixComponentProps = (component, index, translation) => {
432
- const componentKey = component.key || index;
433
- const comp = react.cloneElement(component, {
434
- key: componentKey
435
- });
436
- if (!comp.props || !comp.props.children || translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0) {
437
- return comp;
438
- }
439
- function Componentized() {
440
- return react.createElement(react.Fragment, null, comp);
441
- }
442
- return react.createElement(Componentized, {
443
- key: componentKey
444
- });
445
- };
446
- const generateArrayComponents = (components, translation) => components.map((c, index) => fixComponentProps(c, index, translation));
447
- const generateObjectComponents = (components, translation) => {
448
- const componentMap = {};
449
- Object.keys(components).forEach(c => {
450
- Object.assign(componentMap, {
451
- [c]: fixComponentProps(components[c], c, translation)
452
- });
453
- });
454
- return componentMap;
455
- };
456
- const generateComponents = (components, translation, i18n, i18nKey) => {
457
- if (!components) return null;
458
- if (Array.isArray(components)) {
459
- return generateArrayComponents(components, translation);
460
- }
461
- if (isObject(components)) {
462
- return generateObjectComponents(components, translation);
463
- }
464
- warnOnce(i18n, 'TRANS_INVALID_COMPONENTS', `<Trans /> "components" prop expects an object or array`, {
465
- i18nKey
466
- });
467
- return null;
468
- };
469
- const isComponentsMap = object => {
470
- if (!isObject(object)) return false;
471
- if (Array.isArray(object)) return false;
472
- return Object.keys(object).reduce((acc, key) => acc && Number.isNaN(Number.parseFloat(key)), true);
473
- };
474
- function Trans$1({
475
- children,
476
- count,
477
- parent,
478
- i18nKey,
479
- context,
480
- tOptions = {},
481
- values,
482
- defaults,
483
- components,
484
- ns,
485
- i18n: i18nFromProps,
486
- t: tFromProps,
487
- shouldUnescape,
488
- ...additionalProps
489
- }) {
490
- const i18n = i18nFromProps || getI18n();
491
- if (!i18n) {
492
- warnOnce(i18n, 'NO_I18NEXT_INSTANCE', `Trans: You need to pass in an i18next instance using i18nextReactModule`, {
493
- i18nKey
494
- });
495
- return children;
496
- }
497
- const t = tFromProps || i18n.t.bind(i18n) || (k => k);
498
- const reactI18nextOptions = {
499
- ...getDefaults(),
500
- ...i18n.options?.react
501
- };
502
- let namespaces = ns || t.ns || i18n.options?.defaultNS;
503
- namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
504
- const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey);
505
- const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
506
- const {
507
- hashTransKey
508
- } = reactI18nextOptions;
509
- const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
510
- if (i18n.options?.interpolation?.defaultVariables) {
511
- values = values && Object.keys(values).length > 0 ? {
512
- ...values,
513
- ...i18n.options.interpolation.defaultVariables
514
- } : {
515
- ...i18n.options.interpolation.defaultVariables
516
- };
517
- }
518
- const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? tOptions.interpolation : {
519
- interpolation: {
520
- ...tOptions.interpolation,
521
- prefix: '#$?',
522
- suffix: '?$#'
523
- }
524
- };
525
- const combinedTOpts = {
526
- ...tOptions,
527
- context: context || tOptions.context,
528
- count,
529
- ...values,
530
- ...interpolationOverride,
531
- defaultValue,
532
- ns: namespaces
533
- };
534
- const translation = key ? t(key, combinedTOpts) : defaultValue;
535
- const generatedComponents = generateComponents(components, translation, i18n, i18nKey);
536
- let indexedChildren = generatedComponents || children;
537
- let componentsMap = null;
538
- if (isComponentsMap(generatedComponents)) {
539
- componentsMap = generatedComponents;
540
- indexedChildren = children;
541
- }
542
- const content = renderNodes(indexedChildren, componentsMap, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
543
- const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
544
- return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
545
- }
2408
+ let i18nInstance;
2409
+ const setI18n = instance => {
2410
+ i18nInstance = instance;
2411
+ };
2412
+ const getI18n = () => i18nInstance;
546
2413
 
547
- const initReactI18next = {
548
- type: '3rdParty',
549
- init(instance) {
550
- setDefaults(instance.options.react);
551
- setI18n(instance);
552
- }
553
- };
2414
+ const hasChildren = (node, checkLength) => {
2415
+ if (!node) return false;
2416
+ const base = node.props?.children ?? node.children;
2417
+ if (checkLength) return base.length > 0;
2418
+ return !!base;
2419
+ };
2420
+ const getChildren = node => {
2421
+ if (!node) return [];
2422
+ const children = node.props?.children ?? node.children;
2423
+ return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
2424
+ };
2425
+ const hasValidReactChildren = children => Array.isArray(children) && children.every(react.isValidElement);
2426
+ const getAsArray = data => Array.isArray(data) ? data : [data];
2427
+ const mergeProps = (source, target) => {
2428
+ const newTarget = {
2429
+ ...target
2430
+ };
2431
+ newTarget.props = Object.assign(source.props, target.props);
2432
+ return newTarget;
2433
+ };
2434
+ const nodesToString = (children, i18nOptions, i18n, i18nKey) => {
2435
+ if (!children) return '';
2436
+ let stringNode = '';
2437
+ const childrenArray = getAsArray(children);
2438
+ const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
2439
+ childrenArray.forEach((child, childIndex) => {
2440
+ if (isString(child)) {
2441
+ stringNode += `${child}`;
2442
+ return;
2443
+ }
2444
+ if (react.isValidElement(child)) {
2445
+ const {
2446
+ props,
2447
+ type
2448
+ } = child;
2449
+ const childPropsCount = Object.keys(props).length;
2450
+ const shouldKeepChild = keepArray.indexOf(type) > -1;
2451
+ const childChildren = props.children;
2452
+ if (!childChildren && shouldKeepChild && !childPropsCount) {
2453
+ stringNode += `<${type}/>`;
2454
+ return;
2455
+ }
2456
+ if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
2457
+ stringNode += `<${childIndex}></${childIndex}>`;
2458
+ return;
2459
+ }
2460
+ if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
2461
+ stringNode += `<${type}>${childChildren}</${type}>`;
2462
+ return;
2463
+ }
2464
+ const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey);
2465
+ stringNode += `<${childIndex}>${content}</${childIndex}>`;
2466
+ return;
2467
+ }
2468
+ if (child === null) {
2469
+ warn(i18n, 'TRANS_NULL_VALUE', `Passed in a null value as child`, {
2470
+ i18nKey
2471
+ });
2472
+ return;
2473
+ }
2474
+ if (isObject(child)) {
2475
+ const {
2476
+ format,
2477
+ ...clone
2478
+ } = child;
2479
+ const keys = Object.keys(clone);
2480
+ if (keys.length === 1) {
2481
+ const value = format ? `${keys[0]}, ${format}` : keys[0];
2482
+ stringNode += `{{${value}}}`;
2483
+ return;
2484
+ }
2485
+ warn(i18n, 'TRANS_INVALID_OBJ', `Invalid child - Object should only have keys {{ value, format }} (format is optional).`, {
2486
+ i18nKey,
2487
+ child
2488
+ });
2489
+ return;
2490
+ }
2491
+ warn(i18n, 'TRANS_INVALID_VAR', `Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.`, {
2492
+ i18nKey,
2493
+ child
2494
+ });
2495
+ });
2496
+ return stringNode;
2497
+ };
2498
+ const renderNodes = (children, knownComponentsMap, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
2499
+ if (targetString === '') return [];
2500
+ const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
2501
+ const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
2502
+ if (!children && !knownComponentsMap && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
2503
+ const data = knownComponentsMap ?? {};
2504
+ const getData = childs => {
2505
+ const childrenArray = getAsArray(childs);
2506
+ childrenArray.forEach(child => {
2507
+ if (isString(child)) return;
2508
+ if (hasChildren(child)) getData(getChildren(child));else if (isObject(child) && !react.isValidElement(child)) Object.assign(data, child);
2509
+ });
2510
+ };
2511
+ getData(children);
2512
+ const ast = c.parse(`<0>${targetString}</0>`);
2513
+ const opts = {
2514
+ ...data,
2515
+ ...combinedTOpts
2516
+ };
2517
+ const renderInner = (child, node, rootReactNode) => {
2518
+ const childs = getChildren(child);
2519
+ const mappedChildren = mapAST(childs, node.children, rootReactNode);
2520
+ return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
2521
+ };
2522
+ const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
2523
+ if (child.dummy) {
2524
+ child.children = inner;
2525
+ mem.push(react.cloneElement(child, {
2526
+ key: i
2527
+ }, isVoid ? undefined : inner));
2528
+ } else {
2529
+ mem.push(...react.Children.map([child], c => {
2530
+ const props = {
2531
+ ...c.props
2532
+ };
2533
+ delete props.i18nIsDynamicList;
2534
+ return react.createElement(c.type, {
2535
+ ...props,
2536
+ key: i,
2537
+ ref: c.props.ref ?? c.ref
2538
+ }, isVoid ? null : inner);
2539
+ }));
2540
+ }
2541
+ };
2542
+ const mapAST = (reactNode, astNode, rootReactNode) => {
2543
+ const reactNodes = getAsArray(reactNode);
2544
+ const astNodes = getAsArray(astNode);
2545
+ return astNodes.reduce((mem, node, i) => {
2546
+ const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
2547
+ if (node.type === 'tag') {
2548
+ let tmp = reactNodes[parseInt(node.name, 10)];
2549
+ if (!tmp && knownComponentsMap) tmp = knownComponentsMap[node.name];
2550
+ if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
2551
+ if (!tmp) tmp = {};
2552
+ const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
2553
+ props: node.attrs
2554
+ }, tmp) : tmp;
2555
+ const isElement = react.isValidElement(child);
2556
+ const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
2557
+ const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && isObject(child) && child.dummy && !isElement;
2558
+ const isKnownComponent = isObject(knownComponentsMap) && Object.hasOwnProperty.call(knownComponentsMap, node.name);
2559
+ if (isString(child)) {
2560
+ const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
2561
+ mem.push(value);
2562
+ } else if (hasChildren(child) || isValidTranslationWithChildren) {
2563
+ const inner = renderInner(child, node, rootReactNode);
2564
+ pushTranslatedJSX(child, inner, mem, i);
2565
+ } else if (isEmptyTransWithHTML) {
2566
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
2567
+ pushTranslatedJSX(child, inner, mem, i);
2568
+ } else if (Number.isNaN(parseFloat(node.name))) {
2569
+ if (isKnownComponent) {
2570
+ const inner = renderInner(child, node, rootReactNode);
2571
+ pushTranslatedJSX(child, inner, mem, i, node.voidElement);
2572
+ } else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
2573
+ if (node.voidElement) {
2574
+ mem.push(react.createElement(node.name, {
2575
+ key: `${node.name}-${i}`
2576
+ }));
2577
+ } else {
2578
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
2579
+ mem.push(react.createElement(node.name, {
2580
+ key: `${node.name}-${i}`
2581
+ }, inner));
2582
+ }
2583
+ } else if (node.voidElement) {
2584
+ mem.push(`<${node.name} />`);
2585
+ } else {
2586
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
2587
+ mem.push(`<${node.name}>${inner}</${node.name}>`);
2588
+ }
2589
+ } else if (isObject(child) && !isElement) {
2590
+ const content = node.children[0] ? translationContent : null;
2591
+ if (content) mem.push(content);
2592
+ } else {
2593
+ pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
2594
+ }
2595
+ } else if (node.type === 'text') {
2596
+ const wrapTextNodes = i18nOptions.transWrapTextNodes;
2597
+ const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
2598
+ if (wrapTextNodes) {
2599
+ mem.push(react.createElement(wrapTextNodes, {
2600
+ key: `${node.name}-${i}`
2601
+ }, content));
2602
+ } else {
2603
+ mem.push(content);
2604
+ }
2605
+ }
2606
+ return mem;
2607
+ }, []);
2608
+ };
2609
+ const result = mapAST([{
2610
+ dummy: true,
2611
+ children: children || []
2612
+ }], ast, getAsArray(children || []));
2613
+ return getChildren(result[0]);
2614
+ };
2615
+ const fixComponentProps = (component, index, translation) => {
2616
+ const componentKey = component.key || index;
2617
+ const comp = react.cloneElement(component, {
2618
+ key: componentKey
2619
+ });
2620
+ if (!comp.props || !comp.props.children || translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0) {
2621
+ return comp;
2622
+ }
2623
+ function Componentized() {
2624
+ return react.createElement(react.Fragment, null, comp);
2625
+ }
2626
+ return react.createElement(Componentized, {
2627
+ key: componentKey
2628
+ });
2629
+ };
2630
+ const generateArrayComponents = (components, translation) => components.map((c, index) => fixComponentProps(c, index, translation));
2631
+ const generateObjectComponents = (components, translation) => {
2632
+ const componentMap = {};
2633
+ Object.keys(components).forEach(c => {
2634
+ Object.assign(componentMap, {
2635
+ [c]: fixComponentProps(components[c], c, translation)
2636
+ });
2637
+ });
2638
+ return componentMap;
2639
+ };
2640
+ const generateComponents = (components, translation, i18n, i18nKey) => {
2641
+ if (!components) return null;
2642
+ if (Array.isArray(components)) {
2643
+ return generateArrayComponents(components, translation);
2644
+ }
2645
+ if (isObject(components)) {
2646
+ return generateObjectComponents(components, translation);
2647
+ }
2648
+ warnOnce(i18n, 'TRANS_INVALID_COMPONENTS', `<Trans /> "components" prop expects an object or array`, {
2649
+ i18nKey
2650
+ });
2651
+ return null;
2652
+ };
2653
+ const isComponentsMap = object => {
2654
+ if (!isObject(object)) return false;
2655
+ if (Array.isArray(object)) return false;
2656
+ return Object.keys(object).reduce((acc, key) => acc && Number.isNaN(Number.parseFloat(key)), true);
2657
+ };
2658
+ function Trans$1({
2659
+ children,
2660
+ count,
2661
+ parent,
2662
+ i18nKey,
2663
+ context,
2664
+ tOptions = {},
2665
+ values,
2666
+ defaults,
2667
+ components,
2668
+ ns,
2669
+ i18n: i18nFromProps,
2670
+ t: tFromProps,
2671
+ shouldUnescape,
2672
+ ...additionalProps
2673
+ }) {
2674
+ const i18n = i18nFromProps || getI18n();
2675
+ if (!i18n) {
2676
+ warnOnce(i18n, 'NO_I18NEXT_INSTANCE', `Trans: You need to pass in an i18next instance using i18nextReactModule`, {
2677
+ i18nKey
2678
+ });
2679
+ return children;
2680
+ }
2681
+ const t = tFromProps || i18n.t.bind(i18n) || (k => k);
2682
+ const reactI18nextOptions = {
2683
+ ...getDefaults(),
2684
+ ...i18n.options?.react
2685
+ };
2686
+ let namespaces = ns || t.ns || i18n.options?.defaultNS;
2687
+ namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
2688
+ const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey);
2689
+ const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || (typeof i18nKey === 'function' ? keysFromSelector(i18nKey) : i18nKey);
2690
+ const {
2691
+ hashTransKey
2692
+ } = reactI18nextOptions;
2693
+ const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
2694
+ if (i18n.options?.interpolation?.defaultVariables) {
2695
+ values = values && Object.keys(values).length > 0 ? {
2696
+ ...values,
2697
+ ...i18n.options.interpolation.defaultVariables
2698
+ } : {
2699
+ ...i18n.options.interpolation.defaultVariables
2700
+ };
2701
+ }
2702
+ const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? tOptions.interpolation : {
2703
+ interpolation: {
2704
+ ...tOptions.interpolation,
2705
+ prefix: '#$?',
2706
+ suffix: '?$#'
2707
+ }
2708
+ };
2709
+ const combinedTOpts = {
2710
+ ...tOptions,
2711
+ context: context || tOptions.context,
2712
+ count,
2713
+ ...values,
2714
+ ...interpolationOverride,
2715
+ defaultValue,
2716
+ ns: namespaces
2717
+ };
2718
+ const translation = key ? t(key, combinedTOpts) : defaultValue;
2719
+ const generatedComponents = generateComponents(components, translation, i18n, i18nKey);
2720
+ let indexedChildren = generatedComponents || children;
2721
+ let componentsMap = null;
2722
+ if (isComponentsMap(generatedComponents)) {
2723
+ componentsMap = generatedComponents;
2724
+ indexedChildren = children;
2725
+ }
2726
+ const content = renderNodes(indexedChildren, componentsMap, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
2727
+ const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
2728
+ return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
2729
+ }
554
2730
 
555
- const I18nContext = react.createContext();
556
- class ReportNamespaces {
557
- constructor() {
558
- this.usedNamespaces = {};
559
- }
560
- addUsedNamespaces(namespaces) {
561
- namespaces.forEach(ns => {
562
- if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
563
- });
564
- }
565
- getUsedNamespaces() {
566
- return Object.keys(this.usedNamespaces);
567
- }
568
- }
569
- const composeInitialProps = ForComponent => async ctx => {
570
- const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
571
- const i18nInitialProps = getInitialProps();
572
- return {
573
- ...componentsInitialProps,
574
- ...i18nInitialProps
575
- };
576
- };
577
- const getInitialProps = () => {
578
- const i18n = getI18n();
579
- const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
580
- const ret = {};
581
- const initialI18nStore = {};
582
- i18n.languages.forEach(l => {
583
- initialI18nStore[l] = {};
584
- namespaces.forEach(ns => {
585
- initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
586
- });
587
- });
588
- ret.initialI18nStore = initialI18nStore;
589
- ret.initialLanguage = i18n.language;
590
- return ret;
591
- };
2731
+ const initReactI18next = {
2732
+ type: '3rdParty',
2733
+ init(instance) {
2734
+ setDefaults(instance.options.react);
2735
+ setI18n(instance);
2736
+ }
2737
+ };
592
2738
 
593
- function Trans({
594
- children,
595
- count,
596
- parent,
597
- i18nKey,
598
- context,
599
- tOptions = {},
600
- values,
601
- defaults,
602
- components,
603
- ns,
604
- i18n: i18nFromProps,
605
- t: tFromProps,
606
- shouldUnescape,
607
- ...additionalProps
608
- }) {
609
- const {
610
- i18n: i18nFromContext,
611
- defaultNS: defaultNSFromContext
612
- } = react.useContext(I18nContext) || {};
613
- const i18n = i18nFromProps || i18nFromContext || getI18n();
614
- const t = tFromProps || i18n?.t.bind(i18n);
615
- return Trans$1({
616
- children,
617
- count,
618
- parent,
619
- i18nKey,
620
- context,
621
- tOptions,
622
- values,
623
- defaults,
624
- components,
625
- ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
626
- i18n,
627
- t: tFromProps,
628
- shouldUnescape,
629
- ...additionalProps
630
- });
631
- }
2739
+ const I18nContext = react.createContext();
2740
+ class ReportNamespaces {
2741
+ constructor() {
2742
+ this.usedNamespaces = {};
2743
+ }
2744
+ addUsedNamespaces(namespaces) {
2745
+ namespaces.forEach(ns => {
2746
+ if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
2747
+ });
2748
+ }
2749
+ getUsedNamespaces() {
2750
+ return Object.keys(this.usedNamespaces);
2751
+ }
2752
+ }
2753
+ const composeInitialProps = ForComponent => async ctx => {
2754
+ const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
2755
+ const i18nInitialProps = getInitialProps();
2756
+ return {
2757
+ ...componentsInitialProps,
2758
+ ...i18nInitialProps
2759
+ };
2760
+ };
2761
+ const getInitialProps = () => {
2762
+ const i18n = getI18n();
2763
+ const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
2764
+ const ret = {};
2765
+ const initialI18nStore = {};
2766
+ i18n.languages.forEach(l => {
2767
+ initialI18nStore[l] = {};
2768
+ namespaces.forEach(ns => {
2769
+ initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
2770
+ });
2771
+ });
2772
+ ret.initialI18nStore = initialI18nStore;
2773
+ ret.initialLanguage = i18n.language;
2774
+ return ret;
2775
+ };
632
2776
 
633
- const usePrevious = (value, ignore) => {
634
- const ref = react.useRef();
635
- react.useEffect(() => {
636
- ref.current = value;
637
- }, [value, ignore]);
638
- return ref.current;
639
- };
640
- const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
641
- const useMemoizedT = (i18n, language, namespace, keyPrefix) => react.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
642
- const useTranslation = (ns, props = {}) => {
643
- const {
644
- i18n: i18nFromProps
645
- } = props;
646
- const {
647
- i18n: i18nFromContext,
648
- defaultNS: defaultNSFromContext
649
- } = react.useContext(I18nContext) || {};
650
- const i18n = i18nFromProps || i18nFromContext || getI18n();
651
- if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
652
- if (!i18n) {
653
- warnOnce(i18n, 'NO_I18NEXT_INSTANCE', 'useTranslation: You will need to pass in an i18next instance by using initReactI18next');
654
- const notReadyT = (k, optsOrDefaultValue) => {
655
- if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
656
- if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
657
- return Array.isArray(k) ? k[k.length - 1] : k;
658
- };
659
- const retNotReady = [notReadyT, {}, false];
660
- retNotReady.t = notReadyT;
661
- retNotReady.i18n = {};
662
- retNotReady.ready = false;
663
- return retNotReady;
664
- }
665
- if (i18n.options.react?.wait) warnOnce(i18n, 'DEPRECATED_OPTION', 'useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
666
- const i18nOptions = {
667
- ...getDefaults(),
668
- ...i18n.options.react,
669
- ...props
670
- };
671
- const {
672
- useSuspense,
673
- keyPrefix
674
- } = i18nOptions;
675
- let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
676
- namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
677
- i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
678
- const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
679
- const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
680
- const getT = () => memoGetT;
681
- const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
682
- const [t, setT] = react.useState(getT);
683
- let joinedNS = namespaces.join();
684
- if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
685
- const previousJoinedNS = usePrevious(joinedNS);
686
- const isMounted = react.useRef(true);
687
- react.useEffect(() => {
688
- const {
689
- bindI18n,
690
- bindI18nStore
691
- } = i18nOptions;
692
- isMounted.current = true;
693
- if (!ready && !useSuspense) {
694
- if (props.lng) {
695
- loadLanguages(i18n, props.lng, namespaces, () => {
696
- if (isMounted.current) setT(getNewT);
697
- });
698
- } else {
699
- loadNamespaces(i18n, namespaces, () => {
700
- if (isMounted.current) setT(getNewT);
701
- });
702
- }
703
- }
704
- if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
705
- setT(getNewT);
706
- }
707
- const boundReset = () => {
708
- if (isMounted.current) setT(getNewT);
709
- };
710
- if (bindI18n) i18n?.on(bindI18n, boundReset);
711
- if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
712
- return () => {
713
- isMounted.current = false;
714
- if (i18n && bindI18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
715
- if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
716
- };
717
- }, [i18n, joinedNS]);
718
- react.useEffect(() => {
719
- if (isMounted.current && ready) {
720
- setT(getT);
721
- }
722
- }, [i18n, keyPrefix, ready]);
723
- const ret = [t, i18n, ready];
724
- ret.t = t;
725
- ret.i18n = i18n;
726
- ret.ready = ready;
727
- if (ready) return ret;
728
- if (!ready && !useSuspense) return ret;
729
- throw new Promise(resolve => {
730
- if (props.lng) {
731
- loadLanguages(i18n, props.lng, namespaces, () => resolve());
732
- } else {
733
- loadNamespaces(i18n, namespaces, () => resolve());
734
- }
735
- });
736
- };
2777
+ function Trans({
2778
+ children,
2779
+ count,
2780
+ parent,
2781
+ i18nKey,
2782
+ context,
2783
+ tOptions = {},
2784
+ values,
2785
+ defaults,
2786
+ components,
2787
+ ns,
2788
+ i18n: i18nFromProps,
2789
+ t: tFromProps,
2790
+ shouldUnescape,
2791
+ ...additionalProps
2792
+ }) {
2793
+ const {
2794
+ i18n: i18nFromContext,
2795
+ defaultNS: defaultNSFromContext
2796
+ } = react.useContext(I18nContext) || {};
2797
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
2798
+ const t = tFromProps || i18n?.t.bind(i18n);
2799
+ return Trans$1({
2800
+ children,
2801
+ count,
2802
+ parent,
2803
+ i18nKey,
2804
+ context,
2805
+ tOptions,
2806
+ values,
2807
+ defaults,
2808
+ components,
2809
+ ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
2810
+ i18n,
2811
+ t: tFromProps,
2812
+ shouldUnescape,
2813
+ ...additionalProps
2814
+ });
2815
+ }
737
2816
 
738
- const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {
739
- function I18nextWithTranslation({
740
- forwardedRef,
741
- ...rest
742
- }) {
743
- const [t, i18n, ready] = useTranslation(ns, {
744
- ...rest,
745
- keyPrefix: options.keyPrefix
746
- });
747
- const passDownProps = {
748
- ...rest,
749
- t,
750
- i18n,
751
- tReady: ready
752
- };
753
- if (options.withRef && forwardedRef) {
754
- passDownProps.ref = forwardedRef;
755
- } else if (!options.withRef && forwardedRef) {
756
- passDownProps.forwardedRef = forwardedRef;
757
- }
758
- return react.createElement(WrappedComponent, passDownProps);
759
- }
760
- I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
761
- I18nextWithTranslation.WrappedComponent = WrappedComponent;
762
- const forwardRef = (props, ref) => react.createElement(I18nextWithTranslation, Object.assign({}, props, {
763
- forwardedRef: ref
764
- }));
765
- return options.withRef ? react.forwardRef(forwardRef) : I18nextWithTranslation;
766
- };
2817
+ const usePrevious = (value, ignore) => {
2818
+ const ref = react.useRef();
2819
+ react.useEffect(() => {
2820
+ ref.current = value;
2821
+ }, [value, ignore]);
2822
+ return ref.current;
2823
+ };
2824
+ const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
2825
+ const useMemoizedT = (i18n, language, namespace, keyPrefix) => react.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
2826
+ const useTranslation = (ns, props = {}) => {
2827
+ const {
2828
+ i18n: i18nFromProps
2829
+ } = props;
2830
+ const {
2831
+ i18n: i18nFromContext,
2832
+ defaultNS: defaultNSFromContext
2833
+ } = react.useContext(I18nContext) || {};
2834
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
2835
+ if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
2836
+ if (!i18n) {
2837
+ warnOnce(i18n, 'NO_I18NEXT_INSTANCE', 'useTranslation: You will need to pass in an i18next instance by using initReactI18next');
2838
+ const notReadyT = (k, optsOrDefaultValue) => {
2839
+ if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
2840
+ if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
2841
+ return Array.isArray(k) ? k[k.length - 1] : k;
2842
+ };
2843
+ const retNotReady = [notReadyT, {}, false];
2844
+ retNotReady.t = notReadyT;
2845
+ retNotReady.i18n = {};
2846
+ retNotReady.ready = false;
2847
+ return retNotReady;
2848
+ }
2849
+ if (i18n.options.react?.wait) warnOnce(i18n, 'DEPRECATED_OPTION', 'useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
2850
+ const i18nOptions = {
2851
+ ...getDefaults(),
2852
+ ...i18n.options.react,
2853
+ ...props
2854
+ };
2855
+ const {
2856
+ useSuspense,
2857
+ keyPrefix
2858
+ } = i18nOptions;
2859
+ let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
2860
+ namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
2861
+ i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
2862
+ const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
2863
+ const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
2864
+ const getT = () => memoGetT;
2865
+ const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
2866
+ const [t, setT] = react.useState(getT);
2867
+ let joinedNS = namespaces.join();
2868
+ if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
2869
+ const previousJoinedNS = usePrevious(joinedNS);
2870
+ const isMounted = react.useRef(true);
2871
+ react.useEffect(() => {
2872
+ const {
2873
+ bindI18n,
2874
+ bindI18nStore
2875
+ } = i18nOptions;
2876
+ isMounted.current = true;
2877
+ if (!ready && !useSuspense) {
2878
+ if (props.lng) {
2879
+ loadLanguages(i18n, props.lng, namespaces, () => {
2880
+ if (isMounted.current) setT(getNewT);
2881
+ });
2882
+ } else {
2883
+ loadNamespaces(i18n, namespaces, () => {
2884
+ if (isMounted.current) setT(getNewT);
2885
+ });
2886
+ }
2887
+ }
2888
+ if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
2889
+ setT(getNewT);
2890
+ }
2891
+ const boundReset = () => {
2892
+ if (isMounted.current) setT(getNewT);
2893
+ };
2894
+ if (bindI18n) i18n?.on(bindI18n, boundReset);
2895
+ if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
2896
+ return () => {
2897
+ isMounted.current = false;
2898
+ if (i18n && bindI18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
2899
+ if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
2900
+ };
2901
+ }, [i18n, joinedNS]);
2902
+ react.useEffect(() => {
2903
+ if (isMounted.current && ready) {
2904
+ setT(getT);
2905
+ }
2906
+ }, [i18n, keyPrefix, ready]);
2907
+ const ret = [t, i18n, ready];
2908
+ ret.t = t;
2909
+ ret.i18n = i18n;
2910
+ ret.ready = ready;
2911
+ if (ready) return ret;
2912
+ if (!ready && !useSuspense) return ret;
2913
+ throw new Promise(resolve => {
2914
+ if (props.lng) {
2915
+ loadLanguages(i18n, props.lng, namespaces, () => resolve());
2916
+ } else {
2917
+ loadNamespaces(i18n, namespaces, () => resolve());
2918
+ }
2919
+ });
2920
+ };
767
2921
 
768
- const Translation = ({
769
- ns,
770
- children,
771
- ...options
772
- }) => {
773
- const [t, i18n, ready] = useTranslation(ns, options);
774
- return children(t, {
775
- i18n,
776
- lng: i18n.language
777
- }, ready);
778
- };
2922
+ const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {
2923
+ function I18nextWithTranslation({
2924
+ forwardedRef,
2925
+ ...rest
2926
+ }) {
2927
+ const [t, i18n, ready] = useTranslation(ns, {
2928
+ ...rest,
2929
+ keyPrefix: options.keyPrefix
2930
+ });
2931
+ const passDownProps = {
2932
+ ...rest,
2933
+ t,
2934
+ i18n,
2935
+ tReady: ready
2936
+ };
2937
+ if (options.withRef && forwardedRef) {
2938
+ passDownProps.ref = forwardedRef;
2939
+ } else if (!options.withRef && forwardedRef) {
2940
+ passDownProps.forwardedRef = forwardedRef;
2941
+ }
2942
+ return react.createElement(WrappedComponent, passDownProps);
2943
+ }
2944
+ I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
2945
+ I18nextWithTranslation.WrappedComponent = WrappedComponent;
2946
+ const forwardRef = (props, ref) => react.createElement(I18nextWithTranslation, Object.assign({}, props, {
2947
+ forwardedRef: ref
2948
+ }));
2949
+ return options.withRef ? react.forwardRef(forwardRef) : I18nextWithTranslation;
2950
+ };
779
2951
 
780
- function I18nextProvider({
781
- i18n,
782
- defaultNS,
783
- children
784
- }) {
785
- const value = react.useMemo(() => ({
786
- i18n,
787
- defaultNS
788
- }), [i18n, defaultNS]);
789
- return react.createElement(I18nContext.Provider, {
790
- value
791
- }, children);
792
- }
2952
+ const Translation = ({
2953
+ ns,
2954
+ children,
2955
+ ...options
2956
+ }) => {
2957
+ const [t, i18n, ready] = useTranslation(ns, options);
2958
+ return children(t, {
2959
+ i18n,
2960
+ lng: i18n.language
2961
+ }, ready);
2962
+ };
793
2963
 
794
- const useSSR = (initialI18nStore, initialLanguage, props = {}) => {
795
- const {
796
- i18n: i18nFromProps
797
- } = props;
798
- const {
799
- i18n: i18nFromContext
800
- } = react.useContext(I18nContext) || {};
801
- const i18n = i18nFromProps || i18nFromContext || getI18n();
802
- if (i18n.options?.isClone) return;
803
- if (initialI18nStore && !i18n.initializedStoreOnce) {
804
- i18n.services.resourceStore.data = initialI18nStore;
805
- i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
806
- Object.keys(lngResources).forEach(ns => {
807
- if (mem.indexOf(ns) < 0) mem.push(ns);
808
- });
809
- return mem;
810
- }, i18n.options.ns);
811
- i18n.initializedStoreOnce = true;
812
- i18n.isInitialized = true;
813
- }
814
- if (initialLanguage && !i18n.initializedLanguageOnce) {
815
- i18n.changeLanguage(initialLanguage);
816
- i18n.initializedLanguageOnce = true;
817
- }
818
- };
2964
+ function I18nextProvider({
2965
+ i18n,
2966
+ defaultNS,
2967
+ children
2968
+ }) {
2969
+ const value = react.useMemo(() => ({
2970
+ i18n,
2971
+ defaultNS
2972
+ }), [i18n, defaultNS]);
2973
+ return react.createElement(I18nContext.Provider, {
2974
+ value
2975
+ }, children);
2976
+ }
819
2977
 
820
- const withSSR = () => function Extend(WrappedComponent) {
821
- function I18nextWithSSR({
822
- initialI18nStore,
823
- initialLanguage,
824
- ...rest
825
- }) {
826
- useSSR(initialI18nStore, initialLanguage);
827
- return react.createElement(WrappedComponent, {
828
- ...rest
829
- });
830
- }
831
- I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
832
- I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
833
- I18nextWithSSR.WrappedComponent = WrappedComponent;
834
- return I18nextWithSSR;
835
- };
2978
+ const useSSR = (initialI18nStore, initialLanguage, props = {}) => {
2979
+ const {
2980
+ i18n: i18nFromProps
2981
+ } = props;
2982
+ const {
2983
+ i18n: i18nFromContext
2984
+ } = react.useContext(I18nContext) || {};
2985
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
2986
+ if (i18n.options?.isClone) return;
2987
+ if (initialI18nStore && !i18n.initializedStoreOnce) {
2988
+ i18n.services.resourceStore.data = initialI18nStore;
2989
+ i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
2990
+ Object.keys(lngResources).forEach(ns => {
2991
+ if (mem.indexOf(ns) < 0) mem.push(ns);
2992
+ });
2993
+ return mem;
2994
+ }, i18n.options.ns);
2995
+ i18n.initializedStoreOnce = true;
2996
+ i18n.isInitialized = true;
2997
+ }
2998
+ if (initialLanguage && !i18n.initializedLanguageOnce) {
2999
+ i18n.changeLanguage(initialLanguage);
3000
+ i18n.initializedLanguageOnce = true;
3001
+ }
3002
+ };
836
3003
 
837
- const date = () => '';
838
- const time = () => '';
839
- const number = () => '';
840
- const select = () => '';
841
- const plural = () => '';
842
- const selectOrdinal = () => '';
3004
+ const withSSR = () => function Extend(WrappedComponent) {
3005
+ function I18nextWithSSR({
3006
+ initialI18nStore,
3007
+ initialLanguage,
3008
+ ...rest
3009
+ }) {
3010
+ useSSR(initialI18nStore, initialLanguage);
3011
+ return react.createElement(WrappedComponent, {
3012
+ ...rest
3013
+ });
3014
+ }
3015
+ I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
3016
+ I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
3017
+ I18nextWithSSR.WrappedComponent = WrappedComponent;
3018
+ return I18nextWithSSR;
3019
+ };
843
3020
 
844
- exports.I18nContext = I18nContext;
845
- exports.I18nextProvider = I18nextProvider;
846
- exports.Trans = Trans;
847
- exports.TransWithoutContext = Trans$1;
848
- exports.Translation = Translation;
849
- exports.composeInitialProps = composeInitialProps;
850
- exports.date = date;
851
- exports.getDefaults = getDefaults;
852
- exports.getI18n = getI18n;
853
- exports.getInitialProps = getInitialProps;
854
- exports.initReactI18next = initReactI18next;
855
- exports.number = number;
856
- exports.plural = plural;
857
- exports.select = select;
858
- exports.selectOrdinal = selectOrdinal;
859
- exports.setDefaults = setDefaults;
860
- exports.setI18n = setI18n;
861
- exports.time = time;
862
- exports.useSSR = useSSR;
863
- exports.useTranslation = useTranslation;
864
- exports.withSSR = withSSR;
865
- exports.withTranslation = withTranslation;
3021
+ const date = () => '';
3022
+ const time = () => '';
3023
+ const number = () => '';
3024
+ const select = () => '';
3025
+ const plural = () => '';
3026
+ const selectOrdinal = () => '';
3027
+
3028
+ exports.I18nContext = I18nContext;
3029
+ exports.I18nextProvider = I18nextProvider;
3030
+ exports.Trans = Trans;
3031
+ exports.TransWithoutContext = Trans$1;
3032
+ exports.Translation = Translation;
3033
+ exports.composeInitialProps = composeInitialProps;
3034
+ exports.date = date;
3035
+ exports.getDefaults = getDefaults;
3036
+ exports.getI18n = getI18n;
3037
+ exports.getInitialProps = getInitialProps;
3038
+ exports.initReactI18next = initReactI18next;
3039
+ exports.number = number;
3040
+ exports.plural = plural;
3041
+ exports.select = select;
3042
+ exports.selectOrdinal = selectOrdinal;
3043
+ exports.setDefaults = setDefaults;
3044
+ exports.setI18n = setI18n;
3045
+ exports.time = time;
3046
+ exports.useSSR = useSSR;
3047
+ exports.useTranslation = useTranslation;
3048
+ exports.withSSR = withSSR;
3049
+ exports.withTranslation = withTranslation;
866
3050
 
867
3051
  }));