i18next 26.7.2 → 27.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.
- package/README.md +2 -0
- package/dist/cjs/i18next.js +179 -206
- package/dist/esm/i18next.js +179 -206
- package/dist/esm/package.json +1 -1
- package/dist/umd/i18next.js +179 -206
- package/dist/umd/i18next.min.js +1 -1
- package/i18next.js +179 -206
- package/i18next.min.js +1 -1
- package/index.d.mts +5 -4
- package/index.d.ts +11 -7
- package/package.json +4 -2
- package/typescript/options.d.ts +26 -168
- package/typescript/t.d.ts +181 -77
- package/index.js +0 -5
package/dist/cjs/i18next.js
CHANGED
|
@@ -114,12 +114,11 @@ const _entityMap = {
|
|
|
114
114
|
'<': '<',
|
|
115
115
|
'>': '>',
|
|
116
116
|
'"': '"',
|
|
117
|
-
"'": '''
|
|
118
|
-
'/': '/'
|
|
117
|
+
"'": '''
|
|
119
118
|
};
|
|
120
119
|
const escape = data => {
|
|
121
120
|
if (isString(data)) {
|
|
122
|
-
return data.replace(/[&<>"'
|
|
121
|
+
return data.replace(/[&<>"']/g, s => _entityMap[s]);
|
|
123
122
|
}
|
|
124
123
|
return data;
|
|
125
124
|
};
|
|
@@ -166,6 +165,9 @@ const deepFind = (obj, path, keySeparator = '.') => {
|
|
|
166
165
|
if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
|
|
167
166
|
return obj[path];
|
|
168
167
|
}
|
|
168
|
+
if (!keySeparator) {
|
|
169
|
+
return Object.prototype.hasOwnProperty.call(obj, path) ? obj[path] : undefined;
|
|
170
|
+
}
|
|
169
171
|
const tokens = path.split(keySeparator);
|
|
170
172
|
let current = obj;
|
|
171
173
|
for (let i = 0; i < tokens.length;) {
|
|
@@ -313,15 +315,20 @@ class EventEmitter {
|
|
|
313
315
|
}
|
|
314
316
|
|
|
315
317
|
const nonEmptyBundles = new WeakSet();
|
|
318
|
+
const assertSeparateArguments = (method, ...args) => {
|
|
319
|
+
if (args.some(a => !isString(a))) {
|
|
320
|
+
throw new TypeError(`${method}: pass language, namespace${method === 'addResource' ? ' and key' : ''} as separate strings; the dotted form ('en.translation.key') was removed in i18next v27`);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
316
323
|
class ResourceStore extends EventEmitter {
|
|
317
324
|
constructor(data, options = {
|
|
318
325
|
ns: ['translation'],
|
|
319
326
|
defaultNS: 'translation'
|
|
320
|
-
}) {
|
|
327
|
+
}, logger = baseLogger) {
|
|
321
328
|
super();
|
|
322
329
|
this.data = data || {};
|
|
323
330
|
this.options = options;
|
|
324
|
-
this.logger =
|
|
331
|
+
this.logger = logger.create('resourceStore');
|
|
325
332
|
if (this.options.keySeparator === undefined) {
|
|
326
333
|
this.options.keySeparator = '.';
|
|
327
334
|
}
|
|
@@ -340,49 +347,30 @@ class ResourceStore extends EventEmitter {
|
|
|
340
347
|
this.options.ns.splice(index, 1);
|
|
341
348
|
}
|
|
342
349
|
}
|
|
343
|
-
deprecateDottedLng(method) {
|
|
344
|
-
this.logger.once(method, 'deprecate', `${method}: a dotted language ('en.translation.key') that carries the namespace and key is removed in v27; pass language, namespace and key as separate arguments`);
|
|
345
|
-
}
|
|
346
350
|
getResource(lng, ns, key, options = {}) {
|
|
347
351
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
348
352
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
349
|
-
|
|
350
|
-
if (
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
path.push(...key);
|
|
358
|
-
} else if (isString(key) && keySeparator) {
|
|
359
|
-
path.push(...key.split(keySeparator));
|
|
360
|
-
} else {
|
|
361
|
-
path.push(key);
|
|
362
|
-
}
|
|
353
|
+
const path = [lng, ns];
|
|
354
|
+
if (key) {
|
|
355
|
+
if (Array.isArray(key)) {
|
|
356
|
+
path.push(...key);
|
|
357
|
+
} else if (isString(key) && keySeparator) {
|
|
358
|
+
path.push(...key.split(keySeparator));
|
|
359
|
+
} else {
|
|
360
|
+
path.push(key);
|
|
363
361
|
}
|
|
364
362
|
}
|
|
365
363
|
const result = getPath(this.data, path);
|
|
366
|
-
if (result == null && !ns && !key && lng.includes('.')) {
|
|
367
|
-
lng = path[0];
|
|
368
|
-
ns = path[1];
|
|
369
|
-
key = path.slice(2).join('.');
|
|
370
|
-
}
|
|
371
364
|
if (result != null || !ignoreJSONStructure || !isString(key)) return result;
|
|
372
365
|
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
|
373
366
|
}
|
|
374
367
|
addResource(lng, ns, key, value, options = {
|
|
375
368
|
silent: false
|
|
376
369
|
}) {
|
|
370
|
+
assertSeparateArguments('addResource', lng, ns, key);
|
|
377
371
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
378
372
|
let path = [lng, ns];
|
|
379
373
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
380
|
-
if (lng.includes('.')) {
|
|
381
|
-
this.deprecateDottedLng('addResource');
|
|
382
|
-
path = lng.split('.');
|
|
383
|
-
value = ns;
|
|
384
|
-
ns = path[1];
|
|
385
|
-
}
|
|
386
374
|
this.addNamespaces(ns);
|
|
387
375
|
setPath(this.data, path, value);
|
|
388
376
|
if (!options.silent) this.emit('added', lng, ns, key, value);
|
|
@@ -390,25 +378,21 @@ class ResourceStore extends EventEmitter {
|
|
|
390
378
|
addResources(lng, ns, resources, options = {
|
|
391
379
|
silent: false
|
|
392
380
|
}) {
|
|
393
|
-
|
|
394
|
-
|
|
381
|
+
assertSeparateArguments('addResources', lng, ns);
|
|
382
|
+
Object.keys(resources || {}).forEach(m => {
|
|
383
|
+
if (m === '__proto__' || m === 'constructor') return;
|
|
384
|
+
this.addResource(lng, ns, m, resources[m], {
|
|
395
385
|
silent: true
|
|
396
386
|
});
|
|
397
|
-
}
|
|
387
|
+
});
|
|
398
388
|
if (!options.silent) this.emit('added', lng, ns, resources);
|
|
399
389
|
}
|
|
400
390
|
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
|
401
391
|
silent: false,
|
|
402
392
|
skipCopy: false
|
|
403
393
|
}) {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
this.deprecateDottedLng('addResourceBundle');
|
|
407
|
-
path = lng.split('.');
|
|
408
|
-
deep = resources;
|
|
409
|
-
resources = ns;
|
|
410
|
-
ns = path[1];
|
|
411
|
-
}
|
|
394
|
+
assertSeparateArguments('addResourceBundle', lng, ns);
|
|
395
|
+
const path = [lng, ns];
|
|
412
396
|
this.addNamespaces(ns);
|
|
413
397
|
let pack = getPath(this.data, path) || {};
|
|
414
398
|
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
|
|
@@ -456,19 +440,6 @@ class ResourceStore extends EventEmitter {
|
|
|
456
440
|
}
|
|
457
441
|
}
|
|
458
442
|
|
|
459
|
-
var postProcessor = {
|
|
460
|
-
processors: {},
|
|
461
|
-
addPostProcessor(module) {
|
|
462
|
-
this.processors[module.name] = module;
|
|
463
|
-
},
|
|
464
|
-
handle(processors, value, key, options, translator) {
|
|
465
|
-
processors.forEach(processor => {
|
|
466
|
-
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
|
467
|
-
});
|
|
468
|
-
return value;
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
|
-
|
|
472
443
|
const PATH_KEY = Symbol('i18next/PATH_KEY');
|
|
473
444
|
function createProxy() {
|
|
474
445
|
const state = [];
|
|
@@ -487,7 +458,7 @@ function keysFromSelector(selector, opts) {
|
|
|
487
458
|
const {
|
|
488
459
|
[PATH_KEY]: path
|
|
489
460
|
} = selector(createProxy());
|
|
490
|
-
const keySeparator = opts?.keySeparator
|
|
461
|
+
const keySeparator = opts?.keySeparator || '.';
|
|
491
462
|
const nsSeparator = opts?.nsSeparator ?? ':';
|
|
492
463
|
const strict = opts?.enableSelector === 'strict';
|
|
493
464
|
if (path.length > 1 && nsSeparator) {
|
|
@@ -507,12 +478,12 @@ const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' &
|
|
|
507
478
|
class Translator extends EventEmitter {
|
|
508
479
|
constructor(services, options = {}) {
|
|
509
480
|
super();
|
|
510
|
-
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', '
|
|
481
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', 'postProcessors'], services, this);
|
|
511
482
|
this.options = options;
|
|
512
483
|
if (this.options.keySeparator === undefined) {
|
|
513
484
|
this.options.keySeparator = '.';
|
|
514
485
|
}
|
|
515
|
-
this.logger = baseLogger.create('translator');
|
|
486
|
+
this.logger = (services.logger || baseLogger).create('translator');
|
|
516
487
|
this.checkedLoadedFor = {};
|
|
517
488
|
}
|
|
518
489
|
changeLanguage(lng) {
|
|
@@ -636,23 +607,24 @@ class Translator extends EventEmitter {
|
|
|
636
607
|
}
|
|
637
608
|
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
|
638
609
|
const resType = Object.prototype.toString.apply(resForObjHndl);
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
resolved.res = r;
|
|
651
|
-
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
652
|
-
return resolved;
|
|
653
|
-
}
|
|
654
|
-
return r;
|
|
610
|
+
const isObjectHit = handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && !noObject.includes(resType) && !(isString(joinArrays) && Array.isArray(resForObjHndl));
|
|
611
|
+
const objectNotReturned = isObjectHit && !opt.returnObjects && !this.options.returnObjects;
|
|
612
|
+
if (objectNotReturned && this.options.returnedObjectHandler) {
|
|
613
|
+
const r = this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
614
|
+
...opt,
|
|
615
|
+
ns: namespaces
|
|
616
|
+
});
|
|
617
|
+
if (returnDetails) {
|
|
618
|
+
resolved.res = r;
|
|
619
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
620
|
+
return resolved;
|
|
655
621
|
}
|
|
622
|
+
return r;
|
|
623
|
+
}
|
|
624
|
+
if (objectNotReturned) {
|
|
625
|
+
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
|
626
|
+
}
|
|
627
|
+
if (isObjectHit && !objectNotReturned) {
|
|
656
628
|
if (keySeparator) {
|
|
657
629
|
const resTypeIsArray = Array.isArray(resForObjHndl);
|
|
658
630
|
const copy = resTypeIsArray ? [] : {};
|
|
@@ -689,18 +661,19 @@ class Translator extends EventEmitter {
|
|
|
689
661
|
} else {
|
|
690
662
|
let usedDefault = false;
|
|
691
663
|
let usedKey = false;
|
|
692
|
-
if (
|
|
664
|
+
if (objectNotReturned) res = undefined;
|
|
665
|
+
if (!this.isValidLookup(res) && hasDefaultValue && !(objectNotReturned && shouldHandleAsObject(defaultValue))) {
|
|
693
666
|
usedDefault = true;
|
|
694
667
|
res = defaultValue;
|
|
695
668
|
}
|
|
696
|
-
if (!this.isValidLookup(res)) {
|
|
669
|
+
if (!this.isValidLookup(res) && !(usedDefault && res === '')) {
|
|
697
670
|
usedKey = true;
|
|
698
671
|
res = key;
|
|
699
672
|
}
|
|
700
673
|
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
|
701
674
|
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
|
|
702
675
|
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
|
703
|
-
if (usedKey || usedDefault || updateMissing) {
|
|
676
|
+
if (!objectNotReturned && (usedKey || usedDefault || updateMissing)) {
|
|
704
677
|
this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, needsPluralHandling && !updateMissing ? `${key}${this.pluralResolver.getSuffix(lng, opt.count, opt)}` : key, updateMissing ? defaultValue : res);
|
|
705
678
|
if (keySeparator) {
|
|
706
679
|
const fk = this.resolve(key, {
|
|
@@ -808,18 +781,21 @@ class Translator extends EventEmitter {
|
|
|
808
781
|
const postProcess = opt.postProcess || this.options.postProcess;
|
|
809
782
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
810
783
|
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
811
|
-
|
|
812
|
-
if (this.logger.debug && postProcessor.processors[name] && this.ownPostProcessors && !this.ownPostProcessors.has(name)) {
|
|
813
|
-
this.logger.once(name, 'deprecate', `postProcess: '${name}' was registered with use() on another i18next instance; from v27 post-processors are per instance, so call use() with it on this instance too`);
|
|
814
|
-
}
|
|
815
|
-
});
|
|
816
|
-
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
784
|
+
const ppOptions = this.options?.postProcessPassResolved ? {
|
|
817
785
|
i18nResolved: {
|
|
818
786
|
...resolved,
|
|
819
787
|
usedParams: this.getUsedParamsDetails(opt)
|
|
820
788
|
},
|
|
821
789
|
...opt
|
|
822
|
-
} : opt
|
|
790
|
+
} : opt;
|
|
791
|
+
postProcessorNames.forEach(name => {
|
|
792
|
+
const processor = this.postProcessors?.[name];
|
|
793
|
+
if (!processor) {
|
|
794
|
+
this.logger.once(`postProcess:${name}`, 'warn', `postProcess: '${name}' is not registered on this i18next instance; call use() with it on this instance`);
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
res = processor.process(res, key, ppOptions, this) ?? res;
|
|
798
|
+
});
|
|
823
799
|
}
|
|
824
800
|
return res;
|
|
825
801
|
}
|
|
@@ -949,14 +925,16 @@ class Translator extends EventEmitter {
|
|
|
949
925
|
}
|
|
950
926
|
|
|
951
927
|
class LanguageUtil {
|
|
952
|
-
constructor(options) {
|
|
928
|
+
constructor(options, logger = baseLogger) {
|
|
953
929
|
this.options = options;
|
|
954
930
|
this.supportedLngs = this.options.supportedLngs || false;
|
|
955
|
-
this.logger =
|
|
931
|
+
this.logger = logger.create('languageUtils');
|
|
956
932
|
this.resolveHierarchyCache = {};
|
|
933
|
+
this.formattedCodes = new Map();
|
|
957
934
|
}
|
|
958
935
|
clearCache() {
|
|
959
936
|
this.resolveHierarchyCache = {};
|
|
937
|
+
this.formattedCodes.clear();
|
|
960
938
|
}
|
|
961
939
|
getScriptPartFromCode(code) {
|
|
962
940
|
code = getCleanedCode(code);
|
|
@@ -974,35 +952,27 @@ class LanguageUtil {
|
|
|
974
952
|
return this.formatLanguageCode(p[0]);
|
|
975
953
|
}
|
|
976
954
|
formatLanguageCode(code) {
|
|
977
|
-
if (isString(code)
|
|
978
|
-
|
|
955
|
+
if (!isString(code)) return code;
|
|
956
|
+
let formatted = this.formattedCodes.get(code);
|
|
957
|
+
if (formatted !== undefined) return formatted;
|
|
958
|
+
formatted = this.formatUncached(code);
|
|
959
|
+
if (this.formattedCodes.size >= 1000) this.formattedCodes.clear();
|
|
960
|
+
this.formattedCodes.set(code, formatted);
|
|
961
|
+
return formatted;
|
|
962
|
+
}
|
|
963
|
+
formatUncached(code) {
|
|
964
|
+
const hasSubtags = code.includes('-');
|
|
965
|
+
if (hasSubtags || this.options.cleanCode !== false) {
|
|
966
|
+
let canonical;
|
|
979
967
|
try {
|
|
980
|
-
|
|
968
|
+
canonical = Intl.getCanonicalLocales(code)[0];
|
|
981
969
|
} catch (e) {}
|
|
982
|
-
if (
|
|
983
|
-
|
|
984
|
-
}
|
|
985
|
-
if (formattedCode) return formattedCode;
|
|
986
|
-
if (this.options.lowerCaseLng) {
|
|
987
|
-
return code.toLowerCase();
|
|
970
|
+
if (canonical && (hasSubtags || canonical.toLowerCase() === code.toLowerCase())) {
|
|
971
|
+
return this.options.lowerCaseLng ? canonical.toLowerCase() : canonical;
|
|
988
972
|
}
|
|
989
|
-
return code;
|
|
973
|
+
if (canonical) return code.toLowerCase();
|
|
990
974
|
}
|
|
991
|
-
|
|
992
|
-
if (this.logger.debug && isString(code)) this.deprecateBareCode(code, formatted);
|
|
993
|
-
return formatted;
|
|
994
|
-
}
|
|
995
|
-
deprecateBareCode(code, formatted) {
|
|
996
|
-
let canonical;
|
|
997
|
-
try {
|
|
998
|
-
canonical = Intl.getCanonicalLocales(code)[0];
|
|
999
|
-
} catch (e) {
|
|
1000
|
-
return;
|
|
1001
|
-
}
|
|
1002
|
-
let v27 = canonical.toLowerCase() === code.toLowerCase() ? canonical : code.toLowerCase();
|
|
1003
|
-
if (this.options.lowerCaseLng) v27 = v27.toLowerCase();
|
|
1004
|
-
if (v27 === formatted) return;
|
|
1005
|
-
this.logger.once(code, 'deprecate', `language code '${code}' is used as '${formatted}'; i18next v27 gives it the canonical casing '${v27}', use '${v27}' in lng, fallbackLng, supportedLngs and the resources`);
|
|
975
|
+
return this.options.lowerCaseLng || this.options.cleanCode && !hasSubtags ? code.toLowerCase() : code;
|
|
1006
976
|
}
|
|
1007
977
|
isSupportedCode(code) {
|
|
1008
978
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1113,10 +1083,10 @@ const dummyRule = {
|
|
|
1113
1083
|
})
|
|
1114
1084
|
};
|
|
1115
1085
|
class PluralResolver {
|
|
1116
|
-
constructor(languageUtils, options = {}) {
|
|
1086
|
+
constructor(languageUtils, options = {}, logger = baseLogger) {
|
|
1117
1087
|
this.languageUtils = languageUtils;
|
|
1118
1088
|
this.options = options;
|
|
1119
|
-
this.logger =
|
|
1089
|
+
this.logger = logger.create('pluralResolver');
|
|
1120
1090
|
this.pluralRulesCache = {};
|
|
1121
1091
|
}
|
|
1122
1092
|
clearCache() {
|
|
@@ -1182,8 +1152,8 @@ const deepFindWithDefaults = (data, defaultData, key, keySeparator = '.', ignore
|
|
|
1182
1152
|
return path;
|
|
1183
1153
|
};
|
|
1184
1154
|
class Interpolator {
|
|
1185
|
-
constructor(options = {}) {
|
|
1186
|
-
this.logger =
|
|
1155
|
+
constructor(options = {}, logger = baseLogger) {
|
|
1156
|
+
this.logger = logger.create('interpolator');
|
|
1187
1157
|
this.options = options;
|
|
1188
1158
|
this.format = options?.interpolation?.format || (value => value);
|
|
1189
1159
|
this.init(options);
|
|
@@ -1197,39 +1167,26 @@ class Interpolator {
|
|
|
1197
1167
|
escapeValue,
|
|
1198
1168
|
useRawValueToEscape,
|
|
1199
1169
|
prefix,
|
|
1200
|
-
prefixEscaped,
|
|
1201
1170
|
suffix,
|
|
1202
|
-
suffixEscaped,
|
|
1203
1171
|
formatSeparator,
|
|
1204
1172
|
unescapeSuffix,
|
|
1205
1173
|
unescapePrefix,
|
|
1206
1174
|
nestingPrefix,
|
|
1207
|
-
nestingPrefixEscaped,
|
|
1208
1175
|
nestingSuffix,
|
|
1209
|
-
nestingSuffixEscaped,
|
|
1210
1176
|
nestingOptionsSeparator,
|
|
1211
1177
|
maxReplaces,
|
|
1212
1178
|
alwaysFormat
|
|
1213
1179
|
} = options.interpolation;
|
|
1214
|
-
if (prefixEscaped || suffixEscaped || nestingPrefixEscaped || nestingSuffixEscaped) {
|
|
1215
|
-
const names = Object.entries({
|
|
1216
|
-
prefixEscaped,
|
|
1217
|
-
suffixEscaped,
|
|
1218
|
-
nestingPrefixEscaped,
|
|
1219
|
-
nestingSuffixEscaped
|
|
1220
|
-
}).filter(([, v]) => v).map(([k]) => k);
|
|
1221
|
-
this.logger.once(names.join(), 'deprecate', `interpolation.${names.join(', interpolation.')}: the regex-escaped delimiter options are removed in v27; pass the literal delimiters as prefix, suffix, nestingPrefix and nestingSuffix`);
|
|
1222
|
-
}
|
|
1223
1180
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1224
1181
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1225
1182
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
1226
|
-
this.prefix = prefix ? regexEscape(prefix) :
|
|
1227
|
-
this.suffix = suffix ? regexEscape(suffix) :
|
|
1183
|
+
this.prefix = prefix ? regexEscape(prefix) : '{{';
|
|
1184
|
+
this.suffix = suffix ? regexEscape(suffix) : '}}';
|
|
1228
1185
|
this.formatSeparator = formatSeparator || ',';
|
|
1229
1186
|
this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix ? regexEscape(unescapePrefix) : '-';
|
|
1230
1187
|
this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix ? regexEscape(unescapeSuffix) : '';
|
|
1231
|
-
this.nestingPrefix =
|
|
1232
|
-
this.nestingSuffix =
|
|
1188
|
+
this.nestingPrefix = regexEscape(nestingPrefix || '$t(');
|
|
1189
|
+
this.nestingSuffix = regexEscape(nestingSuffix || ')');
|
|
1233
1190
|
this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
|
|
1234
1191
|
this.maxReplaces = maxReplaces || 1000;
|
|
1235
1192
|
this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
|
|
@@ -1285,13 +1242,7 @@ class Interpolator {
|
|
|
1285
1242
|
safeValue: val => val
|
|
1286
1243
|
}, {
|
|
1287
1244
|
regex: this.regexp,
|
|
1288
|
-
safeValue: val =>
|
|
1289
|
-
if (!this.escapeValue) return val;
|
|
1290
|
-
if (this.escape === escape && this.options?.interpolation?.escapeValue !== false && isString(val) && val.includes('/')) {
|
|
1291
|
-
this.logger.once('/', 'deprecate', "escapeValue renders '/' in a value as '/' today; from v27 the default escape leaves '/' as it is");
|
|
1292
|
-
}
|
|
1293
|
-
return this.escape(val);
|
|
1294
|
-
}
|
|
1245
|
+
safeValue: val => this.escapeValue ? this.escape(val) : val
|
|
1295
1246
|
}];
|
|
1296
1247
|
const unescapedValues = [];
|
|
1297
1248
|
let shift = 0;
|
|
@@ -1443,8 +1394,9 @@ const parseFormatStr = formatStr => {
|
|
|
1443
1394
|
formatOptions
|
|
1444
1395
|
};
|
|
1445
1396
|
};
|
|
1397
|
+
const cachedFormattersLimit = 1000;
|
|
1446
1398
|
const createCachedFormatter = fn => {
|
|
1447
|
-
const cache =
|
|
1399
|
+
const cache = new Map();
|
|
1448
1400
|
return (v, l, o) => {
|
|
1449
1401
|
let optForCache = o;
|
|
1450
1402
|
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
|
@@ -1459,10 +1411,11 @@ const createCachedFormatter = fn => {
|
|
|
1459
1411
|
} catch (e) {
|
|
1460
1412
|
return fn(getCleanedCode(l), o)(v);
|
|
1461
1413
|
}
|
|
1462
|
-
let frm = cache
|
|
1414
|
+
let frm = cache.get(key);
|
|
1463
1415
|
if (!frm) {
|
|
1464
1416
|
frm = fn(getCleanedCode(l), o);
|
|
1465
|
-
cache
|
|
1417
|
+
if (cache.size >= cachedFormattersLimit) cache.delete(cache.keys().next().value);
|
|
1418
|
+
cache.set(key, frm);
|
|
1466
1419
|
}
|
|
1467
1420
|
return frm(v);
|
|
1468
1421
|
};
|
|
@@ -1477,6 +1430,7 @@ class Formatter {
|
|
|
1477
1430
|
init(services, options = {
|
|
1478
1431
|
interpolation: {}
|
|
1479
1432
|
}) {
|
|
1433
|
+
if (services?.logger?.create) this.logger = services.logger.create('formatter');
|
|
1480
1434
|
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
|
1481
1435
|
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1482
1436
|
this.formats = {
|
|
@@ -1573,7 +1527,7 @@ class Connector extends EventEmitter {
|
|
|
1573
1527
|
this.services = services;
|
|
1574
1528
|
this.languageUtils = services.languageUtils;
|
|
1575
1529
|
this.options = options;
|
|
1576
|
-
this.logger = baseLogger.create('backendConnector');
|
|
1530
|
+
this.logger = (services.logger || baseLogger).create('backendConnector');
|
|
1577
1531
|
this.waitingReads = [];
|
|
1578
1532
|
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1579
1533
|
this.readingCalls = 0;
|
|
@@ -1732,15 +1686,10 @@ class Connector extends EventEmitter {
|
|
|
1732
1686
|
reload: true
|
|
1733
1687
|
}, callback);
|
|
1734
1688
|
}
|
|
1735
|
-
noticeDevFallback(lng) {
|
|
1736
|
-
if (lng !== 'dev' || !this.devIsDefaultFallback) return;
|
|
1737
|
-
this.logger.once('dev', 'warn', "language 'dev' is requested because fallbackLng is not set (its default is ['dev']); i18next v27 defaults to fallbackLng: false and loads and saves missing keys in the requested language instead. Set fallbackLng to your source language, or to 'dev' to keep this behavior");
|
|
1738
|
-
}
|
|
1739
1689
|
loadOne(name, prefix = '') {
|
|
1740
1690
|
const s = name.split('|');
|
|
1741
1691
|
const lng = s[0];
|
|
1742
1692
|
const ns = s[1];
|
|
1743
|
-
this.noticeDevFallback(lng);
|
|
1744
1693
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1745
1694
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1746
1695
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
@@ -1754,7 +1703,6 @@ class Connector extends EventEmitter {
|
|
|
1754
1703
|
}
|
|
1755
1704
|
if (key === undefined || key === null || key === '') return;
|
|
1756
1705
|
if (this.backend?.create) {
|
|
1757
|
-
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1758
1706
|
const opts = {
|
|
1759
1707
|
...options,
|
|
1760
1708
|
isUpdate
|
|
@@ -1790,7 +1738,7 @@ const get = () => ({
|
|
|
1790
1738
|
initAsync: true,
|
|
1791
1739
|
ns: ['translation'],
|
|
1792
1740
|
defaultNS: ['translation'],
|
|
1793
|
-
fallbackLng:
|
|
1741
|
+
fallbackLng: false,
|
|
1794
1742
|
fallbackNS: false,
|
|
1795
1743
|
supportedLngs: false,
|
|
1796
1744
|
nonExplicitSupportedLngs: false,
|
|
@@ -1811,7 +1759,7 @@ const get = () => ({
|
|
|
1811
1759
|
postProcess: false,
|
|
1812
1760
|
postProcessPassResolved: false,
|
|
1813
1761
|
returnNull: false,
|
|
1814
|
-
returnEmptyString:
|
|
1762
|
+
returnEmptyString: false,
|
|
1815
1763
|
returnObjects: false,
|
|
1816
1764
|
joinArrays: false,
|
|
1817
1765
|
returnedObjectHandler: false,
|
|
@@ -1856,7 +1804,38 @@ const transformOptions = options => {
|
|
|
1856
1804
|
};
|
|
1857
1805
|
|
|
1858
1806
|
const noop = () => {};
|
|
1859
|
-
|
|
1807
|
+
const withDefaults = options => {
|
|
1808
|
+
const defOpts = get();
|
|
1809
|
+
const merged = {
|
|
1810
|
+
...defOpts,
|
|
1811
|
+
...options
|
|
1812
|
+
};
|
|
1813
|
+
merged.interpolation = {
|
|
1814
|
+
...defOpts.interpolation,
|
|
1815
|
+
...options.interpolation
|
|
1816
|
+
};
|
|
1817
|
+
if (typeof merged.overloadTranslationOptionHandler !== 'function') {
|
|
1818
|
+
merged.overloadTranslationOptionHandler = defOpts.overloadTranslationOptionHandler;
|
|
1819
|
+
}
|
|
1820
|
+
return merged;
|
|
1821
|
+
};
|
|
1822
|
+
const configuredLanguageCodes = options => {
|
|
1823
|
+
const codes = [].concat(options.supportedLngs || [], options.preload || []);
|
|
1824
|
+
codes.push(...Object.keys(options.resources || {}));
|
|
1825
|
+
const fallbackLng = options.fallbackLng;
|
|
1826
|
+
if (isString(fallbackLng) || Array.isArray(fallbackLng)) codes.push(...[].concat(fallbackLng));else if (fallbackLng && typeof fallbackLng === 'object') {
|
|
1827
|
+
Object.keys(fallbackLng).forEach(code => {
|
|
1828
|
+
if (code !== 'default') codes.push(code);
|
|
1829
|
+
codes.push(...[].concat(fallbackLng[code]));
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
return codes;
|
|
1833
|
+
};
|
|
1834
|
+
const isRefusedLanguageCode = (i18n, code) => {
|
|
1835
|
+
if (!isString(code) || isSafeLanguageCode(code) || i18n.configuredLngs?.has(code)) return false;
|
|
1836
|
+
i18n.logger.once(`lng:${code}`, 'warn', `refused the language code '${code.slice(0, 64)}' from lng, changeLanguage(), the language detector or loadLanguages(): it is not configured and has other characters than letters, digits, '-', '_', '@' and '.' (or '..', or more than 128). List it in supportedLngs to use it`);
|
|
1837
|
+
return true;
|
|
1838
|
+
};
|
|
1860
1839
|
const bindMemberFunctions = inst => {
|
|
1861
1840
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1862
1841
|
mems.forEach(mem => {
|
|
@@ -1868,17 +1847,18 @@ const bindMemberFunctions = inst => {
|
|
|
1868
1847
|
class I18n extends EventEmitter {
|
|
1869
1848
|
constructor(options = {}, callback) {
|
|
1870
1849
|
super();
|
|
1871
|
-
this.
|
|
1850
|
+
this.userOptions = transformOptions(options);
|
|
1851
|
+
this.options = withDefaults(this.userOptions);
|
|
1872
1852
|
this.services = {
|
|
1873
|
-
|
|
1853
|
+
postProcessors: Object.create(null)
|
|
1874
1854
|
};
|
|
1875
|
-
this.logger =
|
|
1855
|
+
this.logger = new Logger(null, this.options);
|
|
1876
1856
|
this.modules = {
|
|
1877
1857
|
external: []
|
|
1878
1858
|
};
|
|
1879
1859
|
bindMemberFunctions(this);
|
|
1880
1860
|
if (callback && !this.isInitialized && !options.isClone) {
|
|
1881
|
-
if (!this.
|
|
1861
|
+
if (!this.userOptions.initAsync) {
|
|
1882
1862
|
this.init(options, callback);
|
|
1883
1863
|
return this;
|
|
1884
1864
|
}
|
|
@@ -1900,25 +1880,23 @@ class I18n extends EventEmitter {
|
|
|
1900
1880
|
options.defaultNS = options.ns[0];
|
|
1901
1881
|
}
|
|
1902
1882
|
}
|
|
1903
|
-
|
|
1904
|
-
const
|
|
1905
|
-
|
|
1906
|
-
...
|
|
1907
|
-
...this.options,
|
|
1908
|
-
...transformOptions(options)
|
|
1909
|
-
};
|
|
1910
|
-
this.options.interpolation = {
|
|
1911
|
-
...defOpts.interpolation,
|
|
1912
|
-
...this.options.interpolation
|
|
1883
|
+
transformOptions(options);
|
|
1884
|
+
const userOptions = {
|
|
1885
|
+
...this.userOptions,
|
|
1886
|
+
...options
|
|
1913
1887
|
};
|
|
1914
|
-
|
|
1915
|
-
this.options
|
|
1888
|
+
this.options = withDefaults({
|
|
1889
|
+
...this.options,
|
|
1890
|
+
...options
|
|
1891
|
+
});
|
|
1892
|
+
if (userOptions.keySeparator !== undefined) {
|
|
1893
|
+
this.options.userDefinedKeySeparator = userOptions.keySeparator;
|
|
1916
1894
|
}
|
|
1917
|
-
if (
|
|
1918
|
-
this.options.userDefinedNsSeparator =
|
|
1895
|
+
if (userOptions.nsSeparator !== undefined) {
|
|
1896
|
+
this.options.userDefinedNsSeparator = userOptions.nsSeparator;
|
|
1919
1897
|
}
|
|
1920
|
-
if (
|
|
1921
|
-
this.
|
|
1898
|
+
if (!this.options.isClone) {
|
|
1899
|
+
this.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(userOptions)]);
|
|
1922
1900
|
}
|
|
1923
1901
|
const createClassOnDemand = ClassOrObject => {
|
|
1924
1902
|
if (!ClassOrObject) return null;
|
|
@@ -1926,15 +1904,10 @@ class I18n extends EventEmitter {
|
|
|
1926
1904
|
return ClassOrObject;
|
|
1927
1905
|
};
|
|
1928
1906
|
if (!this.options.isClone) {
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
if (this.modules.logger) {
|
|
1934
|
-
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1935
|
-
} else {
|
|
1936
|
-
baseLogger.init(null, this.options);
|
|
1937
|
-
}
|
|
1907
|
+
this.logger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1908
|
+
const {
|
|
1909
|
+
logger
|
|
1910
|
+
} = this;
|
|
1938
1911
|
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1939
1912
|
this.pendingWarnings = undefined;
|
|
1940
1913
|
let formatter;
|
|
@@ -1943,27 +1916,26 @@ class I18n extends EventEmitter {
|
|
|
1943
1916
|
} else {
|
|
1944
1917
|
formatter = Formatter;
|
|
1945
1918
|
}
|
|
1946
|
-
const lu = new LanguageUtil(this.options);
|
|
1947
|
-
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1919
|
+
const lu = new LanguageUtil(this.options, logger);
|
|
1920
|
+
this.store = new ResourceStore(this.options.resources, this.options, logger);
|
|
1948
1921
|
const s = this.services;
|
|
1949
|
-
s.logger =
|
|
1922
|
+
s.logger = logger;
|
|
1950
1923
|
s.resourceStore = this.store;
|
|
1951
1924
|
s.languageUtils = lu;
|
|
1952
1925
|
s.pluralResolver = new PluralResolver(lu, {
|
|
1953
1926
|
prepend: this.options.pluralSeparator
|
|
1954
|
-
});
|
|
1927
|
+
}, logger);
|
|
1955
1928
|
if (formatter) {
|
|
1956
1929
|
s.formatter = createClassOnDemand(formatter);
|
|
1957
1930
|
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1958
1931
|
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1959
1932
|
}
|
|
1960
|
-
s.interpolator = new Interpolator(this.options);
|
|
1933
|
+
s.interpolator = new Interpolator(this.options, logger);
|
|
1961
1934
|
s.utils = {
|
|
1962
1935
|
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1963
1936
|
reloadResources: this.reloadResources.bind(this)
|
|
1964
1937
|
};
|
|
1965
1938
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1966
|
-
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1967
1939
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1968
1940
|
this.emit(event, ...args);
|
|
1969
1941
|
});
|
|
@@ -2103,8 +2075,7 @@ class I18n extends EventEmitter {
|
|
|
2103
2075
|
setModule('i18nFormat');
|
|
2104
2076
|
}
|
|
2105
2077
|
if (module.type === 'postProcessor') {
|
|
2106
|
-
|
|
2107
|
-
this.services.ownPostProcessors?.add(module.name);
|
|
2078
|
+
this.services.postProcessors[module.name] = module;
|
|
2108
2079
|
}
|
|
2109
2080
|
if (module.type === 'formatter') {
|
|
2110
2081
|
setModule('formatter');
|
|
@@ -2179,14 +2150,16 @@ class I18n extends EventEmitter {
|
|
|
2179
2150
|
};
|
|
2180
2151
|
const setLng = lngs => {
|
|
2181
2152
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2182
|
-
const
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
}
|
|
2153
|
+
const {
|
|
2154
|
+
supportedLngs
|
|
2155
|
+
} = this.options;
|
|
2156
|
+
if (lngs && !supportedLngs) {
|
|
2157
|
+
const accepted = [].concat(lngs).filter(c => !isRefusedLanguageCode(this, c));
|
|
2158
|
+
if (accepted.length < [].concat(lngs).length) lngs = accepted;
|
|
2189
2159
|
}
|
|
2160
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2161
|
+
let l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2162
|
+
if (supportedLngs && isRefusedLanguageCode(this, l)) l = this.services.languageUtils.getBestMatchFromCodes([]);
|
|
2190
2163
|
if (l) {
|
|
2191
2164
|
if (!this.language) {
|
|
2192
2165
|
setLngProps(l);
|
|
@@ -2301,7 +2274,7 @@ class I18n extends EventEmitter {
|
|
|
2301
2274
|
}
|
|
2302
2275
|
loadNamespaces(ns, callback) {
|
|
2303
2276
|
const deferred = defer();
|
|
2304
|
-
if (!this.options.ns) {
|
|
2277
|
+
if (!this.options.ns || !this.services.backendConnector) {
|
|
2305
2278
|
if (callback) callback();
|
|
2306
2279
|
return Promise.resolve();
|
|
2307
2280
|
}
|
|
@@ -2319,7 +2292,7 @@ class I18n extends EventEmitter {
|
|
|
2319
2292
|
const deferred = defer();
|
|
2320
2293
|
if (isString(lngs)) lngs = [lngs];
|
|
2321
2294
|
const preloaded = this.options.preload || [];
|
|
2322
|
-
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2295
|
+
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && !isRefusedLanguageCode(this, lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2323
2296
|
if (!newLngs.length) {
|
|
2324
2297
|
if (callback) callback();
|
|
2325
2298
|
return Promise.resolve();
|
|
@@ -2368,16 +2341,15 @@ class I18n extends EventEmitter {
|
|
|
2368
2341
|
}
|
|
2369
2342
|
};
|
|
2370
2343
|
const clone = new I18n(mergedOptions);
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
}
|
|
2374
|
-
const membersToCopy = ['store', 'services', 'language'];
|
|
2344
|
+
clone.logger = options.debug !== undefined || options.prefix !== undefined ? this.logger.clone(options) : this.logger;
|
|
2345
|
+
const membersToCopy = ['store', 'language'];
|
|
2375
2346
|
membersToCopy.forEach(m => {
|
|
2376
2347
|
clone[m] = this[m];
|
|
2377
2348
|
});
|
|
2378
2349
|
clone.services = {
|
|
2379
2350
|
...this.services
|
|
2380
2351
|
};
|
|
2352
|
+
clone.services.postProcessors = Object.create(this.services.postProcessors);
|
|
2381
2353
|
clone.services.utils = {
|
|
2382
2354
|
...clone.services.utils,
|
|
2383
2355
|
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
@@ -2394,7 +2366,7 @@ class I18n extends EventEmitter {
|
|
|
2394
2366
|
return copied;
|
|
2395
2367
|
};
|
|
2396
2368
|
const clonedData = copyData(this.store.data);
|
|
2397
|
-
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2369
|
+
clone.store = new ResourceStore(clonedData, mergedOptions, this.logger);
|
|
2398
2370
|
clone.services.resourceStore = clone.store;
|
|
2399
2371
|
}
|
|
2400
2372
|
if (options.interpolation) {
|
|
@@ -2408,8 +2380,9 @@ class I18n extends EventEmitter {
|
|
|
2408
2380
|
...mergedOptions,
|
|
2409
2381
|
interpolation: mergedInterpolation
|
|
2410
2382
|
};
|
|
2411
|
-
clone.services.interpolator = new Interpolator(mergedForInterpolator);
|
|
2383
|
+
clone.services.interpolator = new Interpolator(mergedForInterpolator, this.logger);
|
|
2412
2384
|
}
|
|
2385
|
+
clone.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(options)]);
|
|
2413
2386
|
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2414
2387
|
clone.translator.on('*', (event, ...args) => {
|
|
2415
2388
|
clone.emit(event, ...args);
|