i18next 26.7.2 → 27.0.0
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 +174 -203
- package/dist/esm/i18next.js +174 -203
- package/dist/esm/package.json +1 -1
- package/dist/umd/i18next.js +174 -203
- package/dist/umd/i18next.min.js +1 -1
- package/i18next.js +174 -203
- 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/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;
|
|
@@ -1481,6 +1432,7 @@
|
|
|
1481
1432
|
init(services, options = {
|
|
1482
1433
|
interpolation: {}
|
|
1483
1434
|
}) {
|
|
1435
|
+
if (services?.logger?.create) this.logger = services.logger.create('formatter');
|
|
1484
1436
|
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
|
1485
1437
|
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1486
1438
|
this.formats = {
|
|
@@ -1577,7 +1529,7 @@
|
|
|
1577
1529
|
this.services = services;
|
|
1578
1530
|
this.languageUtils = services.languageUtils;
|
|
1579
1531
|
this.options = options;
|
|
1580
|
-
this.logger = baseLogger.create('backendConnector');
|
|
1532
|
+
this.logger = (services.logger || baseLogger).create('backendConnector');
|
|
1581
1533
|
this.waitingReads = [];
|
|
1582
1534
|
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1583
1535
|
this.readingCalls = 0;
|
|
@@ -1736,15 +1688,10 @@
|
|
|
1736
1688
|
reload: true
|
|
1737
1689
|
}, callback);
|
|
1738
1690
|
}
|
|
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
1691
|
loadOne(name, prefix = '') {
|
|
1744
1692
|
const s = name.split('|');
|
|
1745
1693
|
const lng = s[0];
|
|
1746
1694
|
const ns = s[1];
|
|
1747
|
-
this.noticeDevFallback(lng);
|
|
1748
1695
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1749
1696
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1750
1697
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
@@ -1758,7 +1705,6 @@
|
|
|
1758
1705
|
}
|
|
1759
1706
|
if (key === undefined || key === null || key === '') return;
|
|
1760
1707
|
if (this.backend?.create) {
|
|
1761
|
-
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1762
1708
|
const opts = {
|
|
1763
1709
|
...options,
|
|
1764
1710
|
isUpdate
|
|
@@ -1794,7 +1740,7 @@
|
|
|
1794
1740
|
initAsync: true,
|
|
1795
1741
|
ns: ['translation'],
|
|
1796
1742
|
defaultNS: ['translation'],
|
|
1797
|
-
fallbackLng:
|
|
1743
|
+
fallbackLng: false,
|
|
1798
1744
|
fallbackNS: false,
|
|
1799
1745
|
supportedLngs: false,
|
|
1800
1746
|
nonExplicitSupportedLngs: false,
|
|
@@ -1815,7 +1761,7 @@
|
|
|
1815
1761
|
postProcess: false,
|
|
1816
1762
|
postProcessPassResolved: false,
|
|
1817
1763
|
returnNull: false,
|
|
1818
|
-
returnEmptyString:
|
|
1764
|
+
returnEmptyString: false,
|
|
1819
1765
|
returnObjects: false,
|
|
1820
1766
|
joinArrays: false,
|
|
1821
1767
|
returnedObjectHandler: false,
|
|
@@ -1860,7 +1806,38 @@
|
|
|
1860
1806
|
};
|
|
1861
1807
|
|
|
1862
1808
|
const noop = () => {};
|
|
1863
|
-
|
|
1809
|
+
const withDefaults = options => {
|
|
1810
|
+
const defOpts = get();
|
|
1811
|
+
const merged = {
|
|
1812
|
+
...defOpts,
|
|
1813
|
+
...options
|
|
1814
|
+
};
|
|
1815
|
+
merged.interpolation = {
|
|
1816
|
+
...defOpts.interpolation,
|
|
1817
|
+
...options.interpolation
|
|
1818
|
+
};
|
|
1819
|
+
if (typeof merged.overloadTranslationOptionHandler !== 'function') {
|
|
1820
|
+
merged.overloadTranslationOptionHandler = defOpts.overloadTranslationOptionHandler;
|
|
1821
|
+
}
|
|
1822
|
+
return merged;
|
|
1823
|
+
};
|
|
1824
|
+
const configuredLanguageCodes = options => {
|
|
1825
|
+
const codes = [].concat(options.supportedLngs || [], options.preload || []);
|
|
1826
|
+
codes.push(...Object.keys(options.resources || {}));
|
|
1827
|
+
const fallbackLng = options.fallbackLng;
|
|
1828
|
+
if (isString(fallbackLng) || Array.isArray(fallbackLng)) codes.push(...[].concat(fallbackLng));else if (fallbackLng && typeof fallbackLng === 'object') {
|
|
1829
|
+
Object.keys(fallbackLng).forEach(code => {
|
|
1830
|
+
if (code !== 'default') codes.push(code);
|
|
1831
|
+
codes.push(...[].concat(fallbackLng[code]));
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1834
|
+
return codes;
|
|
1835
|
+
};
|
|
1836
|
+
const isRefusedLanguageCode = (i18n, code) => {
|
|
1837
|
+
if (!isString(code) || isSafeLanguageCode(code) || i18n.configuredLngs?.has(code)) return false;
|
|
1838
|
+
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`);
|
|
1839
|
+
return true;
|
|
1840
|
+
};
|
|
1864
1841
|
const bindMemberFunctions = inst => {
|
|
1865
1842
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1866
1843
|
mems.forEach(mem => {
|
|
@@ -1872,17 +1849,18 @@
|
|
|
1872
1849
|
class I18n extends EventEmitter {
|
|
1873
1850
|
constructor(options = {}, callback) {
|
|
1874
1851
|
super();
|
|
1875
|
-
this.
|
|
1852
|
+
this.userOptions = transformOptions(options);
|
|
1853
|
+
this.options = withDefaults(this.userOptions);
|
|
1876
1854
|
this.services = {
|
|
1877
|
-
|
|
1855
|
+
postProcessors: Object.create(null)
|
|
1878
1856
|
};
|
|
1879
|
-
this.logger =
|
|
1857
|
+
this.logger = new Logger(null, this.options);
|
|
1880
1858
|
this.modules = {
|
|
1881
1859
|
external: []
|
|
1882
1860
|
};
|
|
1883
1861
|
bindMemberFunctions(this);
|
|
1884
1862
|
if (callback && !this.isInitialized && !options.isClone) {
|
|
1885
|
-
if (!this.
|
|
1863
|
+
if (!this.userOptions.initAsync) {
|
|
1886
1864
|
this.init(options, callback);
|
|
1887
1865
|
return this;
|
|
1888
1866
|
}
|
|
@@ -1904,25 +1882,23 @@
|
|
|
1904
1882
|
options.defaultNS = options.ns[0];
|
|
1905
1883
|
}
|
|
1906
1884
|
}
|
|
1907
|
-
|
|
1908
|
-
const
|
|
1909
|
-
|
|
1910
|
-
...
|
|
1911
|
-
...this.options,
|
|
1912
|
-
...transformOptions(options)
|
|
1913
|
-
};
|
|
1914
|
-
this.options.interpolation = {
|
|
1915
|
-
...defOpts.interpolation,
|
|
1916
|
-
...this.options.interpolation
|
|
1885
|
+
transformOptions(options);
|
|
1886
|
+
const userOptions = {
|
|
1887
|
+
...this.userOptions,
|
|
1888
|
+
...options
|
|
1917
1889
|
};
|
|
1918
|
-
|
|
1919
|
-
this.options
|
|
1890
|
+
this.options = withDefaults({
|
|
1891
|
+
...this.options,
|
|
1892
|
+
...options
|
|
1893
|
+
});
|
|
1894
|
+
if (userOptions.keySeparator !== undefined) {
|
|
1895
|
+
this.options.userDefinedKeySeparator = userOptions.keySeparator;
|
|
1920
1896
|
}
|
|
1921
|
-
if (
|
|
1922
|
-
this.options.userDefinedNsSeparator =
|
|
1897
|
+
if (userOptions.nsSeparator !== undefined) {
|
|
1898
|
+
this.options.userDefinedNsSeparator = userOptions.nsSeparator;
|
|
1923
1899
|
}
|
|
1924
|
-
if (
|
|
1925
|
-
this.
|
|
1900
|
+
if (!this.options.isClone) {
|
|
1901
|
+
this.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(userOptions)]);
|
|
1926
1902
|
}
|
|
1927
1903
|
const createClassOnDemand = ClassOrObject => {
|
|
1928
1904
|
if (!ClassOrObject) return null;
|
|
@@ -1930,15 +1906,10 @@
|
|
|
1930
1906
|
return ClassOrObject;
|
|
1931
1907
|
};
|
|
1932
1908
|
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
|
-
}
|
|
1909
|
+
this.logger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1910
|
+
const {
|
|
1911
|
+
logger
|
|
1912
|
+
} = this;
|
|
1942
1913
|
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1943
1914
|
this.pendingWarnings = undefined;
|
|
1944
1915
|
let formatter;
|
|
@@ -1947,27 +1918,26 @@
|
|
|
1947
1918
|
} else {
|
|
1948
1919
|
formatter = Formatter;
|
|
1949
1920
|
}
|
|
1950
|
-
const lu = new LanguageUtil(this.options);
|
|
1951
|
-
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1921
|
+
const lu = new LanguageUtil(this.options, logger);
|
|
1922
|
+
this.store = new ResourceStore(this.options.resources, this.options, logger);
|
|
1952
1923
|
const s = this.services;
|
|
1953
|
-
s.logger =
|
|
1924
|
+
s.logger = logger;
|
|
1954
1925
|
s.resourceStore = this.store;
|
|
1955
1926
|
s.languageUtils = lu;
|
|
1956
1927
|
s.pluralResolver = new PluralResolver(lu, {
|
|
1957
1928
|
prepend: this.options.pluralSeparator
|
|
1958
|
-
});
|
|
1929
|
+
}, logger);
|
|
1959
1930
|
if (formatter) {
|
|
1960
1931
|
s.formatter = createClassOnDemand(formatter);
|
|
1961
1932
|
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1962
1933
|
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1963
1934
|
}
|
|
1964
|
-
s.interpolator = new Interpolator(this.options);
|
|
1935
|
+
s.interpolator = new Interpolator(this.options, logger);
|
|
1965
1936
|
s.utils = {
|
|
1966
1937
|
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1967
1938
|
reloadResources: this.reloadResources.bind(this)
|
|
1968
1939
|
};
|
|
1969
1940
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1970
|
-
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1971
1941
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1972
1942
|
this.emit(event, ...args);
|
|
1973
1943
|
});
|
|
@@ -2107,8 +2077,7 @@
|
|
|
2107
2077
|
setModule('i18nFormat');
|
|
2108
2078
|
}
|
|
2109
2079
|
if (module.type === 'postProcessor') {
|
|
2110
|
-
|
|
2111
|
-
this.services.ownPostProcessors?.add(module.name);
|
|
2080
|
+
this.services.postProcessors[module.name] = module;
|
|
2112
2081
|
}
|
|
2113
2082
|
if (module.type === 'formatter') {
|
|
2114
2083
|
setModule('formatter');
|
|
@@ -2183,14 +2152,16 @@
|
|
|
2183
2152
|
};
|
|
2184
2153
|
const setLng = lngs => {
|
|
2185
2154
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2186
|
-
const
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
}
|
|
2155
|
+
const {
|
|
2156
|
+
supportedLngs
|
|
2157
|
+
} = this.options;
|
|
2158
|
+
if (lngs && !supportedLngs) {
|
|
2159
|
+
const accepted = [].concat(lngs).filter(c => !isRefusedLanguageCode(this, c));
|
|
2160
|
+
if (accepted.length < [].concat(lngs).length) lngs = accepted;
|
|
2193
2161
|
}
|
|
2162
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2163
|
+
let l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2164
|
+
if (supportedLngs && isRefusedLanguageCode(this, l)) l = this.services.languageUtils.getBestMatchFromCodes([]);
|
|
2194
2165
|
if (l) {
|
|
2195
2166
|
if (!this.language) {
|
|
2196
2167
|
setLngProps(l);
|
|
@@ -2305,7 +2276,7 @@
|
|
|
2305
2276
|
}
|
|
2306
2277
|
loadNamespaces(ns, callback) {
|
|
2307
2278
|
const deferred = defer();
|
|
2308
|
-
if (!this.options.ns) {
|
|
2279
|
+
if (!this.options.ns || !this.services.backendConnector) {
|
|
2309
2280
|
if (callback) callback();
|
|
2310
2281
|
return Promise.resolve();
|
|
2311
2282
|
}
|
|
@@ -2323,7 +2294,7 @@
|
|
|
2323
2294
|
const deferred = defer();
|
|
2324
2295
|
if (isString(lngs)) lngs = [lngs];
|
|
2325
2296
|
const preloaded = this.options.preload || [];
|
|
2326
|
-
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2297
|
+
const newLngs = lngs.filter(lng => !preloaded.includes(lng) && !isRefusedLanguageCode(this, lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2327
2298
|
if (!newLngs.length) {
|
|
2328
2299
|
if (callback) callback();
|
|
2329
2300
|
return Promise.resolve();
|
|
@@ -2372,16 +2343,15 @@
|
|
|
2372
2343
|
}
|
|
2373
2344
|
};
|
|
2374
2345
|
const clone = new I18n(mergedOptions);
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
}
|
|
2378
|
-
const membersToCopy = ['store', 'services', 'language'];
|
|
2346
|
+
clone.logger = options.debug !== undefined || options.prefix !== undefined ? this.logger.clone(options) : this.logger;
|
|
2347
|
+
const membersToCopy = ['store', 'language'];
|
|
2379
2348
|
membersToCopy.forEach(m => {
|
|
2380
2349
|
clone[m] = this[m];
|
|
2381
2350
|
});
|
|
2382
2351
|
clone.services = {
|
|
2383
2352
|
...this.services
|
|
2384
2353
|
};
|
|
2354
|
+
clone.services.postProcessors = Object.create(this.services.postProcessors);
|
|
2385
2355
|
clone.services.utils = {
|
|
2386
2356
|
...clone.services.utils,
|
|
2387
2357
|
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
@@ -2398,7 +2368,7 @@
|
|
|
2398
2368
|
return copied;
|
|
2399
2369
|
};
|
|
2400
2370
|
const clonedData = copyData(this.store.data);
|
|
2401
|
-
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2371
|
+
clone.store = new ResourceStore(clonedData, mergedOptions, this.logger);
|
|
2402
2372
|
clone.services.resourceStore = clone.store;
|
|
2403
2373
|
}
|
|
2404
2374
|
if (options.interpolation) {
|
|
@@ -2412,8 +2382,9 @@
|
|
|
2412
2382
|
...mergedOptions,
|
|
2413
2383
|
interpolation: mergedInterpolation
|
|
2414
2384
|
};
|
|
2415
|
-
clone.services.interpolator = new Interpolator(mergedForInterpolator);
|
|
2385
|
+
clone.services.interpolator = new Interpolator(mergedForInterpolator, this.logger);
|
|
2416
2386
|
}
|
|
2387
|
+
clone.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(options)]);
|
|
2417
2388
|
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2418
2389
|
clone.translator.on('*', (event, ...args) => {
|
|
2419
2390
|
clone.emit(event, ...args);
|