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/umd/i18next.js
CHANGED
|
@@ -118,12 +118,11 @@
|
|
|
118
118
|
'<': '<',
|
|
119
119
|
'>': '>',
|
|
120
120
|
'"': '"',
|
|
121
|
-
"'": '''
|
|
122
|
-
'/': '/'
|
|
121
|
+
"'": '''
|
|
123
122
|
};
|
|
124
123
|
const escape = data => {
|
|
125
124
|
if (isString(data)) {
|
|
126
|
-
return data.replace(/[&<>"'
|
|
125
|
+
return data.replace(/[&<>"']/g, s => _entityMap[s]);
|
|
127
126
|
}
|
|
128
127
|
return data;
|
|
129
128
|
};
|
|
@@ -170,6 +169,9 @@
|
|
|
170
169
|
if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
|
|
171
170
|
return obj[path];
|
|
172
171
|
}
|
|
172
|
+
if (!keySeparator) {
|
|
173
|
+
return Object.prototype.hasOwnProperty.call(obj, path) ? obj[path] : undefined;
|
|
174
|
+
}
|
|
173
175
|
const tokens = path.split(keySeparator);
|
|
174
176
|
let current = obj;
|
|
175
177
|
for (let i = 0; i < tokens.length;) {
|
|
@@ -317,15 +319,20 @@
|
|
|
317
319
|
}
|
|
318
320
|
|
|
319
321
|
const nonEmptyBundles = new WeakSet();
|
|
322
|
+
const assertSeparateArguments = (method, ...args) => {
|
|
323
|
+
if (args.some(a => !isString(a))) {
|
|
324
|
+
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`);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
320
327
|
class ResourceStore extends EventEmitter {
|
|
321
328
|
constructor(data, options = {
|
|
322
329
|
ns: ['translation'],
|
|
323
330
|
defaultNS: 'translation'
|
|
324
|
-
}) {
|
|
331
|
+
}, logger = baseLogger) {
|
|
325
332
|
super();
|
|
326
333
|
this.data = data || {};
|
|
327
334
|
this.options = options;
|
|
328
|
-
this.logger =
|
|
335
|
+
this.logger = logger.create('resourceStore');
|
|
329
336
|
if (this.options.keySeparator === undefined) {
|
|
330
337
|
this.options.keySeparator = '.';
|
|
331
338
|
}
|
|
@@ -344,49 +351,30 @@
|
|
|
344
351
|
this.options.ns.splice(index, 1);
|
|
345
352
|
}
|
|
346
353
|
}
|
|
347
|
-
deprecateDottedLng(method) {
|
|
348
|
-
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`);
|
|
349
|
-
}
|
|
350
354
|
getResource(lng, ns, key, options = {}) {
|
|
351
355
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
352
356
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
353
|
-
|
|
354
|
-
if (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
path.push(...key);
|
|
362
|
-
} else if (isString(key) && keySeparator) {
|
|
363
|
-
path.push(...key.split(keySeparator));
|
|
364
|
-
} else {
|
|
365
|
-
path.push(key);
|
|
366
|
-
}
|
|
357
|
+
const path = [lng, ns];
|
|
358
|
+
if (key) {
|
|
359
|
+
if (Array.isArray(key)) {
|
|
360
|
+
path.push(...key);
|
|
361
|
+
} else if (isString(key) && keySeparator) {
|
|
362
|
+
path.push(...key.split(keySeparator));
|
|
363
|
+
} else {
|
|
364
|
+
path.push(key);
|
|
367
365
|
}
|
|
368
366
|
}
|
|
369
367
|
const result = getPath(this.data, path);
|
|
370
|
-
if (result == null && !ns && !key && lng.includes('.')) {
|
|
371
|
-
lng = path[0];
|
|
372
|
-
ns = path[1];
|
|
373
|
-
key = path.slice(2).join('.');
|
|
374
|
-
}
|
|
375
368
|
if (result != null || !ignoreJSONStructure || !isString(key)) return result;
|
|
376
369
|
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
|
377
370
|
}
|
|
378
371
|
addResource(lng, ns, key, value, options = {
|
|
379
372
|
silent: false
|
|
380
373
|
}) {
|
|
374
|
+
assertSeparateArguments('addResource', lng, ns, key);
|
|
381
375
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
382
376
|
let path = [lng, ns];
|
|
383
377
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
384
|
-
if (lng.includes('.')) {
|
|
385
|
-
this.deprecateDottedLng('addResource');
|
|
386
|
-
path = lng.split('.');
|
|
387
|
-
value = ns;
|
|
388
|
-
ns = path[1];
|
|
389
|
-
}
|
|
390
378
|
this.addNamespaces(ns);
|
|
391
379
|
setPath(this.data, path, value);
|
|
392
380
|
if (!options.silent) this.emit('added', lng, ns, key, value);
|
|
@@ -394,25 +382,21 @@
|
|
|
394
382
|
addResources(lng, ns, resources, options = {
|
|
395
383
|
silent: false
|
|
396
384
|
}) {
|
|
397
|
-
|
|
398
|
-
|
|
385
|
+
assertSeparateArguments('addResources', lng, ns);
|
|
386
|
+
Object.keys(resources || {}).forEach(m => {
|
|
387
|
+
if (m === '__proto__' || m === 'constructor') return;
|
|
388
|
+
this.addResource(lng, ns, m, resources[m], {
|
|
399
389
|
silent: true
|
|
400
390
|
});
|
|
401
|
-
}
|
|
391
|
+
});
|
|
402
392
|
if (!options.silent) this.emit('added', lng, ns, resources);
|
|
403
393
|
}
|
|
404
394
|
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
|
405
395
|
silent: false,
|
|
406
396
|
skipCopy: false
|
|
407
397
|
}) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
this.deprecateDottedLng('addResourceBundle');
|
|
411
|
-
path = lng.split('.');
|
|
412
|
-
deep = resources;
|
|
413
|
-
resources = ns;
|
|
414
|
-
ns = path[1];
|
|
415
|
-
}
|
|
398
|
+
assertSeparateArguments('addResourceBundle', lng, ns);
|
|
399
|
+
const path = [lng, ns];
|
|
416
400
|
this.addNamespaces(ns);
|
|
417
401
|
let pack = getPath(this.data, path) || {};
|
|
418
402
|
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
|
|
@@ -460,19 +444,6 @@
|
|
|
460
444
|
}
|
|
461
445
|
}
|
|
462
446
|
|
|
463
|
-
var postProcessor = {
|
|
464
|
-
processors: {},
|
|
465
|
-
addPostProcessor(module) {
|
|
466
|
-
this.processors[module.name] = module;
|
|
467
|
-
},
|
|
468
|
-
handle(processors, value, key, options, translator) {
|
|
469
|
-
processors.forEach(processor => {
|
|
470
|
-
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
|
471
|
-
});
|
|
472
|
-
return value;
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
|
|
476
447
|
const PATH_KEY = Symbol('i18next/PATH_KEY');
|
|
477
448
|
function createProxy() {
|
|
478
449
|
const state = [];
|
|
@@ -491,7 +462,7 @@
|
|
|
491
462
|
const {
|
|
492
463
|
[PATH_KEY]: path
|
|
493
464
|
} = selector(createProxy());
|
|
494
|
-
const keySeparator = opts?.keySeparator
|
|
465
|
+
const keySeparator = opts?.keySeparator || '.';
|
|
495
466
|
const nsSeparator = opts?.nsSeparator ?? ':';
|
|
496
467
|
const strict = opts?.enableSelector === 'strict';
|
|
497
468
|
if (path.length > 1 && nsSeparator) {
|
|
@@ -511,12 +482,12 @@
|
|
|
511
482
|
class Translator extends EventEmitter {
|
|
512
483
|
constructor(services, options = {}) {
|
|
513
484
|
super();
|
|
514
|
-
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', '
|
|
485
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', 'postProcessors'], services, this);
|
|
515
486
|
this.options = options;
|
|
516
487
|
if (this.options.keySeparator === undefined) {
|
|
517
488
|
this.options.keySeparator = '.';
|
|
518
489
|
}
|
|
519
|
-
this.logger = baseLogger.create('translator');
|
|
490
|
+
this.logger = (services.logger || baseLogger).create('translator');
|
|
520
491
|
this.checkedLoadedFor = {};
|
|
521
492
|
}
|
|
522
493
|
changeLanguage(lng) {
|
|
@@ -640,23 +611,24 @@
|
|
|
640
611
|
}
|
|
641
612
|
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
|
642
613
|
const resType = Object.prototype.toString.apply(resForObjHndl);
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
resolved.res = r;
|
|
655
|
-
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
656
|
-
return resolved;
|
|
657
|
-
}
|
|
658
|
-
return r;
|
|
614
|
+
const isObjectHit = handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && !noObject.includes(resType) && !(isString(joinArrays) && Array.isArray(resForObjHndl));
|
|
615
|
+
const objectNotReturned = isObjectHit && !opt.returnObjects && !this.options.returnObjects;
|
|
616
|
+
if (objectNotReturned && this.options.returnedObjectHandler) {
|
|
617
|
+
const r = this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
618
|
+
...opt,
|
|
619
|
+
ns: namespaces
|
|
620
|
+
});
|
|
621
|
+
if (returnDetails) {
|
|
622
|
+
resolved.res = r;
|
|
623
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
624
|
+
return resolved;
|
|
659
625
|
}
|
|
626
|
+
return r;
|
|
627
|
+
}
|
|
628
|
+
if (objectNotReturned) {
|
|
629
|
+
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
|
630
|
+
}
|
|
631
|
+
if (isObjectHit && !objectNotReturned) {
|
|
660
632
|
if (keySeparator) {
|
|
661
633
|
const resTypeIsArray = Array.isArray(resForObjHndl);
|
|
662
634
|
const copy = resTypeIsArray ? [] : {};
|
|
@@ -693,18 +665,19 @@
|
|
|
693
665
|
} else {
|
|
694
666
|
let usedDefault = false;
|
|
695
667
|
let usedKey = false;
|
|
696
|
-
if (
|
|
668
|
+
if (objectNotReturned) res = undefined;
|
|
669
|
+
if (!this.isValidLookup(res) && hasDefaultValue && !(objectNotReturned && shouldHandleAsObject(defaultValue))) {
|
|
697
670
|
usedDefault = true;
|
|
698
671
|
res = defaultValue;
|
|
699
672
|
}
|
|
700
|
-
if (!this.isValidLookup(res)) {
|
|
673
|
+
if (!this.isValidLookup(res) && !(usedDefault && res === '')) {
|
|
701
674
|
usedKey = true;
|
|
702
675
|
res = key;
|
|
703
676
|
}
|
|
704
677
|
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
|
705
678
|
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
|
|
706
679
|
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
|
707
|
-
if (usedKey || usedDefault || updateMissing) {
|
|
680
|
+
if (!objectNotReturned && (usedKey || usedDefault || updateMissing)) {
|
|
708
681
|
this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, needsPluralHandling && !updateMissing ? `${key}${this.pluralResolver.getSuffix(lng, opt.count, opt)}` : key, updateMissing ? defaultValue : res);
|
|
709
682
|
if (keySeparator) {
|
|
710
683
|
const fk = this.resolve(key, {
|
|
@@ -812,18 +785,21 @@
|
|
|
812
785
|
const postProcess = opt.postProcess || this.options.postProcess;
|
|
813
786
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
814
787
|
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
815
|
-
|
|
816
|
-
if (this.logger.debug && postProcessor.processors[name] && this.ownPostProcessors && !this.ownPostProcessors.has(name)) {
|
|
817
|
-
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`);
|
|
818
|
-
}
|
|
819
|
-
});
|
|
820
|
-
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
788
|
+
const ppOptions = this.options?.postProcessPassResolved ? {
|
|
821
789
|
i18nResolved: {
|
|
822
790
|
...resolved,
|
|
823
791
|
usedParams: this.getUsedParamsDetails(opt)
|
|
824
792
|
},
|
|
825
793
|
...opt
|
|
826
|
-
} : opt
|
|
794
|
+
} : opt;
|
|
795
|
+
postProcessorNames.forEach(name => {
|
|
796
|
+
const processor = this.postProcessors?.[name];
|
|
797
|
+
if (!processor) {
|
|
798
|
+
this.logger.once(`postProcess:${name}`, 'warn', `postProcess: '${name}' is not registered on this i18next instance; call use() with it on this instance`);
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
res = processor.process(res, key, ppOptions, this) ?? res;
|
|
802
|
+
});
|
|
827
803
|
}
|
|
828
804
|
return res;
|
|
829
805
|
}
|
|
@@ -953,14 +929,16 @@
|
|
|
953
929
|
}
|
|
954
930
|
|
|
955
931
|
class LanguageUtil {
|
|
956
|
-
constructor(options) {
|
|
932
|
+
constructor(options, logger = baseLogger) {
|
|
957
933
|
this.options = options;
|
|
958
934
|
this.supportedLngs = this.options.supportedLngs || false;
|
|
959
|
-
this.logger =
|
|
935
|
+
this.logger = logger.create('languageUtils');
|
|
960
936
|
this.resolveHierarchyCache = {};
|
|
937
|
+
this.formattedCodes = new Map();
|
|
961
938
|
}
|
|
962
939
|
clearCache() {
|
|
963
940
|
this.resolveHierarchyCache = {};
|
|
941
|
+
this.formattedCodes.clear();
|
|
964
942
|
}
|
|
965
943
|
getScriptPartFromCode(code) {
|
|
966
944
|
code = getCleanedCode(code);
|
|
@@ -978,35 +956,27 @@
|
|
|
978
956
|
return this.formatLanguageCode(p[0]);
|
|
979
957
|
}
|
|
980
958
|
formatLanguageCode(code) {
|
|
981
|
-
if (isString(code)
|
|
982
|
-
|
|
959
|
+
if (!isString(code)) return code;
|
|
960
|
+
let formatted = this.formattedCodes.get(code);
|
|
961
|
+
if (formatted !== undefined) return formatted;
|
|
962
|
+
formatted = this.formatUncached(code);
|
|
963
|
+
if (this.formattedCodes.size >= 1000) this.formattedCodes.clear();
|
|
964
|
+
this.formattedCodes.set(code, formatted);
|
|
965
|
+
return formatted;
|
|
966
|
+
}
|
|
967
|
+
formatUncached(code) {
|
|
968
|
+
const hasSubtags = code.includes('-');
|
|
969
|
+
if (hasSubtags || this.options.cleanCode !== false) {
|
|
970
|
+
let canonical;
|
|
983
971
|
try {
|
|
984
|
-
|
|
972
|
+
canonical = Intl.getCanonicalLocales(code)[0];
|
|
985
973
|
} catch (e) {}
|
|
986
|
-
if (
|
|
987
|
-
|
|
988
|
-
}
|
|
989
|
-
if (formattedCode) return formattedCode;
|
|
990
|
-
if (this.options.lowerCaseLng) {
|
|
991
|
-
return code.toLowerCase();
|
|
974
|
+
if (canonical && (hasSubtags || canonical.toLowerCase() === code.toLowerCase())) {
|
|
975
|
+
return this.options.lowerCaseLng ? canonical.toLowerCase() : canonical;
|
|
992
976
|
}
|
|
993
|
-
return code;
|
|
977
|
+
if (canonical) return code.toLowerCase();
|
|
994
978
|
}
|
|
995
|
-
|
|
996
|
-
if (this.logger.debug && isString(code)) this.deprecateBareCode(code, formatted);
|
|
997
|
-
return formatted;
|
|
998
|
-
}
|
|
999
|
-
deprecateBareCode(code, formatted) {
|
|
1000
|
-
let canonical;
|
|
1001
|
-
try {
|
|
1002
|
-
canonical = Intl.getCanonicalLocales(code)[0];
|
|
1003
|
-
} catch (e) {
|
|
1004
|
-
return;
|
|
1005
|
-
}
|
|
1006
|
-
let v27 = canonical.toLowerCase() === code.toLowerCase() ? canonical : code.toLowerCase();
|
|
1007
|
-
if (this.options.lowerCaseLng) v27 = v27.toLowerCase();
|
|
1008
|
-
if (v27 === formatted) return;
|
|
1009
|
-
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`);
|
|
979
|
+
return this.options.lowerCaseLng || this.options.cleanCode && !hasSubtags ? code.toLowerCase() : code;
|
|
1010
980
|
}
|
|
1011
981
|
isSupportedCode(code) {
|
|
1012
982
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1117,10 +1087,10 @@
|
|
|
1117
1087
|
})
|
|
1118
1088
|
};
|
|
1119
1089
|
class PluralResolver {
|
|
1120
|
-
constructor(languageUtils, options = {}) {
|
|
1090
|
+
constructor(languageUtils, options = {}, logger = baseLogger) {
|
|
1121
1091
|
this.languageUtils = languageUtils;
|
|
1122
1092
|
this.options = options;
|
|
1123
|
-
this.logger =
|
|
1093
|
+
this.logger = logger.create('pluralResolver');
|
|
1124
1094
|
this.pluralRulesCache = {};
|
|
1125
1095
|
}
|
|
1126
1096
|
clearCache() {
|
|
@@ -1186,8 +1156,8 @@
|
|
|
1186
1156
|
return path;
|
|
1187
1157
|
};
|
|
1188
1158
|
class Interpolator {
|
|
1189
|
-
constructor(options = {}) {
|
|
1190
|
-
this.logger =
|
|
1159
|
+
constructor(options = {}, logger = baseLogger) {
|
|
1160
|
+
this.logger = logger.create('interpolator');
|
|
1191
1161
|
this.options = options;
|
|
1192
1162
|
this.format = options?.interpolation?.format || (value => value);
|
|
1193
1163
|
this.init(options);
|
|
@@ -1201,39 +1171,26 @@
|
|
|
1201
1171
|
escapeValue,
|
|
1202
1172
|
useRawValueToEscape,
|
|
1203
1173
|
prefix,
|
|
1204
|
-
prefixEscaped,
|
|
1205
1174
|
suffix,
|
|
1206
|
-
suffixEscaped,
|
|
1207
1175
|
formatSeparator,
|
|
1208
1176
|
unescapeSuffix,
|
|
1209
1177
|
unescapePrefix,
|
|
1210
1178
|
nestingPrefix,
|
|
1211
|
-
nestingPrefixEscaped,
|
|
1212
1179
|
nestingSuffix,
|
|
1213
|
-
nestingSuffixEscaped,
|
|
1214
1180
|
nestingOptionsSeparator,
|
|
1215
1181
|
maxReplaces,
|
|
1216
1182
|
alwaysFormat
|
|
1217
1183
|
} = options.interpolation;
|
|
1218
|
-
if (prefixEscaped || suffixEscaped || nestingPrefixEscaped || nestingSuffixEscaped) {
|
|
1219
|
-
const names = Object.entries({
|
|
1220
|
-
prefixEscaped,
|
|
1221
|
-
suffixEscaped,
|
|
1222
|
-
nestingPrefixEscaped,
|
|
1223
|
-
nestingSuffixEscaped
|
|
1224
|
-
}).filter(([, v]) => v).map(([k]) => k);
|
|
1225
|
-
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`);
|
|
1226
|
-
}
|
|
1227
1184
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1228
1185
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1229
1186
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
1230
|
-
this.prefix = prefix ? regexEscape(prefix) :
|
|
1231
|
-
this.suffix = suffix ? regexEscape(suffix) :
|
|
1187
|
+
this.prefix = prefix ? regexEscape(prefix) : '{{';
|
|
1188
|
+
this.suffix = suffix ? regexEscape(suffix) : '}}';
|
|
1232
1189
|
this.formatSeparator = formatSeparator || ',';
|
|
1233
1190
|
this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix ? regexEscape(unescapePrefix) : '-';
|
|
1234
1191
|
this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix ? regexEscape(unescapeSuffix) : '';
|
|
1235
|
-
this.nestingPrefix =
|
|
1236
|
-
this.nestingSuffix =
|
|
1192
|
+
this.nestingPrefix = regexEscape(nestingPrefix || '$t(');
|
|
1193
|
+
this.nestingSuffix = regexEscape(nestingSuffix || ')');
|
|
1237
1194
|
this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
|
|
1238
1195
|
this.maxReplaces = maxReplaces || 1000;
|
|
1239
1196
|
this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
|
|
@@ -1289,13 +1246,7 @@
|
|
|
1289
1246
|
safeValue: val => val
|
|
1290
1247
|
}, {
|
|
1291
1248
|
regex: this.regexp,
|
|
1292
|
-
safeValue: val =>
|
|
1293
|
-
if (!this.escapeValue) return val;
|
|
1294
|
-
if (this.escape === escape && this.options?.interpolation?.escapeValue !== false && isString(val) && val.includes('/')) {
|
|
1295
|
-
this.logger.once('/', 'deprecate', "escapeValue renders '/' in a value as '/' today; from v27 the default escape leaves '/' as it is");
|
|
1296
|
-
}
|
|
1297
|
-
return this.escape(val);
|
|
1298
|
-
}
|
|
1249
|
+
safeValue: val => this.escapeValue ? this.escape(val) : val
|
|
1299
1250
|
}];
|
|
1300
1251
|
const unescapedValues = [];
|
|
1301
1252
|
let shift = 0;
|
|
@@ -1447,8 +1398,9 @@
|
|
|
1447
1398
|
formatOptions
|
|
1448
1399
|
};
|
|
1449
1400
|
};
|
|
1401
|
+
const cachedFormattersLimit = 1000;
|
|
1450
1402
|
const createCachedFormatter = fn => {
|
|
1451
|
-
const cache =
|
|
1403
|
+
const cache = new Map();
|
|
1452
1404
|
return (v, l, o) => {
|
|
1453
1405
|
let optForCache = o;
|
|
1454
1406
|
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
|
@@ -1463,10 +1415,11 @@
|
|
|
1463
1415
|
} catch (e) {
|
|
1464
1416
|
return fn(getCleanedCode(l), o)(v);
|
|
1465
1417
|
}
|
|
1466
|
-
let frm = cache
|
|
1418
|
+
let frm = cache.get(key);
|
|
1467
1419
|
if (!frm) {
|
|
1468
1420
|
frm = fn(getCleanedCode(l), o);
|
|
1469
|
-
cache
|
|
1421
|
+
if (cache.size >= cachedFormattersLimit) cache.delete(cache.keys().next().value);
|
|
1422
|
+
cache.set(key, frm);
|
|
1470
1423
|
}
|
|
1471
1424
|
return frm(v);
|
|
1472
1425
|
};
|
|
@@ -1481,6 +1434,7 @@
|
|
|
1481
1434
|
init(services, options = {
|
|
1482
1435
|
interpolation: {}
|
|
1483
1436
|
}) {
|
|
1437
|
+
if (services?.logger?.create) this.logger = services.logger.create('formatter');
|
|
1484
1438
|
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
|
1485
1439
|
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1486
1440
|
this.formats = {
|
|
@@ -1577,7 +1531,7 @@
|
|
|
1577
1531
|
this.services = services;
|
|
1578
1532
|
this.languageUtils = services.languageUtils;
|
|
1579
1533
|
this.options = options;
|
|
1580
|
-
this.logger = baseLogger.create('backendConnector');
|
|
1534
|
+
this.logger = (services.logger || baseLogger).create('backendConnector');
|
|
1581
1535
|
this.waitingReads = [];
|
|
1582
1536
|
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1583
1537
|
this.readingCalls = 0;
|
|
@@ -1736,15 +1690,10 @@
|
|
|
1736
1690
|
reload: true
|
|
1737
1691
|
}, callback);
|
|
1738
1692
|
}
|
|
1739
|
-
noticeDevFallback(lng) {
|
|
1740
|
-
if (lng !== 'dev' || !this.devIsDefaultFallback) return;
|
|
1741
|
-
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");
|
|
1742
|
-
}
|
|
1743
1693
|
loadOne(name, prefix = '') {
|
|
1744
1694
|
const s = name.split('|');
|
|
1745
1695
|
const lng = s[0];
|
|
1746
1696
|
const ns = s[1];
|
|
1747
|
-
this.noticeDevFallback(lng);
|
|
1748
1697
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1749
1698
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1750
1699
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
@@ -1758,7 +1707,6 @@
|
|
|
1758
1707
|
}
|
|
1759
1708
|
if (key === undefined || key === null || key === '') return;
|
|
1760
1709
|
if (this.backend?.create) {
|
|
1761
|
-
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1762
1710
|
const opts = {
|
|
1763
1711
|
...options,
|
|
1764
1712
|
isUpdate
|
|
@@ -1794,7 +1742,7 @@
|
|
|
1794
1742
|
initAsync: true,
|
|
1795
1743
|
ns: ['translation'],
|
|
1796
1744
|
defaultNS: ['translation'],
|
|
1797
|
-
fallbackLng:
|
|
1745
|
+
fallbackLng: false,
|
|
1798
1746
|
fallbackNS: false,
|
|
1799
1747
|
supportedLngs: false,
|
|
1800
1748
|
nonExplicitSupportedLngs: false,
|
|
@@ -1815,7 +1763,7 @@
|
|
|
1815
1763
|
postProcess: false,
|
|
1816
1764
|
postProcessPassResolved: false,
|
|
1817
1765
|
returnNull: false,
|
|
1818
|
-
returnEmptyString:
|
|
1766
|
+
returnEmptyString: false,
|
|
1819
1767
|
returnObjects: false,
|
|
1820
1768
|
joinArrays: false,
|
|
1821
1769
|
returnedObjectHandler: false,
|
|
@@ -1860,7 +1808,38 @@
|
|
|
1860
1808
|
};
|
|
1861
1809
|
|
|
1862
1810
|
const noop = () => {};
|
|
1863
|
-
|
|
1811
|
+
const withDefaults = options => {
|
|
1812
|
+
const defOpts = get();
|
|
1813
|
+
const merged = {
|
|
1814
|
+
...defOpts,
|
|
1815
|
+
...options
|
|
1816
|
+
};
|
|
1817
|
+
merged.interpolation = {
|
|
1818
|
+
...defOpts.interpolation,
|
|
1819
|
+
...options.interpolation
|
|
1820
|
+
};
|
|
1821
|
+
if (typeof merged.overloadTranslationOptionHandler !== 'function') {
|
|
1822
|
+
merged.overloadTranslationOptionHandler = defOpts.overloadTranslationOptionHandler;
|
|
1823
|
+
}
|
|
1824
|
+
return merged;
|
|
1825
|
+
};
|
|
1826
|
+
const configuredLanguageCodes = options => {
|
|
1827
|
+
const codes = [].concat(options.supportedLngs || [], options.preload || []);
|
|
1828
|
+
codes.push(...Object.keys(options.resources || {}));
|
|
1829
|
+
const fallbackLng = options.fallbackLng;
|
|
1830
|
+
if (isString(fallbackLng) || Array.isArray(fallbackLng)) codes.push(...[].concat(fallbackLng));else if (fallbackLng && typeof fallbackLng === 'object') {
|
|
1831
|
+
Object.keys(fallbackLng).forEach(code => {
|
|
1832
|
+
if (code !== 'default') codes.push(code);
|
|
1833
|
+
codes.push(...[].concat(fallbackLng[code]));
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
return codes;
|
|
1837
|
+
};
|
|
1838
|
+
const isRefusedLanguageCode = (i18n, code) => {
|
|
1839
|
+
if (!isString(code) || isSafeLanguageCode(code) || i18n.configuredLngs?.has(code)) return false;
|
|
1840
|
+
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`);
|
|
1841
|
+
return true;
|
|
1842
|
+
};
|
|
1864
1843
|
const bindMemberFunctions = inst => {
|
|
1865
1844
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1866
1845
|
mems.forEach(mem => {
|
|
@@ -1872,17 +1851,18 @@
|
|
|
1872
1851
|
class I18n extends EventEmitter {
|
|
1873
1852
|
constructor(options = {}, callback) {
|
|
1874
1853
|
super();
|
|
1875
|
-
this.
|
|
1854
|
+
this.userOptions = transformOptions(options);
|
|
1855
|
+
this.options = withDefaults(this.userOptions);
|
|
1876
1856
|
this.services = {
|
|
1877
|
-
|
|
1857
|
+
postProcessors: Object.create(null)
|
|
1878
1858
|
};
|
|
1879
|
-
this.logger =
|
|
1859
|
+
this.logger = new Logger(null, this.options);
|
|
1880
1860
|
this.modules = {
|
|
1881
1861
|
external: []
|
|
1882
1862
|
};
|
|
1883
1863
|
bindMemberFunctions(this);
|
|
1884
1864
|
if (callback && !this.isInitialized && !options.isClone) {
|
|
1885
|
-
if (!this.
|
|
1865
|
+
if (!this.userOptions.initAsync) {
|
|
1886
1866
|
this.init(options, callback);
|
|
1887
1867
|
return this;
|
|
1888
1868
|
}
|
|
@@ -1904,25 +1884,23 @@
|
|
|
1904
1884
|
options.defaultNS = options.ns[0];
|
|
1905
1885
|
}
|
|
1906
1886
|
}
|
|
1907
|
-
|
|
1908
|
-
const
|
|
1909
|
-
|
|
1910
|
-
...
|
|
1911
|
-
...this.options,
|
|
1912
|
-
...transformOptions(options)
|
|
1913
|
-
};
|
|
1914
|
-
this.options.interpolation = {
|
|
1915
|
-
...defOpts.interpolation,
|
|
1916
|
-
...this.options.interpolation
|
|
1887
|
+
transformOptions(options);
|
|
1888
|
+
const userOptions = {
|
|
1889
|
+
...this.userOptions,
|
|
1890
|
+
...options
|
|
1917
1891
|
};
|
|
1918
|
-
|
|
1919
|
-
this.options
|
|
1892
|
+
this.options = withDefaults({
|
|
1893
|
+
...this.options,
|
|
1894
|
+
...options
|
|
1895
|
+
});
|
|
1896
|
+
if (userOptions.keySeparator !== undefined) {
|
|
1897
|
+
this.options.userDefinedKeySeparator = userOptions.keySeparator;
|
|
1920
1898
|
}
|
|
1921
|
-
if (
|
|
1922
|
-
this.options.userDefinedNsSeparator =
|
|
1899
|
+
if (userOptions.nsSeparator !== undefined) {
|
|
1900
|
+
this.options.userDefinedNsSeparator = userOptions.nsSeparator;
|
|
1923
1901
|
}
|
|
1924
|
-
if (
|
|
1925
|
-
this.
|
|
1902
|
+
if (!this.options.isClone) {
|
|
1903
|
+
this.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(userOptions)]);
|
|
1926
1904
|
}
|
|
1927
1905
|
const createClassOnDemand = ClassOrObject => {
|
|
1928
1906
|
if (!ClassOrObject) return null;
|
|
@@ -1930,15 +1908,10 @@
|
|
|
1930
1908
|
return ClassOrObject;
|
|
1931
1909
|
};
|
|
1932
1910
|
if (!this.options.isClone) {
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
if (this.modules.logger) {
|
|
1938
|
-
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1939
|
-
} else {
|
|
1940
|
-
baseLogger.init(null, this.options);
|
|
1941
|
-
}
|
|
1911
|
+
this.logger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1912
|
+
const {
|
|
1913
|
+
logger
|
|
1914
|
+
} = this;
|
|
1942
1915
|
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1943
1916
|
this.pendingWarnings = undefined;
|
|
1944
1917
|
let formatter;
|
|
@@ -1947,27 +1920,26 @@
|
|
|
1947
1920
|
} else {
|
|
1948
1921
|
formatter = Formatter;
|
|
1949
1922
|
}
|
|
1950
|
-
const lu = new LanguageUtil(this.options);
|
|
1951
|
-
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1923
|
+
const lu = new LanguageUtil(this.options, logger);
|
|
1924
|
+
this.store = new ResourceStore(this.options.resources, this.options, logger);
|
|
1952
1925
|
const s = this.services;
|
|
1953
|
-
s.logger =
|
|
1926
|
+
s.logger = logger;
|
|
1954
1927
|
s.resourceStore = this.store;
|
|
1955
1928
|
s.languageUtils = lu;
|
|
1956
1929
|
s.pluralResolver = new PluralResolver(lu, {
|
|
1957
1930
|
prepend: this.options.pluralSeparator
|
|
1958
|
-
});
|
|
1931
|
+
}, logger);
|
|
1959
1932
|
if (formatter) {
|
|
1960
1933
|
s.formatter = createClassOnDemand(formatter);
|
|
1961
1934
|
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1962
1935
|
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1963
1936
|
}
|
|
1964
|
-
s.interpolator = new Interpolator(this.options);
|
|
1937
|
+
s.interpolator = new Interpolator(this.options, logger);
|
|
1965
1938
|
s.utils = {
|
|
1966
1939
|
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1967
1940
|
reloadResources: this.reloadResources.bind(this)
|
|
1968
1941
|
};
|
|
1969
1942
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1970
|
-
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1971
1943
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1972
1944
|
this.emit(event, ...args);
|
|
1973
1945
|
});
|
|
@@ -2107,8 +2079,7 @@
|
|
|
2107
2079
|
setModule('i18nFormat');
|
|
2108
2080
|
}
|
|
2109
2081
|
if (module.type === 'postProcessor') {
|
|
2110
|
-
|
|
2111
|
-
this.services.ownPostProcessors?.add(module.name);
|
|
2082
|
+
this.services.postProcessors[module.name] = module;
|
|
2112
2083
|
}
|
|
2113
2084
|
if (module.type === 'formatter') {
|
|
2114
2085
|
setModule('formatter');
|
|
@@ -2183,14 +2154,16 @@
|
|
|
2183
2154
|
};
|
|
2184
2155
|
const setLng = lngs => {
|
|
2185
2156
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2186
|
-
const
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
}
|
|
2157
|
+
const {
|
|
2158
|
+
supportedLngs
|
|
2159
|
+
} = this.options;
|
|
2160
|
+
if (lngs && !supportedLngs) {
|
|
2161
|
+
const accepted = [].concat(lngs).filter(c => !isRefusedLanguageCode(this, c));
|
|
2162
|
+
if (accepted.length < [].concat(lngs).length) lngs = accepted;
|
|
2193
2163
|
}
|
|
2164
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2165
|
+
let l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2166
|
+
if (supportedLngs && isRefusedLanguageCode(this, l)) l = this.services.languageUtils.getBestMatchFromCodes([]);
|
|
2194
2167
|
if (l) {
|
|
2195
2168
|
if (!this.language) {
|
|
2196
2169
|
setLngProps(l);
|
|
@@ -2305,7 +2278,7 @@
|
|
|
2305
2278
|
}
|
|
2306
2279
|
loadNamespaces(ns, callback) {
|
|
2307
2280
|
const deferred = defer();
|
|
2308
|
-
if (!this.options.ns) {
|
|
2281
|
+
if (!this.options.ns || !this.services.backendConnector) {
|
|
2309
2282
|
if (callback) callback();
|
|
2310
2283
|
return Promise.resolve();
|
|
2311
2284
|
}
|
|
@@ -2323,7 +2296,7 @@
|
|
|
2323
2296
|
const deferred = defer();
|
|
2324
2297
|
if (isString(lngs)) lngs = [lngs];
|
|
2325
2298
|
const preloaded = this.options.preload || [];
|
|
2326
|
-
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2299
|
+
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && !isRefusedLanguageCode(this, lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2327
2300
|
if (!newLngs.length) {
|
|
2328
2301
|
if (callback) callback();
|
|
2329
2302
|
return Promise.resolve();
|
|
@@ -2372,16 +2345,15 @@
|
|
|
2372
2345
|
}
|
|
2373
2346
|
};
|
|
2374
2347
|
const clone = new I18n(mergedOptions);
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
}
|
|
2378
|
-
const membersToCopy = ['store', 'services', 'language'];
|
|
2348
|
+
clone.logger = options.debug !== undefined || options.prefix !== undefined ? this.logger.clone(options) : this.logger;
|
|
2349
|
+
const membersToCopy = ['store', 'language'];
|
|
2379
2350
|
membersToCopy.forEach(m => {
|
|
2380
2351
|
clone[m] = this[m];
|
|
2381
2352
|
});
|
|
2382
2353
|
clone.services = {
|
|
2383
2354
|
...this.services
|
|
2384
2355
|
};
|
|
2356
|
+
clone.services.postProcessors = Object.create(this.services.postProcessors);
|
|
2385
2357
|
clone.services.utils = {
|
|
2386
2358
|
...clone.services.utils,
|
|
2387
2359
|
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
@@ -2398,7 +2370,7 @@
|
|
|
2398
2370
|
return copied;
|
|
2399
2371
|
};
|
|
2400
2372
|
const clonedData = copyData(this.store.data);
|
|
2401
|
-
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2373
|
+
clone.store = new ResourceStore(clonedData, mergedOptions, this.logger);
|
|
2402
2374
|
clone.services.resourceStore = clone.store;
|
|
2403
2375
|
}
|
|
2404
2376
|
if (options.interpolation) {
|
|
@@ -2412,8 +2384,9 @@
|
|
|
2412
2384
|
...mergedOptions,
|
|
2413
2385
|
interpolation: mergedInterpolation
|
|
2414
2386
|
};
|
|
2415
|
-
clone.services.interpolator = new Interpolator(mergedForInterpolator);
|
|
2387
|
+
clone.services.interpolator = new Interpolator(mergedForInterpolator, this.logger);
|
|
2416
2388
|
}
|
|
2389
|
+
clone.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(options)]);
|
|
2417
2390
|
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2418
2391
|
clone.translator.on('*', (event, ...args) => {
|
|
2419
2392
|
clone.emit(event, ...args);
|