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