@andre1502/react-utilities 1.0.4 → 1.0.5
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.
- package/dist/Config/GoogleAuth.js +10 -13
- package/dist/Config/GoogleAuth.js.map +1 -1
- package/dist/Config/Sitemap.js +10 -11
- package/dist/Config/Sitemap.js.map +1 -1
- package/dist/EnvironmentEnum-BjXsfSRZ.js.map +1 -1
- package/dist/EnvironmentEnum-UcQ6Il1O.js.map +1 -1
- package/dist/NumberFormat-CvvBWhHc.js.map +1 -1
- package/dist/NumberFormat-glmpbk7E.js.map +1 -1
- package/dist/React-BaJ1KfGF.js.map +1 -1
- package/dist/React-qUl0CBmE.js.map +1 -1
- package/dist/{ReactNative-Ckbnh5vm.js → ReactNative-B4gt8nBr.js} +22 -2
- package/dist/ReactNative-B4gt8nBr.js.map +1 -0
- package/dist/{ReactNative-DLA9Xwp4.js → ReactNative-CDMfQFfN.js} +22 -2
- package/dist/ReactNative-CDMfQFfN.js.map +1 -0
- package/dist/Sentry/Build.js +27 -28
- package/dist/Sentry/Build.js.map +1 -1
- package/dist/Sentry/React.js +1 -2
- package/dist/Sentry/React.js.map +1 -1
- package/dist/Sentry/ReactNative.js +1 -2
- package/dist/Sentry/ReactNative.js.map +1 -1
- package/dist/Utils/Files.js +1 -2
- package/dist/Utils/Files.js.map +1 -1
- package/dist/Utils-BC1fDy39.js.map +1 -1
- package/dist/Utils-Cq948gfa.js.map +1 -1
- package/dist/Utils-D7KRF8VU.js.map +1 -1
- package/dist/Utils-Dilye04y.js.map +1 -1
- package/dist/config-cli.cjs.map +1 -1
- package/dist/config-cli.js +38 -44
- package/dist/config-cli.js.map +1 -1
- package/dist/config-cli.mjs.map +1 -1
- package/dist/hooks.js +1 -2
- package/dist/hooks.js.map +1 -1
- package/dist/i18n.cjs +216 -290
- package/dist/i18n.cjs.map +1 -1
- package/dist/i18n.mjs +216 -290
- package/dist/i18n.mjs.map +1 -1
- package/dist/index-rn.cjs +1 -1
- package/dist/index-rn.mjs +1 -1
- package/dist/sentry-cli.cjs.map +1 -1
- package/dist/sentry-cli.js +8 -9
- package/dist/sentry-cli.js.map +1 -1
- package/dist/sentry-cli.mjs.map +1 -1
- package/dist/sentry-rn.cjs +1 -1
- package/dist/sentry-rn.mjs +1 -1
- package/dist/useWebSocket-B_B6kWXM.js.map +1 -1
- package/dist/useWebSocket-YQWaiBz9.js.map +1 -1
- package/package.json +30 -26
- package/dist/ReactNative-Ckbnh5vm.js.map +0 -1
- package/dist/ReactNative-DLA9Xwp4.js.map +0 -1
package/dist/i18n.cjs
CHANGED
@@ -161,8 +161,7 @@ const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
|
|
161
161
|
}
|
162
162
|
return matched;
|
163
163
|
};
|
164
|
-
const deepFind =
|
165
|
-
let keySeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';
|
164
|
+
const deepFind = (obj, path, keySeparator = '.') => {
|
166
165
|
if (!obj) return undefined;
|
167
166
|
if (obj[path]) {
|
168
167
|
if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
|
@@ -212,39 +211,25 @@ const consoleLogger = {
|
|
212
211
|
}
|
213
212
|
};
|
214
213
|
class Logger {
|
215
|
-
constructor(concreteLogger) {
|
216
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
214
|
+
constructor(concreteLogger, options = {}) {
|
217
215
|
this.init(concreteLogger, options);
|
218
216
|
}
|
219
|
-
init(concreteLogger) {
|
220
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
217
|
+
init(concreteLogger, options = {}) {
|
221
218
|
this.prefix = options.prefix || 'i18next:';
|
222
219
|
this.logger = concreteLogger || consoleLogger;
|
223
220
|
this.options = options;
|
224
221
|
this.debug = options.debug;
|
225
222
|
}
|
226
|
-
log() {
|
227
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
228
|
-
args[_key] = arguments[_key];
|
229
|
-
}
|
223
|
+
log(...args) {
|
230
224
|
return this.forward(args, 'log', '', true);
|
231
225
|
}
|
232
|
-
warn() {
|
233
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
234
|
-
args[_key2] = arguments[_key2];
|
235
|
-
}
|
226
|
+
warn(...args) {
|
236
227
|
return this.forward(args, 'warn', '', true);
|
237
228
|
}
|
238
|
-
error() {
|
239
|
-
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
240
|
-
args[_key3] = arguments[_key3];
|
241
|
-
}
|
229
|
+
error(...args) {
|
242
230
|
return this.forward(args, 'error', '');
|
243
231
|
}
|
244
|
-
deprecate() {
|
245
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
246
|
-
args[_key4] = arguments[_key4];
|
247
|
-
}
|
232
|
+
deprecate(...args) {
|
248
233
|
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
|
249
234
|
}
|
250
235
|
forward(args, lvl, prefix, debugOnly) {
|
@@ -288,14 +273,10 @@ class EventEmitter {
|
|
288
273
|
}
|
289
274
|
this.observers[event].delete(listener);
|
290
275
|
}
|
291
|
-
emit(event) {
|
292
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
293
|
-
args[_key - 1] = arguments[_key];
|
294
|
-
}
|
276
|
+
emit(event, ...args) {
|
295
277
|
if (this.observers[event]) {
|
296
278
|
const cloned = Array.from(this.observers[event].entries());
|
297
|
-
cloned.forEach(
|
298
|
-
let [observer, numTimesAdded] = _ref;
|
279
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
299
280
|
for (let i = 0; i < numTimesAdded; i++) {
|
300
281
|
observer(...args);
|
301
282
|
}
|
@@ -303,8 +284,7 @@ class EventEmitter {
|
|
303
284
|
}
|
304
285
|
if (this.observers['*']) {
|
305
286
|
const cloned = Array.from(this.observers['*'].entries());
|
306
|
-
cloned.forEach(
|
307
|
-
let [observer, numTimesAdded] = _ref2;
|
287
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
308
288
|
for (let i = 0; i < numTimesAdded; i++) {
|
309
289
|
observer.apply(observer, [event, ...args]);
|
310
290
|
}
|
@@ -314,11 +294,10 @@ class EventEmitter {
|
|
314
294
|
}
|
315
295
|
|
316
296
|
class ResourceStore extends EventEmitter {
|
317
|
-
constructor(data
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
};
|
297
|
+
constructor(data, options = {
|
298
|
+
ns: ['translation'],
|
299
|
+
defaultNS: 'translation'
|
300
|
+
}) {
|
322
301
|
super();
|
323
302
|
this.data = data || {};
|
324
303
|
this.options = options;
|
@@ -340,8 +319,7 @@ class ResourceStore extends EventEmitter {
|
|
340
319
|
this.options.ns.splice(index, 1);
|
341
320
|
}
|
342
321
|
}
|
343
|
-
getResource(lng, ns, key) {
|
344
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
322
|
+
getResource(lng, ns, key, options = {}) {
|
345
323
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
346
324
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
347
325
|
let path;
|
@@ -368,10 +346,9 @@ class ResourceStore extends EventEmitter {
|
|
368
346
|
if (result || !ignoreJSONStructure || !isString(key)) return result;
|
369
347
|
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
370
348
|
}
|
371
|
-
addResource(lng, ns, key, value
|
372
|
-
|
373
|
-
|
374
|
-
};
|
349
|
+
addResource(lng, ns, key, value, options = {
|
350
|
+
silent: false
|
351
|
+
}) {
|
375
352
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
376
353
|
let path = [lng, ns];
|
377
354
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
@@ -384,10 +361,9 @@ class ResourceStore extends EventEmitter {
|
|
384
361
|
setPath(this.data, path, value);
|
385
362
|
if (!options.silent) this.emit('added', lng, ns, key, value);
|
386
363
|
}
|
387
|
-
addResources(lng, ns, resources
|
388
|
-
|
389
|
-
|
390
|
-
};
|
364
|
+
addResources(lng, ns, resources, options = {
|
365
|
+
silent: false
|
366
|
+
}) {
|
391
367
|
for (const m in resources) {
|
392
368
|
if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
|
393
369
|
silent: true
|
@@ -395,11 +371,10 @@ class ResourceStore extends EventEmitter {
|
|
395
371
|
}
|
396
372
|
if (!options.silent) this.emit('added', lng, ns, resources);
|
397
373
|
}
|
398
|
-
addResourceBundle(lng, ns, resources, deep, overwrite
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
};
|
374
|
+
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
375
|
+
silent: false,
|
376
|
+
skipCopy: false
|
377
|
+
}) {
|
403
378
|
let path = [lng, ns];
|
404
379
|
if (lng.indexOf('.') > -1) {
|
405
380
|
path = lng.split('.');
|
@@ -464,8 +439,7 @@ var postProcessor = {
|
|
464
439
|
const checkedLoadedFor = {};
|
465
440
|
const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' && typeof res !== 'number';
|
466
441
|
class Translator extends EventEmitter {
|
467
|
-
constructor(services) {
|
468
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
442
|
+
constructor(services, options = {}) {
|
469
443
|
super();
|
470
444
|
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
|
471
445
|
this.options = options;
|
@@ -477,23 +451,23 @@ class Translator extends EventEmitter {
|
|
477
451
|
changeLanguage(lng) {
|
478
452
|
if (lng) this.language = lng;
|
479
453
|
}
|
480
|
-
exists(key
|
481
|
-
|
482
|
-
|
454
|
+
exists(key, o = {
|
455
|
+
interpolation: {}
|
456
|
+
}) {
|
457
|
+
const opt = {
|
458
|
+
...o
|
483
459
|
};
|
484
|
-
if (key == null)
|
485
|
-
|
486
|
-
}
|
487
|
-
const resolved = this.resolve(key, options);
|
460
|
+
if (key == null) return false;
|
461
|
+
const resolved = this.resolve(key, opt);
|
488
462
|
return resolved?.res !== undefined;
|
489
463
|
}
|
490
|
-
extractFromKey(key,
|
491
|
-
let nsSeparator =
|
464
|
+
extractFromKey(key, opt) {
|
465
|
+
let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
|
492
466
|
if (nsSeparator === undefined) nsSeparator = ':';
|
493
|
-
const keySeparator =
|
494
|
-
let namespaces =
|
467
|
+
const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
|
468
|
+
let namespaces = opt.ns || this.options.defaultNS || [];
|
495
469
|
const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
|
496
|
-
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !
|
470
|
+
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
|
497
471
|
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
|
498
472
|
const m = key.match(this.interpolator.nestingRegexp);
|
499
473
|
if (m && m.length > 0) {
|
@@ -511,28 +485,32 @@ class Translator extends EventEmitter {
|
|
511
485
|
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
512
486
|
};
|
513
487
|
}
|
514
|
-
translate(keys,
|
515
|
-
|
516
|
-
|
488
|
+
translate(keys, o, lastKey) {
|
489
|
+
let opt = typeof o === 'object' ? {
|
490
|
+
...o
|
491
|
+
} : o;
|
492
|
+
if (typeof opt !== 'object' && this.options.overloadTranslationOptionHandler) {
|
493
|
+
opt = this.options.overloadTranslationOptionHandler(arguments);
|
517
494
|
}
|
518
|
-
if (typeof options === 'object')
|
519
|
-
...
|
495
|
+
if (typeof options === 'object') opt = {
|
496
|
+
...opt
|
520
497
|
};
|
521
|
-
if (!
|
498
|
+
if (!opt) opt = {};
|
522
499
|
if (keys == null) return '';
|
523
500
|
if (!Array.isArray(keys)) keys = [String(keys)];
|
524
|
-
const returnDetails =
|
525
|
-
const keySeparator =
|
501
|
+
const returnDetails = opt.returnDetails !== undefined ? opt.returnDetails : this.options.returnDetails;
|
502
|
+
const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
|
526
503
|
const {
|
527
504
|
key,
|
528
505
|
namespaces
|
529
|
-
} = this.extractFromKey(keys[keys.length - 1],
|
506
|
+
} = this.extractFromKey(keys[keys.length - 1], opt);
|
530
507
|
const namespace = namespaces[namespaces.length - 1];
|
531
|
-
|
532
|
-
|
508
|
+
let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
|
509
|
+
if (nsSeparator === undefined) nsSeparator = ':';
|
510
|
+
const lng = opt.lng || this.language;
|
511
|
+
const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
|
533
512
|
if (lng?.toLowerCase() === 'cimode') {
|
534
513
|
if (appendNamespaceToCIMode) {
|
535
|
-
const nsSeparator = options.nsSeparator || this.options.nsSeparator;
|
536
514
|
if (returnDetails) {
|
537
515
|
return {
|
538
516
|
res: `${namespace}${nsSeparator}${key}`,
|
@@ -540,7 +518,7 @@ class Translator extends EventEmitter {
|
|
540
518
|
exactUsedKey: key,
|
541
519
|
usedLng: lng,
|
542
520
|
usedNS: namespace,
|
543
|
-
usedParams: this.getUsedParamsDetails(
|
521
|
+
usedParams: this.getUsedParamsDetails(opt)
|
544
522
|
};
|
545
523
|
}
|
546
524
|
return `${namespace}${nsSeparator}${key}`;
|
@@ -552,26 +530,26 @@ class Translator extends EventEmitter {
|
|
552
530
|
exactUsedKey: key,
|
553
531
|
usedLng: lng,
|
554
532
|
usedNS: namespace,
|
555
|
-
usedParams: this.getUsedParamsDetails(
|
533
|
+
usedParams: this.getUsedParamsDetails(opt)
|
556
534
|
};
|
557
535
|
}
|
558
536
|
return key;
|
559
537
|
}
|
560
|
-
const resolved = this.resolve(keys,
|
538
|
+
const resolved = this.resolve(keys, opt);
|
561
539
|
let res = resolved?.res;
|
562
540
|
const resUsedKey = resolved?.usedKey || key;
|
563
541
|
const resExactUsedKey = resolved?.exactUsedKey || key;
|
564
542
|
const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
|
565
|
-
const joinArrays =
|
543
|
+
const joinArrays = opt.joinArrays !== undefined ? opt.joinArrays : this.options.joinArrays;
|
566
544
|
const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
|
567
|
-
const needsPluralHandling =
|
568
|
-
const hasDefaultValue = Translator.hasDefaultValue(
|
569
|
-
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng,
|
570
|
-
const defaultValueSuffixOrdinalFallback =
|
545
|
+
const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
|
546
|
+
const hasDefaultValue = Translator.hasDefaultValue(opt);
|
547
|
+
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : '';
|
548
|
+
const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
|
571
549
|
ordinal: false
|
572
550
|
}) : '';
|
573
|
-
const needsZeroSuffixLookup = needsPluralHandling && !
|
574
|
-
const defaultValue = needsZeroSuffixLookup &&
|
551
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
552
|
+
const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
|
575
553
|
let resForObjHndl = res;
|
576
554
|
if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
|
577
555
|
resForObjHndl = defaultValue;
|
@@ -579,17 +557,17 @@ class Translator extends EventEmitter {
|
|
579
557
|
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
580
558
|
const resType = Object.prototype.toString.apply(resForObjHndl);
|
581
559
|
if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
|
582
|
-
if (!
|
560
|
+
if (!opt.returnObjects && !this.options.returnObjects) {
|
583
561
|
if (!this.options.returnedObjectHandler) {
|
584
562
|
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
585
563
|
}
|
586
564
|
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
587
|
-
...
|
565
|
+
...opt,
|
588
566
|
ns: namespaces
|
589
567
|
}) : `key '${key} (${this.language})' returned an object instead of string.`;
|
590
568
|
if (returnDetails) {
|
591
569
|
resolved.res = r;
|
592
|
-
resolved.usedParams = this.getUsedParamsDetails(
|
570
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
593
571
|
return resolved;
|
594
572
|
}
|
595
573
|
return r;
|
@@ -603,7 +581,7 @@ class Translator extends EventEmitter {
|
|
603
581
|
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
|
604
582
|
if (hasDefaultValue && !res) {
|
605
583
|
copy[m] = this.translate(deepKey, {
|
606
|
-
...
|
584
|
+
...opt,
|
607
585
|
defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : undefined,
|
608
586
|
...{
|
609
587
|
joinArrays: false,
|
@@ -612,7 +590,7 @@ class Translator extends EventEmitter {
|
|
612
590
|
});
|
613
591
|
} else {
|
614
592
|
copy[m] = this.translate(deepKey, {
|
615
|
-
...
|
593
|
+
...opt,
|
616
594
|
...{
|
617
595
|
joinArrays: false,
|
618
596
|
ns: namespaces
|
@@ -626,7 +604,7 @@ class Translator extends EventEmitter {
|
|
626
604
|
}
|
627
605
|
} else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
|
628
606
|
res = res.join(joinArrays);
|
629
|
-
if (res) res = this.extendTranslation(res, keys,
|
607
|
+
if (res) res = this.extendTranslation(res, keys, opt, lastKey);
|
630
608
|
} else {
|
631
609
|
let usedDefault = false;
|
632
610
|
let usedKey = false;
|
@@ -638,47 +616,47 @@ class Translator extends EventEmitter {
|
|
638
616
|
usedKey = true;
|
639
617
|
res = key;
|
640
618
|
}
|
641
|
-
const missingKeyNoValueFallbackToKey =
|
619
|
+
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
642
620
|
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
|
643
621
|
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
644
622
|
if (usedKey || usedDefault || updateMissing) {
|
645
623
|
this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
|
646
624
|
if (keySeparator) {
|
647
625
|
const fk = this.resolve(key, {
|
648
|
-
...
|
626
|
+
...opt,
|
649
627
|
keySeparator: false
|
650
628
|
});
|
651
629
|
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.');
|
652
630
|
}
|
653
631
|
let lngs = [];
|
654
|
-
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng,
|
632
|
+
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
|
655
633
|
if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
|
656
634
|
for (let i = 0; i < fallbackLngs.length; i++) {
|
657
635
|
lngs.push(fallbackLngs[i]);
|
658
636
|
}
|
659
637
|
} else if (this.options.saveMissingTo === 'all') {
|
660
|
-
lngs = this.languageUtils.toResolveHierarchy(
|
638
|
+
lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
|
661
639
|
} else {
|
662
|
-
lngs.push(
|
640
|
+
lngs.push(opt.lng || this.language);
|
663
641
|
}
|
664
642
|
const send = (l, k, specificDefaultValue) => {
|
665
643
|
const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
|
666
644
|
if (this.options.missingKeyHandler) {
|
667
|
-
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing,
|
645
|
+
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
668
646
|
} else if (this.backendConnector?.saveMissing) {
|
669
|
-
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing,
|
647
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
670
648
|
}
|
671
649
|
this.emit('missingKey', l, namespace, k, res);
|
672
650
|
};
|
673
651
|
if (this.options.saveMissing) {
|
674
652
|
if (this.options.saveMissingPlurals && needsPluralHandling) {
|
675
653
|
lngs.forEach(language => {
|
676
|
-
const suffixes = this.pluralResolver.getSuffixes(language,
|
677
|
-
if (needsZeroSuffixLookup &&
|
654
|
+
const suffixes = this.pluralResolver.getSuffixes(language, opt);
|
655
|
+
if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
|
678
656
|
suffixes.push(`${this.options.pluralSeparator}zero`);
|
679
657
|
}
|
680
658
|
suffixes.forEach(suffix => {
|
681
|
-
send([language], key + suffix,
|
659
|
+
send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
|
682
660
|
});
|
683
661
|
});
|
684
662
|
} else {
|
@@ -686,83 +664,80 @@ class Translator extends EventEmitter {
|
|
686
664
|
}
|
687
665
|
}
|
688
666
|
}
|
689
|
-
res = this.extendTranslation(res, keys,
|
690
|
-
if (usedKey && res === key && this.options.appendNamespaceToMissingKey)
|
667
|
+
res = this.extendTranslation(res, keys, opt, resolved, lastKey);
|
668
|
+
if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
|
669
|
+
res = `${namespace}${nsSeparator}${key}`;
|
670
|
+
}
|
691
671
|
if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
|
692
|
-
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}
|
672
|
+
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : undefined, opt);
|
693
673
|
}
|
694
674
|
}
|
695
675
|
if (returnDetails) {
|
696
676
|
resolved.res = res;
|
697
|
-
resolved.usedParams = this.getUsedParamsDetails(
|
677
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
698
678
|
return resolved;
|
699
679
|
}
|
700
680
|
return res;
|
701
681
|
}
|
702
|
-
extendTranslation(res, key,
|
703
|
-
var _this = this;
|
682
|
+
extendTranslation(res, key, opt, resolved, lastKey) {
|
704
683
|
if (this.i18nFormat?.parse) {
|
705
684
|
res = this.i18nFormat.parse(res, {
|
706
685
|
...this.options.interpolation.defaultVariables,
|
707
|
-
...
|
708
|
-
},
|
686
|
+
...opt
|
687
|
+
}, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
|
709
688
|
resolved
|
710
689
|
});
|
711
|
-
} else if (!
|
712
|
-
if (
|
713
|
-
...
|
690
|
+
} else if (!opt.skipInterpolation) {
|
691
|
+
if (opt.interpolation) this.interpolator.init({
|
692
|
+
...opt,
|
714
693
|
...{
|
715
694
|
interpolation: {
|
716
695
|
...this.options.interpolation,
|
717
|
-
...
|
696
|
+
...opt.interpolation
|
718
697
|
}
|
719
698
|
}
|
720
699
|
});
|
721
|
-
const skipOnVariables = isString(res) && (
|
700
|
+
const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== undefined ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
|
722
701
|
let nestBef;
|
723
702
|
if (skipOnVariables) {
|
724
703
|
const nb = res.match(this.interpolator.nestingRegexp);
|
725
704
|
nestBef = nb && nb.length;
|
726
705
|
}
|
727
|
-
let data =
|
706
|
+
let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
|
728
707
|
if (this.options.interpolation.defaultVariables) data = {
|
729
708
|
...this.options.interpolation.defaultVariables,
|
730
709
|
...data
|
731
710
|
};
|
732
|
-
res = this.interpolator.interpolate(res, data,
|
711
|
+
res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
|
733
712
|
if (skipOnVariables) {
|
734
713
|
const na = res.match(this.interpolator.nestingRegexp);
|
735
714
|
const nestAft = na && na.length;
|
736
|
-
if (nestBef < nestAft)
|
715
|
+
if (nestBef < nestAft) opt.nest = false;
|
737
716
|
}
|
738
|
-
if (!
|
739
|
-
if (
|
740
|
-
|
741
|
-
args[
|
742
|
-
}
|
743
|
-
if (lastKey?.[0] === args[0] && !options.context) {
|
744
|
-
_this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
717
|
+
if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
|
718
|
+
if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
|
719
|
+
if (lastKey?.[0] === args[0] && !opt.context) {
|
720
|
+
this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
745
721
|
return null;
|
746
722
|
}
|
747
|
-
return
|
748
|
-
},
|
749
|
-
if (
|
723
|
+
return this.translate(...args, key);
|
724
|
+
}, opt);
|
725
|
+
if (opt.interpolation) this.interpolator.reset();
|
750
726
|
}
|
751
|
-
const postProcess =
|
727
|
+
const postProcess = opt.postProcess || this.options.postProcess;
|
752
728
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
753
|
-
if (res != null && postProcessorNames?.length &&
|
729
|
+
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
754
730
|
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
755
731
|
i18nResolved: {
|
756
732
|
...resolved,
|
757
|
-
usedParams: this.getUsedParamsDetails(
|
733
|
+
usedParams: this.getUsedParamsDetails(opt)
|
758
734
|
},
|
759
|
-
...
|
760
|
-
} :
|
735
|
+
...opt
|
736
|
+
} : opt, this);
|
761
737
|
}
|
762
738
|
return res;
|
763
739
|
}
|
764
|
-
resolve(keys) {
|
765
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
740
|
+
resolve(keys, opt = {}) {
|
766
741
|
let found;
|
767
742
|
let usedKey;
|
768
743
|
let exactUsedKey;
|
@@ -771,15 +746,15 @@ class Translator extends EventEmitter {
|
|
771
746
|
if (isString(keys)) keys = [keys];
|
772
747
|
keys.forEach(k => {
|
773
748
|
if (this.isValidLookup(found)) return;
|
774
|
-
const extracted = this.extractFromKey(k,
|
749
|
+
const extracted = this.extractFromKey(k, opt);
|
775
750
|
const key = extracted.key;
|
776
751
|
usedKey = key;
|
777
752
|
let namespaces = extracted.namespaces;
|
778
753
|
if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
|
779
|
-
const needsPluralHandling =
|
780
|
-
const needsZeroSuffixLookup = needsPluralHandling && !
|
781
|
-
const needsContextHandling =
|
782
|
-
const codes =
|
754
|
+
const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
|
755
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
756
|
+
const needsContextHandling = opt.context !== undefined && (isString(opt.context) || typeof opt.context === 'number') && opt.context !== '';
|
757
|
+
const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
|
783
758
|
namespaces.forEach(ns => {
|
784
759
|
if (this.isValidLookup(found)) return;
|
785
760
|
usedNS = ns;
|
@@ -792,15 +767,15 @@ class Translator extends EventEmitter {
|
|
792
767
|
usedLng = code;
|
793
768
|
const finalKeys = [key];
|
794
769
|
if (this.i18nFormat?.addLookupKeys) {
|
795
|
-
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns,
|
770
|
+
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
|
796
771
|
} else {
|
797
772
|
let pluralSuffix;
|
798
|
-
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code,
|
773
|
+
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
|
799
774
|
const zeroSuffix = `${this.options.pluralSeparator}zero`;
|
800
775
|
const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
|
801
776
|
if (needsPluralHandling) {
|
802
777
|
finalKeys.push(key + pluralSuffix);
|
803
|
-
if (
|
778
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
804
779
|
finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
805
780
|
}
|
806
781
|
if (needsZeroSuffixLookup) {
|
@@ -808,11 +783,11 @@ class Translator extends EventEmitter {
|
|
808
783
|
}
|
809
784
|
}
|
810
785
|
if (needsContextHandling) {
|
811
|
-
const contextKey = `${key}${this.options.contextSeparator}${
|
786
|
+
const contextKey = `${key}${this.options.contextSeparator}${opt.context}`;
|
812
787
|
finalKeys.push(contextKey);
|
813
788
|
if (needsPluralHandling) {
|
814
789
|
finalKeys.push(contextKey + pluralSuffix);
|
815
|
-
if (
|
790
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
816
791
|
finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
817
792
|
}
|
818
793
|
if (needsZeroSuffixLookup) {
|
@@ -825,7 +800,7 @@ class Translator extends EventEmitter {
|
|
825
800
|
while (possibleKey = finalKeys.pop()) {
|
826
801
|
if (!this.isValidLookup(found)) {
|
827
802
|
exactUsedKey = possibleKey;
|
828
|
-
found = this.getResource(code, ns, possibleKey,
|
803
|
+
found = this.getResource(code, ns, possibleKey, opt);
|
829
804
|
}
|
830
805
|
}
|
831
806
|
});
|
@@ -842,13 +817,11 @@ class Translator extends EventEmitter {
|
|
842
817
|
isValidLookup(res) {
|
843
818
|
return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
|
844
819
|
}
|
845
|
-
getResource(code, ns, key) {
|
846
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
820
|
+
getResource(code, ns, key, options = {}) {
|
847
821
|
if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
|
848
822
|
return this.resourceStore.getResource(code, ns, key, options);
|
849
823
|
}
|
850
|
-
getUsedParamsDetails() {
|
851
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
824
|
+
getUsedParamsDetails(options = {}) {
|
852
825
|
const optionsKeys = ['defaultValue', 'ordinal', 'context', 'replace', 'lng', 'lngs', 'fallbackLng', 'ns', 'keySeparator', 'nsSeparator', 'returnObjects', 'returnDetails', 'joinArrays', 'postProcess', 'interpolation'];
|
853
826
|
const useOptionsReplaceForData = options.replace && !isString(options.replace);
|
854
827
|
let data = useOptionsReplaceForData ? options.replace : options;
|
@@ -937,6 +910,8 @@ class LanguageUtil {
|
|
937
910
|
if (!found && this.options.supportedLngs) {
|
938
911
|
codes.forEach(code => {
|
939
912
|
if (found) return;
|
913
|
+
const lngScOnly = this.getScriptPartFromCode(code);
|
914
|
+
if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
|
940
915
|
const lngOnly = this.getLanguagePartFromCode(code);
|
941
916
|
if (this.isSupportedCode(lngOnly)) return found = lngOnly;
|
942
917
|
found = this.options.supportedLngs.find(supportedLng => {
|
@@ -964,7 +939,7 @@ class LanguageUtil {
|
|
964
939
|
return found || [];
|
965
940
|
}
|
966
941
|
toResolveHierarchy(code, fallbackCode) {
|
967
|
-
const fallbackCodes = this.getFallbackCodes(fallbackCode || this.options.fallbackLng || [], code);
|
942
|
+
const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
|
968
943
|
const codes = [];
|
969
944
|
const addCode = c => {
|
970
945
|
if (!c) return;
|
@@ -1003,8 +978,7 @@ const dummyRule = {
|
|
1003
978
|
})
|
1004
979
|
};
|
1005
980
|
class PluralResolver {
|
1006
|
-
constructor(languageUtils) {
|
1007
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
981
|
+
constructor(languageUtils, options = {}) {
|
1008
982
|
this.languageUtils = languageUtils;
|
1009
983
|
this.options = options;
|
1010
984
|
this.logger = baseLogger.create('pluralResolver');
|
@@ -1016,8 +990,7 @@ class PluralResolver {
|
|
1016
990
|
clearCache() {
|
1017
991
|
this.pluralRulesCache = {};
|
1018
992
|
}
|
1019
|
-
getRule(code) {
|
1020
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
993
|
+
getRule(code, options = {}) {
|
1021
994
|
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
|
1022
995
|
const type = options.ordinal ? 'ordinal' : 'cardinal';
|
1023
996
|
const cacheKey = JSON.stringify({
|
@@ -1044,25 +1017,21 @@ class PluralResolver {
|
|
1044
1017
|
this.pluralRulesCache[cacheKey] = rule;
|
1045
1018
|
return rule;
|
1046
1019
|
}
|
1047
|
-
needsPlural(code) {
|
1048
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1020
|
+
needsPlural(code, options = {}) {
|
1049
1021
|
let rule = this.getRule(code, options);
|
1050
1022
|
if (!rule) rule = this.getRule('dev', options);
|
1051
1023
|
return rule?.resolvedOptions().pluralCategories.length > 1;
|
1052
1024
|
}
|
1053
|
-
getPluralFormsOfKey(code, key) {
|
1054
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
1025
|
+
getPluralFormsOfKey(code, key, options = {}) {
|
1055
1026
|
return this.getSuffixes(code, options).map(suffix => `${key}${suffix}`);
|
1056
1027
|
}
|
1057
|
-
getSuffixes(code) {
|
1058
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1028
|
+
getSuffixes(code, options = {}) {
|
1059
1029
|
let rule = this.getRule(code, options);
|
1060
1030
|
if (!rule) rule = this.getRule('dev', options);
|
1061
1031
|
if (!rule) return [];
|
1062
1032
|
return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
|
1063
1033
|
}
|
1064
|
-
getSuffix(code, count) {
|
1065
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
1034
|
+
getSuffix(code, count, options = {}) {
|
1066
1035
|
const rule = this.getRule(code, options);
|
1067
1036
|
if (rule) {
|
1068
1037
|
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
|
@@ -1072,9 +1041,7 @@ class PluralResolver {
|
|
1072
1041
|
}
|
1073
1042
|
}
|
1074
1043
|
|
1075
|
-
const deepFindWithDefaults =
|
1076
|
-
let keySeparator = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '.';
|
1077
|
-
let ignoreJSONStructure = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
|
1044
|
+
const deepFindWithDefaults = (data, defaultData, key, keySeparator = '.', ignoreJSONStructure = true) => {
|
1078
1045
|
let path = getPathWithDefaults(data, defaultData, key);
|
1079
1046
|
if (!path && ignoreJSONStructure && isString(key)) {
|
1080
1047
|
path = deepFind(data, key, keySeparator);
|
@@ -1084,15 +1051,13 @@ const deepFindWithDefaults = function (data, defaultData, key) {
|
|
1084
1051
|
};
|
1085
1052
|
const regexSafe = val => val.replace(/\$/g, '$$$$');
|
1086
1053
|
class Interpolator {
|
1087
|
-
constructor() {
|
1088
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
1054
|
+
constructor(options = {}) {
|
1089
1055
|
this.logger = baseLogger.create('interpolator');
|
1090
1056
|
this.options = options;
|
1091
1057
|
this.format = options?.interpolation?.format || (value => value);
|
1092
1058
|
this.init(options);
|
1093
1059
|
}
|
1094
|
-
init() {
|
1095
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
1060
|
+
init(options = {}) {
|
1096
1061
|
if (!options.interpolation) options.interpolation = {
|
1097
1062
|
escapeValue: true
|
1098
1063
|
};
|
@@ -1215,8 +1180,7 @@ class Interpolator {
|
|
1215
1180
|
});
|
1216
1181
|
return str;
|
1217
1182
|
}
|
1218
|
-
nest(str, fc) {
|
1219
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
1183
|
+
nest(str, fc, options = {}) {
|
1220
1184
|
let match;
|
1221
1185
|
let value;
|
1222
1186
|
let clonedOptions;
|
@@ -1313,68 +1277,68 @@ const parseFormatStr = formatStr => {
|
|
1313
1277
|
};
|
1314
1278
|
const createCachedFormatter = fn => {
|
1315
1279
|
const cache = {};
|
1316
|
-
return (
|
1317
|
-
let optForCache =
|
1318
|
-
if (
|
1280
|
+
return (v, l, o) => {
|
1281
|
+
let optForCache = o;
|
1282
|
+
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
1319
1283
|
optForCache = {
|
1320
1284
|
...optForCache,
|
1321
|
-
[
|
1285
|
+
[o.interpolationkey]: undefined
|
1322
1286
|
};
|
1323
1287
|
}
|
1324
|
-
const key =
|
1325
|
-
let
|
1326
|
-
if (!
|
1327
|
-
|
1328
|
-
cache[key] =
|
1288
|
+
const key = l + JSON.stringify(optForCache);
|
1289
|
+
let frm = cache[key];
|
1290
|
+
if (!frm) {
|
1291
|
+
frm = fn(getCleanedCode(l), o);
|
1292
|
+
cache[key] = frm;
|
1329
1293
|
}
|
1330
|
-
return
|
1294
|
+
return frm(v);
|
1331
1295
|
};
|
1332
1296
|
};
|
1297
|
+
const createNonCachedFormatter = fn => (v, l, o) => fn(getCleanedCode(l), o)(v);
|
1333
1298
|
class Formatter {
|
1334
|
-
constructor() {
|
1335
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
1299
|
+
constructor(options = {}) {
|
1336
1300
|
this.logger = baseLogger.create('formatter');
|
1337
1301
|
this.options = options;
|
1302
|
+
this.init(options);
|
1303
|
+
}
|
1304
|
+
init(services, options = {
|
1305
|
+
interpolation: {}
|
1306
|
+
}) {
|
1307
|
+
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
1308
|
+
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
1338
1309
|
this.formats = {
|
1339
|
-
number:
|
1310
|
+
number: cf((lng, opt) => {
|
1340
1311
|
const formatter = new Intl.NumberFormat(lng, {
|
1341
1312
|
...opt
|
1342
1313
|
});
|
1343
1314
|
return val => formatter.format(val);
|
1344
1315
|
}),
|
1345
|
-
currency:
|
1316
|
+
currency: cf((lng, opt) => {
|
1346
1317
|
const formatter = new Intl.NumberFormat(lng, {
|
1347
1318
|
...opt,
|
1348
1319
|
style: 'currency'
|
1349
1320
|
});
|
1350
1321
|
return val => formatter.format(val);
|
1351
1322
|
}),
|
1352
|
-
datetime:
|
1323
|
+
datetime: cf((lng, opt) => {
|
1353
1324
|
const formatter = new Intl.DateTimeFormat(lng, {
|
1354
1325
|
...opt
|
1355
1326
|
});
|
1356
1327
|
return val => formatter.format(val);
|
1357
1328
|
}),
|
1358
|
-
relativetime:
|
1329
|
+
relativetime: cf((lng, opt) => {
|
1359
1330
|
const formatter = new Intl.RelativeTimeFormat(lng, {
|
1360
1331
|
...opt
|
1361
1332
|
});
|
1362
1333
|
return val => formatter.format(val, opt.range || 'day');
|
1363
1334
|
}),
|
1364
|
-
list:
|
1335
|
+
list: cf((lng, opt) => {
|
1365
1336
|
const formatter = new Intl.ListFormat(lng, {
|
1366
1337
|
...opt
|
1367
1338
|
});
|
1368
1339
|
return val => formatter.format(val);
|
1369
1340
|
})
|
1370
1341
|
};
|
1371
|
-
this.init(options);
|
1372
|
-
}
|
1373
|
-
init(services) {
|
1374
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
|
1375
|
-
interpolation: {}
|
1376
|
-
};
|
1377
|
-
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
1378
1342
|
}
|
1379
1343
|
add(name, fc) {
|
1380
1344
|
this.formats[name.toLowerCase().trim()] = fc;
|
@@ -1382,8 +1346,7 @@ class Formatter {
|
|
1382
1346
|
addCached(name, fc) {
|
1383
1347
|
this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
|
1384
1348
|
}
|
1385
|
-
format(value, format, lng) {
|
1386
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
1349
|
+
format(value, format, lng, options = {}) {
|
1387
1350
|
const formats = format.split(this.formatSeparator);
|
1388
1351
|
if (formats.length > 1 && formats[0].indexOf('(') > 1 && formats[0].indexOf(')') < 0 && formats.find(f => f.indexOf(')') > -1)) {
|
1389
1352
|
const lastIndex = formats.findIndex(f => f.indexOf(')') > -1);
|
@@ -1424,8 +1387,7 @@ const removePending = (q, name) => {
|
|
1424
1387
|
}
|
1425
1388
|
};
|
1426
1389
|
class Connector extends EventEmitter {
|
1427
|
-
constructor(backend, store, services) {
|
1428
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
1390
|
+
constructor(backend, store, services, options = {}) {
|
1429
1391
|
super();
|
1430
1392
|
this.backend = backend;
|
1431
1393
|
this.store = store;
|
@@ -1519,10 +1481,7 @@ class Connector extends EventEmitter {
|
|
1519
1481
|
this.emit('loaded', loaded);
|
1520
1482
|
this.queue = this.queue.filter(q => !q.done);
|
1521
1483
|
}
|
1522
|
-
read(lng, ns, fcName) {
|
1523
|
-
let tried = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
1524
|
-
let wait = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : this.retryTimeout;
|
1525
|
-
let callback = arguments.length > 5 ? arguments[5] : undefined;
|
1484
|
+
read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
|
1526
1485
|
if (!lng.length) return callback(null, {});
|
1527
1486
|
if (this.readingCalls >= this.maxParallelReads) {
|
1528
1487
|
this.waitingReads.push({
|
@@ -1566,9 +1525,7 @@ class Connector extends EventEmitter {
|
|
1566
1525
|
}
|
1567
1526
|
return fc(lng, ns, resolver);
|
1568
1527
|
}
|
1569
|
-
prepareLoading(languages, namespaces) {
|
1570
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
1571
|
-
let callback = arguments.length > 3 ? arguments[3] : undefined;
|
1528
|
+
prepareLoading(languages, namespaces, options = {}, callback) {
|
1572
1529
|
if (!this.backend) {
|
1573
1530
|
this.logger.warn('No backend was added via i18next.use. Will not load resources.');
|
1574
1531
|
return callback && callback();
|
@@ -1592,8 +1549,7 @@ class Connector extends EventEmitter {
|
|
1592
1549
|
reload: true
|
1593
1550
|
}, callback);
|
1594
1551
|
}
|
1595
|
-
loadOne(name) {
|
1596
|
-
let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
1552
|
+
loadOne(name, prefix = '') {
|
1597
1553
|
const s = name.split('|');
|
1598
1554
|
const lng = s[0];
|
1599
1555
|
const ns = s[1];
|
@@ -1603,9 +1559,7 @@ class Connector extends EventEmitter {
|
|
1603
1559
|
this.loaded(name, err, data);
|
1604
1560
|
});
|
1605
1561
|
}
|
1606
|
-
saveMissing(languages, namespace, key, fallbackValue, isUpdate) {
|
1607
|
-
let options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
|
1608
|
-
let clb = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : () => {};
|
1562
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
|
1609
1563
|
if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
|
1610
1564
|
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!!!');
|
1611
1565
|
return;
|
@@ -1700,7 +1654,8 @@ const get = () => ({
|
|
1700
1654
|
nestingOptionsSeparator: ',',
|
1701
1655
|
maxReplaces: 1000,
|
1702
1656
|
skipOnVariables: true
|
1703
|
-
}
|
1657
|
+
},
|
1658
|
+
cacheInBuiltFormats: true
|
1704
1659
|
});
|
1705
1660
|
const transformOptions = options => {
|
1706
1661
|
if (isString(options.ns)) options.ns = [options.ns];
|
@@ -1723,9 +1678,7 @@ const bindMemberFunctions = inst => {
|
|
1723
1678
|
});
|
1724
1679
|
};
|
1725
1680
|
class I18n extends EventEmitter {
|
1726
|
-
constructor() {
|
1727
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
1728
|
-
let callback = arguments.length > 1 ? arguments[1] : undefined;
|
1681
|
+
constructor(options = {}, callback) {
|
1729
1682
|
super();
|
1730
1683
|
this.options = transformOptions(options);
|
1731
1684
|
this.services = {};
|
@@ -1744,10 +1697,7 @@ class I18n extends EventEmitter {
|
|
1744
1697
|
}, 0);
|
1745
1698
|
}
|
1746
1699
|
}
|
1747
|
-
init() {
|
1748
|
-
var _this = this;
|
1749
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
1750
|
-
let callback = arguments.length > 1 ? arguments[1] : undefined;
|
1700
|
+
init(options = {}, callback) {
|
1751
1701
|
this.isInitializing = true;
|
1752
1702
|
if (typeof options === 'function') {
|
1753
1703
|
callback = options;
|
@@ -1813,11 +1763,8 @@ class I18n extends EventEmitter {
|
|
1813
1763
|
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
1814
1764
|
};
|
1815
1765
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
1816
|
-
s.backendConnector.on('*',
|
1817
|
-
|
1818
|
-
args[_key - 1] = arguments[_key];
|
1819
|
-
}
|
1820
|
-
_this.emit(event, ...args);
|
1766
|
+
s.backendConnector.on('*', (event, ...args) => {
|
1767
|
+
this.emit(event, ...args);
|
1821
1768
|
});
|
1822
1769
|
if (this.modules.languageDetector) {
|
1823
1770
|
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
@@ -1828,11 +1775,8 @@ class I18n extends EventEmitter {
|
|
1828
1775
|
if (s.i18nFormat.init) s.i18nFormat.init(this);
|
1829
1776
|
}
|
1830
1777
|
this.translator = new Translator(this.services, this.options);
|
1831
|
-
this.translator.on('*',
|
1832
|
-
|
1833
|
-
args[_key2 - 1] = arguments[_key2];
|
1834
|
-
}
|
1835
|
-
_this.emit(event, ...args);
|
1778
|
+
this.translator.on('*', (event, ...args) => {
|
1779
|
+
this.emit(event, ...args);
|
1836
1780
|
});
|
1837
1781
|
this.modules.external.forEach(m => {
|
1838
1782
|
if (m.init) m.init(this);
|
@@ -1849,15 +1793,13 @@ class I18n extends EventEmitter {
|
|
1849
1793
|
}
|
1850
1794
|
const storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
|
1851
1795
|
storeApi.forEach(fcName => {
|
1852
|
-
this[fcName] =
|
1853
|
-
return _this.store[fcName](...arguments);
|
1854
|
-
};
|
1796
|
+
this[fcName] = (...args) => this.store[fcName](...args);
|
1855
1797
|
});
|
1856
1798
|
const storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
|
1857
1799
|
storeApiChained.forEach(fcName => {
|
1858
|
-
this[fcName] =
|
1859
|
-
|
1860
|
-
return
|
1800
|
+
this[fcName] = (...args) => {
|
1801
|
+
this.store[fcName](...args);
|
1802
|
+
return this;
|
1861
1803
|
};
|
1862
1804
|
});
|
1863
1805
|
const deferred = defer();
|
@@ -1881,8 +1823,7 @@ class I18n extends EventEmitter {
|
|
1881
1823
|
}
|
1882
1824
|
return deferred;
|
1883
1825
|
}
|
1884
|
-
loadResources(language) {
|
1885
|
-
let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
|
1826
|
+
loadResources(language, callback = noop) {
|
1886
1827
|
let usedCallback = callback;
|
1887
1828
|
const usedLng = isString(language) ? language : this.language;
|
1888
1829
|
if (typeof language === 'function') usedCallback = language;
|
@@ -1969,9 +1910,12 @@ class I18n extends EventEmitter {
|
|
1969
1910
|
break;
|
1970
1911
|
}
|
1971
1912
|
}
|
1913
|
+
if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
|
1914
|
+
this.resolvedLanguage = l;
|
1915
|
+
this.languages.unshift(l);
|
1916
|
+
}
|
1972
1917
|
}
|
1973
1918
|
changeLanguage(lng, callback) {
|
1974
|
-
var _this2 = this;
|
1975
1919
|
this.isLanguageChangingTo = lng;
|
1976
1920
|
const deferred = defer();
|
1977
1921
|
this.emit('languageChanging', lng);
|
@@ -1983,24 +1927,23 @@ class I18n extends EventEmitter {
|
|
1983
1927
|
};
|
1984
1928
|
const done = (err, l) => {
|
1985
1929
|
if (l) {
|
1986
|
-
|
1987
|
-
|
1988
|
-
|
1989
|
-
|
1990
|
-
|
1930
|
+
if (this.isLanguageChangingTo === lng) {
|
1931
|
+
setLngProps(l);
|
1932
|
+
this.translator.changeLanguage(l);
|
1933
|
+
this.isLanguageChangingTo = undefined;
|
1934
|
+
this.emit('languageChanged', l);
|
1935
|
+
this.logger.log('languageChanged', l);
|
1936
|
+
}
|
1991
1937
|
} else {
|
1992
1938
|
this.isLanguageChangingTo = undefined;
|
1993
1939
|
}
|
1994
|
-
deferred.resolve(
|
1995
|
-
|
1996
|
-
});
|
1997
|
-
if (callback) callback(err, function () {
|
1998
|
-
return _this2.t(...arguments);
|
1999
|
-
});
|
1940
|
+
deferred.resolve((...args) => this.t(...args));
|
1941
|
+
if (callback) callback(err, (...args) => this.t(...args));
|
2000
1942
|
};
|
2001
1943
|
const setLng = lngs => {
|
2002
1944
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
2003
|
-
const
|
1945
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
1946
|
+
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
2004
1947
|
if (l) {
|
2005
1948
|
if (!this.language) {
|
2006
1949
|
setLngProps(l);
|
@@ -2026,31 +1969,27 @@ class I18n extends EventEmitter {
|
|
2026
1969
|
return deferred;
|
2027
1970
|
}
|
2028
1971
|
getFixedT(lng, ns, keyPrefix) {
|
2029
|
-
|
2030
|
-
|
2031
|
-
let options;
|
1972
|
+
const fixedT = (key, opts, ...rest) => {
|
1973
|
+
let o;
|
2032
1974
|
if (typeof opts !== 'object') {
|
2033
|
-
|
2034
|
-
rest[_key3 - 2] = arguments[_key3];
|
2035
|
-
}
|
2036
|
-
options = _this3.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
1975
|
+
o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
2037
1976
|
} else {
|
2038
|
-
|
1977
|
+
o = {
|
2039
1978
|
...opts
|
2040
1979
|
};
|
2041
1980
|
}
|
2042
|
-
|
2043
|
-
|
2044
|
-
|
2045
|
-
if (
|
2046
|
-
const keySeparator =
|
1981
|
+
o.lng = o.lng || fixedT.lng;
|
1982
|
+
o.lngs = o.lngs || fixedT.lngs;
|
1983
|
+
o.ns = o.ns || fixedT.ns;
|
1984
|
+
if (o.keyPrefix !== '') o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
|
1985
|
+
const keySeparator = this.options.keySeparator || '.';
|
2047
1986
|
let resultKey;
|
2048
|
-
if (
|
2049
|
-
resultKey = key.map(k => `${
|
1987
|
+
if (o.keyPrefix && Array.isArray(key)) {
|
1988
|
+
resultKey = key.map(k => `${o.keyPrefix}${keySeparator}${k}`);
|
2050
1989
|
} else {
|
2051
|
-
resultKey =
|
1990
|
+
resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
|
2052
1991
|
}
|
2053
|
-
return
|
1992
|
+
return this.t(resultKey, o);
|
2054
1993
|
};
|
2055
1994
|
if (isString(lng)) {
|
2056
1995
|
fixedT.lng = lng;
|
@@ -2061,23 +2000,16 @@ class I18n extends EventEmitter {
|
|
2061
2000
|
fixedT.keyPrefix = keyPrefix;
|
2062
2001
|
return fixedT;
|
2063
2002
|
}
|
2064
|
-
t() {
|
2065
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
2066
|
-
args[_key4] = arguments[_key4];
|
2067
|
-
}
|
2003
|
+
t(...args) {
|
2068
2004
|
return this.translator?.translate(...args);
|
2069
2005
|
}
|
2070
|
-
exists() {
|
2071
|
-
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
2072
|
-
args[_key5] = arguments[_key5];
|
2073
|
-
}
|
2006
|
+
exists(...args) {
|
2074
2007
|
return this.translator?.exists(...args);
|
2075
2008
|
}
|
2076
2009
|
setDefaultNamespace(ns) {
|
2077
2010
|
this.options.defaultNS = ns;
|
2078
2011
|
}
|
2079
|
-
hasLoadedNamespace(ns) {
|
2080
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
2012
|
+
hasLoadedNamespace(ns, options = {}) {
|
2081
2013
|
if (!this.isInitialized) {
|
2082
2014
|
this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
|
2083
2015
|
return false;
|
@@ -2142,14 +2074,10 @@ class I18n extends EventEmitter {
|
|
2142
2074
|
const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
|
2143
2075
|
return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf('-arab') > 1 ? 'rtl' : 'ltr';
|
2144
2076
|
}
|
2145
|
-
static createInstance() {
|
2146
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
2147
|
-
let callback = arguments.length > 1 ? arguments[1] : undefined;
|
2077
|
+
static createInstance(options = {}, callback) {
|
2148
2078
|
return new I18n(options, callback);
|
2149
2079
|
}
|
2150
|
-
cloneInstance() {
|
2151
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
2152
|
-
let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
|
2080
|
+
cloneInstance(options = {}, callback = noop) {
|
2153
2081
|
const forkResourceStore = options.forkResourceStore;
|
2154
2082
|
if (forkResourceStore) delete options.forkResourceStore;
|
2155
2083
|
const mergedOptions = {
|
@@ -2178,21 +2106,19 @@ class I18n extends EventEmitter {
|
|
2178
2106
|
prev[l] = {
|
2179
2107
|
...this.store.data[l]
|
2180
2108
|
};
|
2181
|
-
|
2109
|
+
prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
|
2182
2110
|
acc[n] = {
|
2183
2111
|
...prev[l][n]
|
2184
2112
|
};
|
2185
2113
|
return acc;
|
2186
|
-
},
|
2114
|
+
}, prev[l]);
|
2115
|
+
return prev;
|
2187
2116
|
}, {});
|
2188
2117
|
clone.store = new ResourceStore(clonedData, mergedOptions);
|
2189
2118
|
clone.services.resourceStore = clone.store;
|
2190
2119
|
}
|
2191
2120
|
clone.translator = new Translator(clone.services, mergedOptions);
|
2192
|
-
clone.translator.on('*',
|
2193
|
-
for (var _len6 = arguments.length, args = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
|
2194
|
-
args[_key6 - 1] = arguments[_key6];
|
2195
|
-
}
|
2121
|
+
clone.translator.on('*', (event, ...args) => {
|
2196
2122
|
clone.emit(event, ...args);
|
2197
2123
|
});
|
2198
2124
|
clone.init(mergedOptions, callback);
|