@andre1502/react-utilities 0.9.6 → 1.0.1

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