i18next 26.5.12 → 26.7.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/dist/cjs/i18next.js +152 -30
- package/dist/esm/i18next.js +152 -31
- package/dist/esm/package.json +1 -1
- package/dist/umd/i18next.js +152 -30
- package/dist/umd/i18next.min.js +1 -1
- package/i18next.js +152 -30
- package/i18next.min.js +1 -1
- package/index.d.mts +8 -0
- package/index.d.ts +45 -2
- package/package.json +10 -1
- package/typescript/options.d.ts +49 -4
- package/typescript/t.d.ts +126 -30
package/i18next.js
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
if (object == null) return '';
|
|
21
21
|
return String(object);
|
|
22
22
|
};
|
|
23
|
+
const isSafeLanguageCode = code => isString(code) && /^[A-Za-z0-9_@.-]{1,128}$/.test(code) && !code.includes('..') && !['__proto__', 'constructor', 'prototype'].includes(code);
|
|
24
|
+
const callbackApiOf = plugin => plugin?.callbackApi ?? plugin?.constructor?.callbackApi;
|
|
23
25
|
const copy = (a, s, t) => {
|
|
24
26
|
a.forEach(m => {
|
|
25
27
|
if (s[m]) t[m] = s[m];
|
|
@@ -223,6 +225,7 @@
|
|
|
223
225
|
this.logger = concreteLogger || consoleLogger;
|
|
224
226
|
this.options = options;
|
|
225
227
|
this.debug = options.debug;
|
|
228
|
+
this.seen = new Set();
|
|
226
229
|
}
|
|
227
230
|
log(...args) {
|
|
228
231
|
return this.forward(args, 'log', '', true);
|
|
@@ -236,6 +239,11 @@
|
|
|
236
239
|
deprecate(...args) {
|
|
237
240
|
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
|
|
238
241
|
}
|
|
242
|
+
once(key, lvl, ...args) {
|
|
243
|
+
if (!this.debug || this.seen.has(key) || this.seen.size >= 1000) return null;
|
|
244
|
+
this.seen.add(key);
|
|
245
|
+
return this[lvl](...args);
|
|
246
|
+
}
|
|
239
247
|
forward(args, lvl, prefix, debugOnly) {
|
|
240
248
|
if (debugOnly && !this.debug) return null;
|
|
241
249
|
args = args.map(a => isString(a) ? a.replace(/[\r\n\x00-\x1F\x7F]/g, ' ') : a);
|
|
@@ -270,13 +278,15 @@
|
|
|
270
278
|
});
|
|
271
279
|
return this;
|
|
272
280
|
}
|
|
273
|
-
off(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
281
|
+
off(events, listener) {
|
|
282
|
+
(typeof events === 'string' ? events.split(' ') : [events]).forEach(event => {
|
|
283
|
+
if (!this.observers[event]) return;
|
|
284
|
+
if (!listener) {
|
|
285
|
+
delete this.observers[event];
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.observers[event].delete(listener);
|
|
289
|
+
});
|
|
280
290
|
}
|
|
281
291
|
once(event, listener) {
|
|
282
292
|
const wrapper = (...args) => {
|
|
@@ -315,6 +325,7 @@
|
|
|
315
325
|
super();
|
|
316
326
|
this.data = data || {};
|
|
317
327
|
this.options = options;
|
|
328
|
+
this.logger = baseLogger.create('resourceStore');
|
|
318
329
|
if (this.options.keySeparator === undefined) {
|
|
319
330
|
this.options.keySeparator = '.';
|
|
320
331
|
}
|
|
@@ -333,11 +344,15 @@
|
|
|
333
344
|
this.options.ns.splice(index, 1);
|
|
334
345
|
}
|
|
335
346
|
}
|
|
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
|
+
}
|
|
336
350
|
getResource(lng, ns, key, options = {}) {
|
|
337
351
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
338
352
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
339
353
|
let path;
|
|
340
354
|
if (lng.includes('.')) {
|
|
355
|
+
this.deprecateDottedLng('getResource');
|
|
341
356
|
path = lng.split('.');
|
|
342
357
|
} else {
|
|
343
358
|
path = [lng, ns];
|
|
@@ -367,6 +382,7 @@
|
|
|
367
382
|
let path = [lng, ns];
|
|
368
383
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
369
384
|
if (lng.includes('.')) {
|
|
385
|
+
this.deprecateDottedLng('addResource');
|
|
370
386
|
path = lng.split('.');
|
|
371
387
|
value = ns;
|
|
372
388
|
ns = path[1];
|
|
@@ -391,6 +407,7 @@
|
|
|
391
407
|
}) {
|
|
392
408
|
let path = [lng, ns];
|
|
393
409
|
if (lng.includes('.')) {
|
|
410
|
+
this.deprecateDottedLng('addResourceBundle');
|
|
394
411
|
path = lng.split('.');
|
|
395
412
|
deep = resources;
|
|
396
413
|
resources = ns;
|
|
@@ -494,7 +511,7 @@
|
|
|
494
511
|
class Translator extends EventEmitter {
|
|
495
512
|
constructor(services, options = {}) {
|
|
496
513
|
super();
|
|
497
|
-
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
|
|
514
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', 'ownPostProcessors'], services, this);
|
|
498
515
|
this.options = options;
|
|
499
516
|
if (this.options.keySeparator === undefined) {
|
|
500
517
|
this.options.keySeparator = '.';
|
|
@@ -627,6 +644,7 @@
|
|
|
627
644
|
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
628
645
|
if (!this.options.returnedObjectHandler) {
|
|
629
646
|
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
|
647
|
+
this.logger.once('returnedObject', 'deprecate', "accessing an object without returnObjects returns a sentence about it today; from v27 t() returns the key, as for a missing key (returnedObjectHandler can keep today's text)");
|
|
630
648
|
}
|
|
631
649
|
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
632
650
|
...opt,
|
|
@@ -711,7 +729,7 @@
|
|
|
711
729
|
if (this.options.missingKeyHandler) {
|
|
712
730
|
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
713
731
|
} else if (this.backendConnector?.saveMissing) {
|
|
714
|
-
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
732
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt, undefined, this.utils?.hasLoadedNamespace);
|
|
715
733
|
}
|
|
716
734
|
this.emit('missingKey', l, namespace, k, res);
|
|
717
735
|
};
|
|
@@ -794,6 +812,11 @@
|
|
|
794
812
|
const postProcess = opt.postProcess || this.options.postProcess;
|
|
795
813
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
796
814
|
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
815
|
+
postProcessorNames.forEach(name => {
|
|
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
|
+
});
|
|
797
820
|
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
798
821
|
i18nResolved: {
|
|
799
822
|
...resolved,
|
|
@@ -969,7 +992,20 @@
|
|
|
969
992
|
}
|
|
970
993
|
return code;
|
|
971
994
|
}
|
|
972
|
-
|
|
995
|
+
const formatted = this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
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`);
|
|
973
1009
|
}
|
|
974
1010
|
isSupportedCode(code) {
|
|
975
1011
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1178,6 +1214,15 @@
|
|
|
1178
1214
|
maxReplaces,
|
|
1179
1215
|
alwaysFormat
|
|
1180
1216
|
} = 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
|
+
}
|
|
1181
1226
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1182
1227
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1183
1228
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
@@ -1212,6 +1257,7 @@
|
|
|
1212
1257
|
let match;
|
|
1213
1258
|
let value;
|
|
1214
1259
|
let replaces;
|
|
1260
|
+
const original = str;
|
|
1215
1261
|
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1216
1262
|
const handleFormat = key => {
|
|
1217
1263
|
if (!key.includes(this.formatSeparator)) {
|
|
@@ -1242,7 +1288,13 @@
|
|
|
1242
1288
|
safeValue: val => val
|
|
1243
1289
|
}, {
|
|
1244
1290
|
regex: this.regexp,
|
|
1245
|
-
safeValue: val =>
|
|
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
|
+
}
|
|
1246
1298
|
}];
|
|
1247
1299
|
const unescapedValues = [];
|
|
1248
1300
|
let shift = 0;
|
|
@@ -1264,6 +1316,9 @@
|
|
|
1264
1316
|
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1265
1317
|
value = '';
|
|
1266
1318
|
} else if (skipOnVariables) {
|
|
1319
|
+
if (this.logger.debug) {
|
|
1320
|
+
this.logger.once(`${original}\u0000${match[0]}`, 'warn', `missed to pass in variable ${matchedVar} for interpolating ${original}`);
|
|
1321
|
+
}
|
|
1267
1322
|
value = match[0];
|
|
1268
1323
|
continue;
|
|
1269
1324
|
} else {
|
|
@@ -1530,6 +1585,10 @@
|
|
|
1530
1585
|
this.state = {};
|
|
1531
1586
|
this.queue = [];
|
|
1532
1587
|
this.backend?.init?.(services, options.backend, options);
|
|
1588
|
+
this.callbackApi = callbackApiOf(this.backend);
|
|
1589
|
+
if (this.backend && this.callbackApi === undefined && (this.backend.read || this.backend.create)) {
|
|
1590
|
+
this.logger.deprecate('backend: set callbackApi to declare the calling convention (true: read(language, namespace, callback) and create(languages, namespace, key, fallbackValue, callback, options); false: both return a promise or a value); guessing it from the number of parameters is deprecated');
|
|
1591
|
+
}
|
|
1533
1592
|
}
|
|
1534
1593
|
queueLoad(languages, namespaces, options, callback) {
|
|
1535
1594
|
const toLoad = {};
|
|
@@ -1637,7 +1696,7 @@
|
|
|
1637
1696
|
callback(err, data);
|
|
1638
1697
|
};
|
|
1639
1698
|
const fc = this.backend[fcName].bind(this.backend);
|
|
1640
|
-
if (fc.length === 2) {
|
|
1699
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length === 2) {
|
|
1641
1700
|
try {
|
|
1642
1701
|
const r = fc(lng, ns);
|
|
1643
1702
|
if (r && typeof r.then === 'function') {
|
|
@@ -1676,32 +1735,38 @@
|
|
|
1676
1735
|
reload: true
|
|
1677
1736
|
}, callback);
|
|
1678
1737
|
}
|
|
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
|
+
}
|
|
1679
1742
|
loadOne(name, prefix = '') {
|
|
1680
1743
|
const s = name.split('|');
|
|
1681
1744
|
const lng = s[0];
|
|
1682
1745
|
const ns = s[1];
|
|
1746
|
+
this.noticeDevFallback(lng);
|
|
1683
1747
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1684
1748
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1685
1749
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1686
1750
|
this.loaded(name, err, data);
|
|
1687
1751
|
});
|
|
1688
1752
|
}
|
|
1689
|
-
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
|
|
1690
|
-
if (
|
|
1753
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}, hasLoadedNamespace = this.newestCloneHasLoadedNamespace || this.services?.utils?.hasLoadedNamespace) {
|
|
1754
|
+
if (hasLoadedNamespace && !hasLoadedNamespace(namespace)) {
|
|
1691
1755
|
this.logger.warn(`did not save key "${key}" as the namespace "${namespace}" was not yet loaded`, 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
|
|
1692
1756
|
return;
|
|
1693
1757
|
}
|
|
1694
1758
|
if (key === undefined || key === null || key === '') return;
|
|
1695
1759
|
if (this.backend?.create) {
|
|
1760
|
+
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1696
1761
|
const opts = {
|
|
1697
1762
|
...options,
|
|
1698
1763
|
isUpdate
|
|
1699
1764
|
};
|
|
1700
1765
|
const fc = this.backend.create.bind(this.backend);
|
|
1701
|
-
if (fc.length < 6) {
|
|
1766
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length < 6) {
|
|
1702
1767
|
try {
|
|
1703
1768
|
let r;
|
|
1704
|
-
if (fc.length === 5) {
|
|
1769
|
+
if (this.callbackApi === false || fc.length === 5) {
|
|
1705
1770
|
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1706
1771
|
} else {
|
|
1707
1772
|
r = fc(languages, namespace, key, fallbackValue);
|
|
@@ -1794,6 +1859,7 @@
|
|
|
1794
1859
|
};
|
|
1795
1860
|
|
|
1796
1861
|
const noop = () => {};
|
|
1862
|
+
let loggerOwner;
|
|
1797
1863
|
const bindMemberFunctions = inst => {
|
|
1798
1864
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1799
1865
|
mems.forEach(mem => {
|
|
@@ -1806,7 +1872,9 @@
|
|
|
1806
1872
|
constructor(options = {}, callback) {
|
|
1807
1873
|
super();
|
|
1808
1874
|
this.options = transformOptions(options);
|
|
1809
|
-
this.services = {
|
|
1875
|
+
this.services = {
|
|
1876
|
+
ownPostProcessors: new Set()
|
|
1877
|
+
};
|
|
1810
1878
|
this.logger = baseLogger;
|
|
1811
1879
|
this.modules = {
|
|
1812
1880
|
external: []
|
|
@@ -1835,6 +1903,7 @@
|
|
|
1835
1903
|
options.defaultNS = options.ns[0];
|
|
1836
1904
|
}
|
|
1837
1905
|
}
|
|
1906
|
+
const fallbackLngIsDefault = options.fallbackLng === undefined && this.options.fallbackLng === undefined;
|
|
1838
1907
|
const defOpts = get();
|
|
1839
1908
|
this.options = {
|
|
1840
1909
|
...defOpts,
|
|
@@ -1860,11 +1929,17 @@
|
|
|
1860
1929
|
return ClassOrObject;
|
|
1861
1930
|
};
|
|
1862
1931
|
if (!this.options.isClone) {
|
|
1932
|
+
if (loggerOwner && loggerOwner !== this && loggerOwner.modules.logger !== this.modules.logger) {
|
|
1933
|
+
baseLogger.deprecate('init: this init() replaces the logger another i18next instance set with use(), as the logger is shared in v26; from v27 each instance keeps its own logger');
|
|
1934
|
+
}
|
|
1935
|
+
loggerOwner = this.modules.logger ? this : undefined;
|
|
1863
1936
|
if (this.modules.logger) {
|
|
1864
1937
|
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1865
1938
|
} else {
|
|
1866
1939
|
baseLogger.init(null, this.options);
|
|
1867
1940
|
}
|
|
1941
|
+
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1942
|
+
this.pendingWarnings = undefined;
|
|
1868
1943
|
let formatter;
|
|
1869
1944
|
if (this.modules.formatter) {
|
|
1870
1945
|
formatter = this.modules.formatter;
|
|
@@ -1887,15 +1962,20 @@
|
|
|
1887
1962
|
}
|
|
1888
1963
|
s.interpolator = new Interpolator(this.options);
|
|
1889
1964
|
s.utils = {
|
|
1890
|
-
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1965
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1966
|
+
reloadResources: this.reloadResources.bind(this)
|
|
1891
1967
|
};
|
|
1892
1968
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1969
|
+
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1893
1970
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1894
1971
|
this.emit(event, ...args);
|
|
1895
1972
|
});
|
|
1896
1973
|
if (this.modules.languageDetector) {
|
|
1897
1974
|
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1898
1975
|
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1976
|
+
if (s.languageDetector.async && callbackApiOf(s.languageDetector) === undefined) {
|
|
1977
|
+
this.logger.deprecate('languageDetector: set callbackApi to declare how the async detect() answers (true: detect(callback); false: detect() returns a promise); guessing it from the number of parameters is deprecated');
|
|
1978
|
+
}
|
|
1899
1979
|
}
|
|
1900
1980
|
if (this.modules.i18nFormat) {
|
|
1901
1981
|
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
@@ -1991,7 +2071,7 @@
|
|
|
1991
2071
|
callback = ns;
|
|
1992
2072
|
ns = undefined;
|
|
1993
2073
|
}
|
|
1994
|
-
if (!lngs) lngs = this.languages;
|
|
2074
|
+
if (!lngs) lngs = this.languages || [];
|
|
1995
2075
|
if (!ns) ns = this.options.ns;
|
|
1996
2076
|
if (!callback) callback = noop;
|
|
1997
2077
|
this.services.backendConnector.reload(lngs, ns, err => {
|
|
@@ -2003,29 +2083,59 @@
|
|
|
2003
2083
|
use(module) {
|
|
2004
2084
|
if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
|
|
2005
2085
|
if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
|
|
2086
|
+
const setModule = type => {
|
|
2087
|
+
if (this.modules[type] && this.modules[type] !== module) {
|
|
2088
|
+
const msg = `use: this ${type} replaces the ${type} passed to an earlier use() call; only one ${type} is used per instance`;
|
|
2089
|
+
if (this.isInitializing || this.isInitialized) this.logger.warn(msg);else (this.pendingWarnings = this.pendingWarnings || []).push(msg);
|
|
2090
|
+
}
|
|
2091
|
+
this.modules[type] = module;
|
|
2092
|
+
};
|
|
2006
2093
|
if (module.type === 'backend') {
|
|
2007
|
-
|
|
2094
|
+
setModule('backend');
|
|
2008
2095
|
}
|
|
2009
2096
|
if (module.type === 'logger' || module.log && module.warn && module.error) {
|
|
2010
2097
|
this.modules.logger = module;
|
|
2011
2098
|
}
|
|
2012
2099
|
if (module.type === 'languageDetector') {
|
|
2013
|
-
|
|
2100
|
+
setModule('languageDetector');
|
|
2014
2101
|
}
|
|
2015
2102
|
if (module.type === 'i18nFormat') {
|
|
2016
|
-
|
|
2103
|
+
setModule('i18nFormat');
|
|
2017
2104
|
}
|
|
2018
2105
|
if (module.type === 'postProcessor') {
|
|
2019
2106
|
postProcessor.addPostProcessor(module);
|
|
2107
|
+
this.services.ownPostProcessors?.add(module.name);
|
|
2020
2108
|
}
|
|
2021
2109
|
if (module.type === 'formatter') {
|
|
2022
|
-
|
|
2110
|
+
setModule('formatter');
|
|
2023
2111
|
}
|
|
2024
2112
|
if (module.type === '3rdParty') {
|
|
2025
2113
|
this.modules.external.push(module);
|
|
2026
2114
|
}
|
|
2027
2115
|
return this;
|
|
2028
2116
|
}
|
|
2117
|
+
hydrate(resources, lng) {
|
|
2118
|
+
if (this.options.isClone) return this;
|
|
2119
|
+
if (!this.services.resourceStore) {
|
|
2120
|
+
this.logger.warn('hydrate: call init() first, the instance has no resource store yet');
|
|
2121
|
+
return this;
|
|
2122
|
+
}
|
|
2123
|
+
if (resources && !this.initializedStoreOnce) {
|
|
2124
|
+
this.services.resourceStore.data = resources;
|
|
2125
|
+
Object.values(resources).forEach(lngResources => {
|
|
2126
|
+
Object.keys(lngResources).forEach(ns => {
|
|
2127
|
+
if (!this.options.ns.includes(ns)) this.options.ns.push(ns);
|
|
2128
|
+
});
|
|
2129
|
+
});
|
|
2130
|
+
this.initializedStoreOnce = true;
|
|
2131
|
+
this.isInitialized = true;
|
|
2132
|
+
}
|
|
2133
|
+
if (lng && !this.initializedLanguageOnce) {
|
|
2134
|
+
this.changeLanguage(lng);
|
|
2135
|
+
this.initializedLanguageOnce = true;
|
|
2136
|
+
}
|
|
2137
|
+
return this;
|
|
2138
|
+
}
|
|
2029
2139
|
setResolvedLanguage(l) {
|
|
2030
2140
|
if (!l || !this.languages) return;
|
|
2031
2141
|
if (['cimode', 'dev'].includes(l)) return;
|
|
@@ -2071,6 +2181,12 @@
|
|
|
2071
2181
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2072
2182
|
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2073
2183
|
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2184
|
+
if (l && this.logger.debug && !isSafeLanguageCode(l)) {
|
|
2185
|
+
const configured = [].concat(this.options.supportedLngs || [], this.services.languageUtils.getFallbackCodes(this.options.fallbackLng, l), this.options.preload || []);
|
|
2186
|
+
if (!configured.includes(l) && !Object.prototype.hasOwnProperty.call(this.store.data, l)) {
|
|
2187
|
+
this.logger.once(`lng:${l}`, 'deprecate', `changeLanguage: the language code '${l.slice(0, 64)}' comes from lng, changeLanguage() or the language detector and is not configured; from v27 such a code is refused unless it has only letters, digits, '-', '_', '@' and '.' (no '..'), at most 128 characters. Set supportedLngs, or check the code before passing it on`);
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2074
2190
|
if (l) {
|
|
2075
2191
|
if (!this.language) {
|
|
2076
2192
|
setLngProps(l);
|
|
@@ -2085,7 +2201,8 @@
|
|
|
2085
2201
|
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
2086
2202
|
setLng(this.services.languageDetector.detect());
|
|
2087
2203
|
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
2088
|
-
|
|
2204
|
+
const callbackApi = callbackApiOf(this.services.languageDetector);
|
|
2205
|
+
if (callbackApi === false || callbackApi === undefined && this.services.languageDetector.detect.length === 0) {
|
|
2089
2206
|
this.services.languageDetector.detect().then(setLng);
|
|
2090
2207
|
} else {
|
|
2091
2208
|
this.services.languageDetector.detect(setLng);
|
|
@@ -2140,7 +2257,11 @@
|
|
|
2140
2257
|
return fixedT;
|
|
2141
2258
|
}
|
|
2142
2259
|
t(...args) {
|
|
2143
|
-
|
|
2260
|
+
if (!this.translator) {
|
|
2261
|
+
this.logger.warn('t: called before init(); returning an empty string');
|
|
2262
|
+
return '';
|
|
2263
|
+
}
|
|
2264
|
+
return this.translator.translate(...args);
|
|
2144
2265
|
}
|
|
2145
2266
|
exists(...args) {
|
|
2146
2267
|
return this.translator?.exists(...args);
|
|
@@ -2212,7 +2333,7 @@
|
|
|
2212
2333
|
}
|
|
2213
2334
|
dir(lng) {
|
|
2214
2335
|
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2215
|
-
if (!lng) return '
|
|
2336
|
+
if (!lng) return 'ltr';
|
|
2216
2337
|
try {
|
|
2217
2338
|
const l = new Intl.Locale(lng);
|
|
2218
2339
|
if (l && l.getTextInfo) {
|
|
@@ -2258,7 +2379,9 @@
|
|
|
2258
2379
|
...this.services
|
|
2259
2380
|
};
|
|
2260
2381
|
clone.services.utils = {
|
|
2261
|
-
|
|
2382
|
+
...clone.services.utils,
|
|
2383
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
2384
|
+
reloadResources: clone.reloadResources.bind(clone)
|
|
2262
2385
|
};
|
|
2263
2386
|
if (forkResourceStore) {
|
|
2264
2387
|
const copyData = obj => {
|
|
@@ -2293,9 +2416,7 @@
|
|
|
2293
2416
|
});
|
|
2294
2417
|
clone.init(mergedOptions, callback);
|
|
2295
2418
|
clone.translator.options = mergedOptions;
|
|
2296
|
-
clone.translator.backendConnector.
|
|
2297
|
-
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2298
|
-
};
|
|
2419
|
+
clone.translator.backendConnector.newestCloneHasLoadedNamespace = clone.hasLoadedNamespace.bind(clone);
|
|
2299
2420
|
return clone;
|
|
2300
2421
|
}
|
|
2301
2422
|
toJSON() {
|
|
@@ -2311,6 +2432,7 @@
|
|
|
2311
2432
|
const instance = I18n.createInstance();
|
|
2312
2433
|
|
|
2313
2434
|
instance.keyFromSelector = keysFromSelector;
|
|
2435
|
+
instance.isSafeLanguageCode = isSafeLanguageCode;
|
|
2314
2436
|
|
|
2315
2437
|
return instance;
|
|
2316
2438
|
|