i18next 26.7.1 → 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 -2
- package/dist/cjs/i18next.js +174 -202
- package/dist/esm/i18next.js +174 -202
- package/dist/esm/package.json +1 -1
- package/dist/umd/i18next.js +174 -202
- package/dist/umd/i18next.min.js +1 -1
- package/i18next.js +174 -202
- package/i18next.min.js +1 -1
- package/index.d.mts +5 -4
- package/index.d.ts +11 -7
- package/package.json +24 -25
- 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,34 +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 v27;
|
|
1001
|
-
try {
|
|
1002
|
-
v27 = Intl.getCanonicalLocales(code)[0];
|
|
1003
|
-
} catch (e) {
|
|
1004
|
-
return;
|
|
1005
|
-
}
|
|
1006
|
-
if (this.options.lowerCaseLng) v27 = v27.toLowerCase();
|
|
1007
|
-
if (v27 === formatted) return;
|
|
1008
|
-
this.logger.once(code, 'deprecate', `language code '${code}' is used as '${formatted}'; i18next v27 canonicalizes it to '${v27}', use '${v27}' in lng, fallbackLng, supportedLngs and the resources`);
|
|
979
|
+
return this.options.lowerCaseLng || this.options.cleanCode && !hasSubtags ? code.toLowerCase() : code;
|
|
1009
980
|
}
|
|
1010
981
|
isSupportedCode(code) {
|
|
1011
982
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1116,10 +1087,10 @@
|
|
|
1116
1087
|
})
|
|
1117
1088
|
};
|
|
1118
1089
|
class PluralResolver {
|
|
1119
|
-
constructor(languageUtils, options = {}) {
|
|
1090
|
+
constructor(languageUtils, options = {}, logger = baseLogger) {
|
|
1120
1091
|
this.languageUtils = languageUtils;
|
|
1121
1092
|
this.options = options;
|
|
1122
|
-
this.logger =
|
|
1093
|
+
this.logger = logger.create('pluralResolver');
|
|
1123
1094
|
this.pluralRulesCache = {};
|
|
1124
1095
|
}
|
|
1125
1096
|
clearCache() {
|
|
@@ -1185,8 +1156,8 @@
|
|
|
1185
1156
|
return path;
|
|
1186
1157
|
};
|
|
1187
1158
|
class Interpolator {
|
|
1188
|
-
constructor(options = {}) {
|
|
1189
|
-
this.logger =
|
|
1159
|
+
constructor(options = {}, logger = baseLogger) {
|
|
1160
|
+
this.logger = logger.create('interpolator');
|
|
1190
1161
|
this.options = options;
|
|
1191
1162
|
this.format = options?.interpolation?.format || (value => value);
|
|
1192
1163
|
this.init(options);
|
|
@@ -1200,39 +1171,26 @@
|
|
|
1200
1171
|
escapeValue,
|
|
1201
1172
|
useRawValueToEscape,
|
|
1202
1173
|
prefix,
|
|
1203
|
-
prefixEscaped,
|
|
1204
1174
|
suffix,
|
|
1205
|
-
suffixEscaped,
|
|
1206
1175
|
formatSeparator,
|
|
1207
1176
|
unescapeSuffix,
|
|
1208
1177
|
unescapePrefix,
|
|
1209
1178
|
nestingPrefix,
|
|
1210
|
-
nestingPrefixEscaped,
|
|
1211
1179
|
nestingSuffix,
|
|
1212
|
-
nestingSuffixEscaped,
|
|
1213
1180
|
nestingOptionsSeparator,
|
|
1214
1181
|
maxReplaces,
|
|
1215
1182
|
alwaysFormat
|
|
1216
1183
|
} = options.interpolation;
|
|
1217
|
-
if (prefixEscaped || suffixEscaped || nestingPrefixEscaped || nestingSuffixEscaped) {
|
|
1218
|
-
const names = Object.entries({
|
|
1219
|
-
prefixEscaped,
|
|
1220
|
-
suffixEscaped,
|
|
1221
|
-
nestingPrefixEscaped,
|
|
1222
|
-
nestingSuffixEscaped
|
|
1223
|
-
}).filter(([, v]) => v).map(([k]) => k);
|
|
1224
|
-
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`);
|
|
1225
|
-
}
|
|
1226
1184
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1227
1185
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1228
1186
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
1229
|
-
this.prefix = prefix ? regexEscape(prefix) :
|
|
1230
|
-
this.suffix = suffix ? regexEscape(suffix) :
|
|
1187
|
+
this.prefix = prefix ? regexEscape(prefix) : '{{';
|
|
1188
|
+
this.suffix = suffix ? regexEscape(suffix) : '}}';
|
|
1231
1189
|
this.formatSeparator = formatSeparator || ',';
|
|
1232
1190
|
this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix ? regexEscape(unescapePrefix) : '-';
|
|
1233
1191
|
this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix ? regexEscape(unescapeSuffix) : '';
|
|
1234
|
-
this.nestingPrefix =
|
|
1235
|
-
this.nestingSuffix =
|
|
1192
|
+
this.nestingPrefix = regexEscape(nestingPrefix || '$t(');
|
|
1193
|
+
this.nestingSuffix = regexEscape(nestingSuffix || ')');
|
|
1236
1194
|
this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
|
|
1237
1195
|
this.maxReplaces = maxReplaces || 1000;
|
|
1238
1196
|
this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
|
|
@@ -1288,13 +1246,7 @@
|
|
|
1288
1246
|
safeValue: val => val
|
|
1289
1247
|
}, {
|
|
1290
1248
|
regex: this.regexp,
|
|
1291
|
-
safeValue: val =>
|
|
1292
|
-
if (!this.escapeValue) return val;
|
|
1293
|
-
if (this.escape === escape && this.options?.interpolation?.escapeValue !== false && isString(val) && val.includes('/')) {
|
|
1294
|
-
this.logger.once('/', 'deprecate', "escapeValue renders '/' in a value as '/' today; from v27 the default escape leaves '/' as it is");
|
|
1295
|
-
}
|
|
1296
|
-
return this.escape(val);
|
|
1297
|
-
}
|
|
1249
|
+
safeValue: val => this.escapeValue ? this.escape(val) : val
|
|
1298
1250
|
}];
|
|
1299
1251
|
const unescapedValues = [];
|
|
1300
1252
|
let shift = 0;
|
|
@@ -1480,6 +1432,7 @@
|
|
|
1480
1432
|
init(services, options = {
|
|
1481
1433
|
interpolation: {}
|
|
1482
1434
|
}) {
|
|
1435
|
+
if (services?.logger?.create) this.logger = services.logger.create('formatter');
|
|
1483
1436
|
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
|
1484
1437
|
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1485
1438
|
this.formats = {
|
|
@@ -1576,7 +1529,7 @@
|
|
|
1576
1529
|
this.services = services;
|
|
1577
1530
|
this.languageUtils = services.languageUtils;
|
|
1578
1531
|
this.options = options;
|
|
1579
|
-
this.logger = baseLogger.create('backendConnector');
|
|
1532
|
+
this.logger = (services.logger || baseLogger).create('backendConnector');
|
|
1580
1533
|
this.waitingReads = [];
|
|
1581
1534
|
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1582
1535
|
this.readingCalls = 0;
|
|
@@ -1735,15 +1688,10 @@
|
|
|
1735
1688
|
reload: true
|
|
1736
1689
|
}, callback);
|
|
1737
1690
|
}
|
|
1738
|
-
noticeDevFallback(lng) {
|
|
1739
|
-
if (lng !== 'dev' || !this.devIsDefaultFallback) return;
|
|
1740
|
-
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");
|
|
1741
|
-
}
|
|
1742
1691
|
loadOne(name, prefix = '') {
|
|
1743
1692
|
const s = name.split('|');
|
|
1744
1693
|
const lng = s[0];
|
|
1745
1694
|
const ns = s[1];
|
|
1746
|
-
this.noticeDevFallback(lng);
|
|
1747
1695
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1748
1696
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1749
1697
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
@@ -1757,7 +1705,6 @@
|
|
|
1757
1705
|
}
|
|
1758
1706
|
if (key === undefined || key === null || key === '') return;
|
|
1759
1707
|
if (this.backend?.create) {
|
|
1760
|
-
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1761
1708
|
const opts = {
|
|
1762
1709
|
...options,
|
|
1763
1710
|
isUpdate
|
|
@@ -1793,7 +1740,7 @@
|
|
|
1793
1740
|
initAsync: true,
|
|
1794
1741
|
ns: ['translation'],
|
|
1795
1742
|
defaultNS: ['translation'],
|
|
1796
|
-
fallbackLng:
|
|
1743
|
+
fallbackLng: false,
|
|
1797
1744
|
fallbackNS: false,
|
|
1798
1745
|
supportedLngs: false,
|
|
1799
1746
|
nonExplicitSupportedLngs: false,
|
|
@@ -1814,7 +1761,7 @@
|
|
|
1814
1761
|
postProcess: false,
|
|
1815
1762
|
postProcessPassResolved: false,
|
|
1816
1763
|
returnNull: false,
|
|
1817
|
-
returnEmptyString:
|
|
1764
|
+
returnEmptyString: false,
|
|
1818
1765
|
returnObjects: false,
|
|
1819
1766
|
joinArrays: false,
|
|
1820
1767
|
returnedObjectHandler: false,
|
|
@@ -1859,7 +1806,38 @@
|
|
|
1859
1806
|
};
|
|
1860
1807
|
|
|
1861
1808
|
const noop = () => {};
|
|
1862
|
-
|
|
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
|
+
};
|
|
1863
1841
|
const bindMemberFunctions = inst => {
|
|
1864
1842
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1865
1843
|
mems.forEach(mem => {
|
|
@@ -1871,17 +1849,18 @@
|
|
|
1871
1849
|
class I18n extends EventEmitter {
|
|
1872
1850
|
constructor(options = {}, callback) {
|
|
1873
1851
|
super();
|
|
1874
|
-
this.
|
|
1852
|
+
this.userOptions = transformOptions(options);
|
|
1853
|
+
this.options = withDefaults(this.userOptions);
|
|
1875
1854
|
this.services = {
|
|
1876
|
-
|
|
1855
|
+
postProcessors: Object.create(null)
|
|
1877
1856
|
};
|
|
1878
|
-
this.logger =
|
|
1857
|
+
this.logger = new Logger(null, this.options);
|
|
1879
1858
|
this.modules = {
|
|
1880
1859
|
external: []
|
|
1881
1860
|
};
|
|
1882
1861
|
bindMemberFunctions(this);
|
|
1883
1862
|
if (callback && !this.isInitialized && !options.isClone) {
|
|
1884
|
-
if (!this.
|
|
1863
|
+
if (!this.userOptions.initAsync) {
|
|
1885
1864
|
this.init(options, callback);
|
|
1886
1865
|
return this;
|
|
1887
1866
|
}
|
|
@@ -1903,25 +1882,23 @@
|
|
|
1903
1882
|
options.defaultNS = options.ns[0];
|
|
1904
1883
|
}
|
|
1905
1884
|
}
|
|
1906
|
-
|
|
1907
|
-
const
|
|
1908
|
-
|
|
1909
|
-
...
|
|
1910
|
-
...this.options,
|
|
1911
|
-
...transformOptions(options)
|
|
1912
|
-
};
|
|
1913
|
-
this.options.interpolation = {
|
|
1914
|
-
...defOpts.interpolation,
|
|
1915
|
-
...this.options.interpolation
|
|
1885
|
+
transformOptions(options);
|
|
1886
|
+
const userOptions = {
|
|
1887
|
+
...this.userOptions,
|
|
1888
|
+
...options
|
|
1916
1889
|
};
|
|
1917
|
-
|
|
1918
|
-
this.options
|
|
1890
|
+
this.options = withDefaults({
|
|
1891
|
+
...this.options,
|
|
1892
|
+
...options
|
|
1893
|
+
});
|
|
1894
|
+
if (userOptions.keySeparator !== undefined) {
|
|
1895
|
+
this.options.userDefinedKeySeparator = userOptions.keySeparator;
|
|
1919
1896
|
}
|
|
1920
|
-
if (
|
|
1921
|
-
this.options.userDefinedNsSeparator =
|
|
1897
|
+
if (userOptions.nsSeparator !== undefined) {
|
|
1898
|
+
this.options.userDefinedNsSeparator = userOptions.nsSeparator;
|
|
1922
1899
|
}
|
|
1923
|
-
if (
|
|
1924
|
-
this.
|
|
1900
|
+
if (!this.options.isClone) {
|
|
1901
|
+
this.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(userOptions)]);
|
|
1925
1902
|
}
|
|
1926
1903
|
const createClassOnDemand = ClassOrObject => {
|
|
1927
1904
|
if (!ClassOrObject) return null;
|
|
@@ -1929,15 +1906,10 @@
|
|
|
1929
1906
|
return ClassOrObject;
|
|
1930
1907
|
};
|
|
1931
1908
|
if (!this.options.isClone) {
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
if (this.modules.logger) {
|
|
1937
|
-
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1938
|
-
} else {
|
|
1939
|
-
baseLogger.init(null, this.options);
|
|
1940
|
-
}
|
|
1909
|
+
this.logger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1910
|
+
const {
|
|
1911
|
+
logger
|
|
1912
|
+
} = this;
|
|
1941
1913
|
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1942
1914
|
this.pendingWarnings = undefined;
|
|
1943
1915
|
let formatter;
|
|
@@ -1946,27 +1918,26 @@
|
|
|
1946
1918
|
} else {
|
|
1947
1919
|
formatter = Formatter;
|
|
1948
1920
|
}
|
|
1949
|
-
const lu = new LanguageUtil(this.options);
|
|
1950
|
-
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);
|
|
1951
1923
|
const s = this.services;
|
|
1952
|
-
s.logger =
|
|
1924
|
+
s.logger = logger;
|
|
1953
1925
|
s.resourceStore = this.store;
|
|
1954
1926
|
s.languageUtils = lu;
|
|
1955
1927
|
s.pluralResolver = new PluralResolver(lu, {
|
|
1956
1928
|
prepend: this.options.pluralSeparator
|
|
1957
|
-
});
|
|
1929
|
+
}, logger);
|
|
1958
1930
|
if (formatter) {
|
|
1959
1931
|
s.formatter = createClassOnDemand(formatter);
|
|
1960
1932
|
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1961
1933
|
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1962
1934
|
}
|
|
1963
|
-
s.interpolator = new Interpolator(this.options);
|
|
1935
|
+
s.interpolator = new Interpolator(this.options, logger);
|
|
1964
1936
|
s.utils = {
|
|
1965
1937
|
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1966
1938
|
reloadResources: this.reloadResources.bind(this)
|
|
1967
1939
|
};
|
|
1968
1940
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1969
|
-
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1970
1941
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1971
1942
|
this.emit(event, ...args);
|
|
1972
1943
|
});
|
|
@@ -2106,8 +2077,7 @@
|
|
|
2106
2077
|
setModule('i18nFormat');
|
|
2107
2078
|
}
|
|
2108
2079
|
if (module.type === 'postProcessor') {
|
|
2109
|
-
|
|
2110
|
-
this.services.ownPostProcessors?.add(module.name);
|
|
2080
|
+
this.services.postProcessors[module.name] = module;
|
|
2111
2081
|
}
|
|
2112
2082
|
if (module.type === 'formatter') {
|
|
2113
2083
|
setModule('formatter');
|
|
@@ -2182,14 +2152,16 @@
|
|
|
2182
2152
|
};
|
|
2183
2153
|
const setLng = lngs => {
|
|
2184
2154
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2185
|
-
const
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
}
|
|
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;
|
|
2192
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([]);
|
|
2193
2165
|
if (l) {
|
|
2194
2166
|
if (!this.language) {
|
|
2195
2167
|
setLngProps(l);
|
|
@@ -2304,7 +2276,7 @@
|
|
|
2304
2276
|
}
|
|
2305
2277
|
loadNamespaces(ns, callback) {
|
|
2306
2278
|
const deferred = defer();
|
|
2307
|
-
if (!this.options.ns) {
|
|
2279
|
+
if (!this.options.ns || !this.services.backendConnector) {
|
|
2308
2280
|
if (callback) callback();
|
|
2309
2281
|
return Promise.resolve();
|
|
2310
2282
|
}
|
|
@@ -2322,7 +2294,7 @@
|
|
|
2322
2294
|
const deferred = defer();
|
|
2323
2295
|
if (isString(lngs)) lngs = [lngs];
|
|
2324
2296
|
const preloaded = this.options.preload || [];
|
|
2325
|
-
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));
|
|
2326
2298
|
if (!newLngs.length) {
|
|
2327
2299
|
if (callback) callback();
|
|
2328
2300
|
return Promise.resolve();
|
|
@@ -2371,16 +2343,15 @@
|
|
|
2371
2343
|
}
|
|
2372
2344
|
};
|
|
2373
2345
|
const clone = new I18n(mergedOptions);
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
}
|
|
2377
|
-
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'];
|
|
2378
2348
|
membersToCopy.forEach(m => {
|
|
2379
2349
|
clone[m] = this[m];
|
|
2380
2350
|
});
|
|
2381
2351
|
clone.services = {
|
|
2382
2352
|
...this.services
|
|
2383
2353
|
};
|
|
2354
|
+
clone.services.postProcessors = Object.create(this.services.postProcessors);
|
|
2384
2355
|
clone.services.utils = {
|
|
2385
2356
|
...clone.services.utils,
|
|
2386
2357
|
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
@@ -2397,7 +2368,7 @@
|
|
|
2397
2368
|
return copied;
|
|
2398
2369
|
};
|
|
2399
2370
|
const clonedData = copyData(this.store.data);
|
|
2400
|
-
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2371
|
+
clone.store = new ResourceStore(clonedData, mergedOptions, this.logger);
|
|
2401
2372
|
clone.services.resourceStore = clone.store;
|
|
2402
2373
|
}
|
|
2403
2374
|
if (options.interpolation) {
|
|
@@ -2411,8 +2382,9 @@
|
|
|
2411
2382
|
...mergedOptions,
|
|
2412
2383
|
interpolation: mergedInterpolation
|
|
2413
2384
|
};
|
|
2414
|
-
clone.services.interpolator = new Interpolator(mergedForInterpolator);
|
|
2385
|
+
clone.services.interpolator = new Interpolator(mergedForInterpolator, this.logger);
|
|
2415
2386
|
}
|
|
2387
|
+
clone.configuredLngs = new Set([...(this.configuredLngs || []), ...configuredLanguageCodes(options)]);
|
|
2416
2388
|
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2417
2389
|
clone.translator.on('*', (event, ...args) => {
|
|
2418
2390
|
clone.emit(event, ...args);
|