i18next 26.6.0 → 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 +40 -4
- package/typescript/t.d.ts +54 -14
package/dist/cjs/i18next.js
CHANGED
|
@@ -16,6 +16,8 @@ const makeString = object => {
|
|
|
16
16
|
if (object == null) return '';
|
|
17
17
|
return String(object);
|
|
18
18
|
};
|
|
19
|
+
const isSafeLanguageCode = code => isString(code) && /^[A-Za-z0-9_@.-]{1,128}$/.test(code) && !code.includes('..') && !['__proto__', 'constructor', 'prototype'].includes(code);
|
|
20
|
+
const callbackApiOf = plugin => plugin?.callbackApi ?? plugin?.constructor?.callbackApi;
|
|
19
21
|
const copy = (a, s, t) => {
|
|
20
22
|
a.forEach(m => {
|
|
21
23
|
if (s[m]) t[m] = s[m];
|
|
@@ -219,6 +221,7 @@ class Logger {
|
|
|
219
221
|
this.logger = concreteLogger || consoleLogger;
|
|
220
222
|
this.options = options;
|
|
221
223
|
this.debug = options.debug;
|
|
224
|
+
this.seen = new Set();
|
|
222
225
|
}
|
|
223
226
|
log(...args) {
|
|
224
227
|
return this.forward(args, 'log', '', true);
|
|
@@ -232,6 +235,11 @@ class Logger {
|
|
|
232
235
|
deprecate(...args) {
|
|
233
236
|
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
|
|
234
237
|
}
|
|
238
|
+
once(key, lvl, ...args) {
|
|
239
|
+
if (!this.debug || this.seen.has(key) || this.seen.size >= 1000) return null;
|
|
240
|
+
this.seen.add(key);
|
|
241
|
+
return this[lvl](...args);
|
|
242
|
+
}
|
|
235
243
|
forward(args, lvl, prefix, debugOnly) {
|
|
236
244
|
if (debugOnly && !this.debug) return null;
|
|
237
245
|
args = args.map(a => isString(a) ? a.replace(/[\r\n\x00-\x1F\x7F]/g, ' ') : a);
|
|
@@ -266,13 +274,15 @@ class EventEmitter {
|
|
|
266
274
|
});
|
|
267
275
|
return this;
|
|
268
276
|
}
|
|
269
|
-
off(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
277
|
+
off(events, listener) {
|
|
278
|
+
(typeof events === 'string' ? events.split(' ') : [events]).forEach(event => {
|
|
279
|
+
if (!this.observers[event]) return;
|
|
280
|
+
if (!listener) {
|
|
281
|
+
delete this.observers[event];
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.observers[event].delete(listener);
|
|
285
|
+
});
|
|
276
286
|
}
|
|
277
287
|
once(event, listener) {
|
|
278
288
|
const wrapper = (...args) => {
|
|
@@ -311,6 +321,7 @@ class ResourceStore extends EventEmitter {
|
|
|
311
321
|
super();
|
|
312
322
|
this.data = data || {};
|
|
313
323
|
this.options = options;
|
|
324
|
+
this.logger = baseLogger.create('resourceStore');
|
|
314
325
|
if (this.options.keySeparator === undefined) {
|
|
315
326
|
this.options.keySeparator = '.';
|
|
316
327
|
}
|
|
@@ -329,11 +340,15 @@ class ResourceStore extends EventEmitter {
|
|
|
329
340
|
this.options.ns.splice(index, 1);
|
|
330
341
|
}
|
|
331
342
|
}
|
|
343
|
+
deprecateDottedLng(method) {
|
|
344
|
+
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`);
|
|
345
|
+
}
|
|
332
346
|
getResource(lng, ns, key, options = {}) {
|
|
333
347
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
334
348
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
335
349
|
let path;
|
|
336
350
|
if (lng.includes('.')) {
|
|
351
|
+
this.deprecateDottedLng('getResource');
|
|
337
352
|
path = lng.split('.');
|
|
338
353
|
} else {
|
|
339
354
|
path = [lng, ns];
|
|
@@ -363,6 +378,7 @@ class ResourceStore extends EventEmitter {
|
|
|
363
378
|
let path = [lng, ns];
|
|
364
379
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
365
380
|
if (lng.includes('.')) {
|
|
381
|
+
this.deprecateDottedLng('addResource');
|
|
366
382
|
path = lng.split('.');
|
|
367
383
|
value = ns;
|
|
368
384
|
ns = path[1];
|
|
@@ -387,6 +403,7 @@ class ResourceStore extends EventEmitter {
|
|
|
387
403
|
}) {
|
|
388
404
|
let path = [lng, ns];
|
|
389
405
|
if (lng.includes('.')) {
|
|
406
|
+
this.deprecateDottedLng('addResourceBundle');
|
|
390
407
|
path = lng.split('.');
|
|
391
408
|
deep = resources;
|
|
392
409
|
resources = ns;
|
|
@@ -490,7 +507,7 @@ const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' &
|
|
|
490
507
|
class Translator extends EventEmitter {
|
|
491
508
|
constructor(services, options = {}) {
|
|
492
509
|
super();
|
|
493
|
-
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
|
|
510
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', 'ownPostProcessors'], services, this);
|
|
494
511
|
this.options = options;
|
|
495
512
|
if (this.options.keySeparator === undefined) {
|
|
496
513
|
this.options.keySeparator = '.';
|
|
@@ -623,6 +640,7 @@ class Translator extends EventEmitter {
|
|
|
623
640
|
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
624
641
|
if (!this.options.returnedObjectHandler) {
|
|
625
642
|
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
|
643
|
+
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)");
|
|
626
644
|
}
|
|
627
645
|
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
628
646
|
...opt,
|
|
@@ -707,7 +725,7 @@ class Translator extends EventEmitter {
|
|
|
707
725
|
if (this.options.missingKeyHandler) {
|
|
708
726
|
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
709
727
|
} else if (this.backendConnector?.saveMissing) {
|
|
710
|
-
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
728
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt, undefined, this.utils?.hasLoadedNamespace);
|
|
711
729
|
}
|
|
712
730
|
this.emit('missingKey', l, namespace, k, res);
|
|
713
731
|
};
|
|
@@ -790,6 +808,11 @@ class Translator extends EventEmitter {
|
|
|
790
808
|
const postProcess = opt.postProcess || this.options.postProcess;
|
|
791
809
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
792
810
|
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
811
|
+
postProcessorNames.forEach(name => {
|
|
812
|
+
if (this.logger.debug && postProcessor.processors[name] && this.ownPostProcessors && !this.ownPostProcessors.has(name)) {
|
|
813
|
+
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`);
|
|
814
|
+
}
|
|
815
|
+
});
|
|
793
816
|
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
794
817
|
i18nResolved: {
|
|
795
818
|
...resolved,
|
|
@@ -965,7 +988,20 @@ class LanguageUtil {
|
|
|
965
988
|
}
|
|
966
989
|
return code;
|
|
967
990
|
}
|
|
968
|
-
|
|
991
|
+
const formatted = this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
992
|
+
if (this.logger.debug && isString(code)) this.deprecateBareCode(code, formatted);
|
|
993
|
+
return formatted;
|
|
994
|
+
}
|
|
995
|
+
deprecateBareCode(code, formatted) {
|
|
996
|
+
let v27;
|
|
997
|
+
try {
|
|
998
|
+
v27 = Intl.getCanonicalLocales(code)[0];
|
|
999
|
+
} catch (e) {
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
if (this.options.lowerCaseLng) v27 = v27.toLowerCase();
|
|
1003
|
+
if (v27 === formatted) return;
|
|
1004
|
+
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`);
|
|
969
1005
|
}
|
|
970
1006
|
isSupportedCode(code) {
|
|
971
1007
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1174,6 +1210,15 @@ class Interpolator {
|
|
|
1174
1210
|
maxReplaces,
|
|
1175
1211
|
alwaysFormat
|
|
1176
1212
|
} = options.interpolation;
|
|
1213
|
+
if (prefixEscaped || suffixEscaped || nestingPrefixEscaped || nestingSuffixEscaped) {
|
|
1214
|
+
const names = Object.entries({
|
|
1215
|
+
prefixEscaped,
|
|
1216
|
+
suffixEscaped,
|
|
1217
|
+
nestingPrefixEscaped,
|
|
1218
|
+
nestingSuffixEscaped
|
|
1219
|
+
}).filter(([, v]) => v).map(([k]) => k);
|
|
1220
|
+
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`);
|
|
1221
|
+
}
|
|
1177
1222
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1178
1223
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1179
1224
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
@@ -1208,6 +1253,7 @@ class Interpolator {
|
|
|
1208
1253
|
let match;
|
|
1209
1254
|
let value;
|
|
1210
1255
|
let replaces;
|
|
1256
|
+
const original = str;
|
|
1211
1257
|
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1212
1258
|
const handleFormat = key => {
|
|
1213
1259
|
if (!key.includes(this.formatSeparator)) {
|
|
@@ -1238,7 +1284,13 @@ class Interpolator {
|
|
|
1238
1284
|
safeValue: val => val
|
|
1239
1285
|
}, {
|
|
1240
1286
|
regex: this.regexp,
|
|
1241
|
-
safeValue: val =>
|
|
1287
|
+
safeValue: val => {
|
|
1288
|
+
if (!this.escapeValue) return val;
|
|
1289
|
+
if (this.escape === escape && this.options?.interpolation?.escapeValue !== false && isString(val) && val.includes('/')) {
|
|
1290
|
+
this.logger.once('/', 'deprecate', "escapeValue renders '/' in a value as '/' today; from v27 the default escape leaves '/' as it is");
|
|
1291
|
+
}
|
|
1292
|
+
return this.escape(val);
|
|
1293
|
+
}
|
|
1242
1294
|
}];
|
|
1243
1295
|
const unescapedValues = [];
|
|
1244
1296
|
let shift = 0;
|
|
@@ -1260,6 +1312,9 @@ class Interpolator {
|
|
|
1260
1312
|
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1261
1313
|
value = '';
|
|
1262
1314
|
} else if (skipOnVariables) {
|
|
1315
|
+
if (this.logger.debug) {
|
|
1316
|
+
this.logger.once(`${original}\u0000${match[0]}`, 'warn', `missed to pass in variable ${matchedVar} for interpolating ${original}`);
|
|
1317
|
+
}
|
|
1263
1318
|
value = match[0];
|
|
1264
1319
|
continue;
|
|
1265
1320
|
} else {
|
|
@@ -1526,6 +1581,10 @@ class Connector extends EventEmitter {
|
|
|
1526
1581
|
this.state = {};
|
|
1527
1582
|
this.queue = [];
|
|
1528
1583
|
this.backend?.init?.(services, options.backend, options);
|
|
1584
|
+
this.callbackApi = callbackApiOf(this.backend);
|
|
1585
|
+
if (this.backend && this.callbackApi === undefined && (this.backend.read || this.backend.create)) {
|
|
1586
|
+
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');
|
|
1587
|
+
}
|
|
1529
1588
|
}
|
|
1530
1589
|
queueLoad(languages, namespaces, options, callback) {
|
|
1531
1590
|
const toLoad = {};
|
|
@@ -1633,7 +1692,7 @@ class Connector extends EventEmitter {
|
|
|
1633
1692
|
callback(err, data);
|
|
1634
1693
|
};
|
|
1635
1694
|
const fc = this.backend[fcName].bind(this.backend);
|
|
1636
|
-
if (fc.length === 2) {
|
|
1695
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length === 2) {
|
|
1637
1696
|
try {
|
|
1638
1697
|
const r = fc(lng, ns);
|
|
1639
1698
|
if (r && typeof r.then === 'function') {
|
|
@@ -1672,32 +1731,38 @@ class Connector extends EventEmitter {
|
|
|
1672
1731
|
reload: true
|
|
1673
1732
|
}, callback);
|
|
1674
1733
|
}
|
|
1734
|
+
noticeDevFallback(lng) {
|
|
1735
|
+
if (lng !== 'dev' || !this.devIsDefaultFallback) return;
|
|
1736
|
+
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");
|
|
1737
|
+
}
|
|
1675
1738
|
loadOne(name, prefix = '') {
|
|
1676
1739
|
const s = name.split('|');
|
|
1677
1740
|
const lng = s[0];
|
|
1678
1741
|
const ns = s[1];
|
|
1742
|
+
this.noticeDevFallback(lng);
|
|
1679
1743
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1680
1744
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1681
1745
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1682
1746
|
this.loaded(name, err, data);
|
|
1683
1747
|
});
|
|
1684
1748
|
}
|
|
1685
|
-
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
|
|
1686
|
-
if (
|
|
1749
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}, hasLoadedNamespace = this.newestCloneHasLoadedNamespace || this.services?.utils?.hasLoadedNamespace) {
|
|
1750
|
+
if (hasLoadedNamespace && !hasLoadedNamespace(namespace)) {
|
|
1687
1751
|
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!!!');
|
|
1688
1752
|
return;
|
|
1689
1753
|
}
|
|
1690
1754
|
if (key === undefined || key === null || key === '') return;
|
|
1691
1755
|
if (this.backend?.create) {
|
|
1756
|
+
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1692
1757
|
const opts = {
|
|
1693
1758
|
...options,
|
|
1694
1759
|
isUpdate
|
|
1695
1760
|
};
|
|
1696
1761
|
const fc = this.backend.create.bind(this.backend);
|
|
1697
|
-
if (fc.length < 6) {
|
|
1762
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length < 6) {
|
|
1698
1763
|
try {
|
|
1699
1764
|
let r;
|
|
1700
|
-
if (fc.length === 5) {
|
|
1765
|
+
if (this.callbackApi === false || fc.length === 5) {
|
|
1701
1766
|
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1702
1767
|
} else {
|
|
1703
1768
|
r = fc(languages, namespace, key, fallbackValue);
|
|
@@ -1790,6 +1855,7 @@ const transformOptions = options => {
|
|
|
1790
1855
|
};
|
|
1791
1856
|
|
|
1792
1857
|
const noop = () => {};
|
|
1858
|
+
let loggerOwner;
|
|
1793
1859
|
const bindMemberFunctions = inst => {
|
|
1794
1860
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1795
1861
|
mems.forEach(mem => {
|
|
@@ -1802,7 +1868,9 @@ class I18n extends EventEmitter {
|
|
|
1802
1868
|
constructor(options = {}, callback) {
|
|
1803
1869
|
super();
|
|
1804
1870
|
this.options = transformOptions(options);
|
|
1805
|
-
this.services = {
|
|
1871
|
+
this.services = {
|
|
1872
|
+
ownPostProcessors: new Set()
|
|
1873
|
+
};
|
|
1806
1874
|
this.logger = baseLogger;
|
|
1807
1875
|
this.modules = {
|
|
1808
1876
|
external: []
|
|
@@ -1831,6 +1899,7 @@ class I18n extends EventEmitter {
|
|
|
1831
1899
|
options.defaultNS = options.ns[0];
|
|
1832
1900
|
}
|
|
1833
1901
|
}
|
|
1902
|
+
const fallbackLngIsDefault = options.fallbackLng === undefined && this.options.fallbackLng === undefined;
|
|
1834
1903
|
const defOpts = get();
|
|
1835
1904
|
this.options = {
|
|
1836
1905
|
...defOpts,
|
|
@@ -1856,11 +1925,17 @@ class I18n extends EventEmitter {
|
|
|
1856
1925
|
return ClassOrObject;
|
|
1857
1926
|
};
|
|
1858
1927
|
if (!this.options.isClone) {
|
|
1928
|
+
if (loggerOwner && loggerOwner !== this && loggerOwner.modules.logger !== this.modules.logger) {
|
|
1929
|
+
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');
|
|
1930
|
+
}
|
|
1931
|
+
loggerOwner = this.modules.logger ? this : undefined;
|
|
1859
1932
|
if (this.modules.logger) {
|
|
1860
1933
|
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1861
1934
|
} else {
|
|
1862
1935
|
baseLogger.init(null, this.options);
|
|
1863
1936
|
}
|
|
1937
|
+
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1938
|
+
this.pendingWarnings = undefined;
|
|
1864
1939
|
let formatter;
|
|
1865
1940
|
if (this.modules.formatter) {
|
|
1866
1941
|
formatter = this.modules.formatter;
|
|
@@ -1883,15 +1958,20 @@ class I18n extends EventEmitter {
|
|
|
1883
1958
|
}
|
|
1884
1959
|
s.interpolator = new Interpolator(this.options);
|
|
1885
1960
|
s.utils = {
|
|
1886
|
-
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1961
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1962
|
+
reloadResources: this.reloadResources.bind(this)
|
|
1887
1963
|
};
|
|
1888
1964
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1965
|
+
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1889
1966
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1890
1967
|
this.emit(event, ...args);
|
|
1891
1968
|
});
|
|
1892
1969
|
if (this.modules.languageDetector) {
|
|
1893
1970
|
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1894
1971
|
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1972
|
+
if (s.languageDetector.async && callbackApiOf(s.languageDetector) === undefined) {
|
|
1973
|
+
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');
|
|
1974
|
+
}
|
|
1895
1975
|
}
|
|
1896
1976
|
if (this.modules.i18nFormat) {
|
|
1897
1977
|
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
@@ -1987,7 +2067,7 @@ class I18n extends EventEmitter {
|
|
|
1987
2067
|
callback = ns;
|
|
1988
2068
|
ns = undefined;
|
|
1989
2069
|
}
|
|
1990
|
-
if (!lngs) lngs = this.languages;
|
|
2070
|
+
if (!lngs) lngs = this.languages || [];
|
|
1991
2071
|
if (!ns) ns = this.options.ns;
|
|
1992
2072
|
if (!callback) callback = noop;
|
|
1993
2073
|
this.services.backendConnector.reload(lngs, ns, err => {
|
|
@@ -1999,29 +2079,59 @@ class I18n extends EventEmitter {
|
|
|
1999
2079
|
use(module) {
|
|
2000
2080
|
if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
|
|
2001
2081
|
if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
|
|
2082
|
+
const setModule = type => {
|
|
2083
|
+
if (this.modules[type] && this.modules[type] !== module) {
|
|
2084
|
+
const msg = `use: this ${type} replaces the ${type} passed to an earlier use() call; only one ${type} is used per instance`;
|
|
2085
|
+
if (this.isInitializing || this.isInitialized) this.logger.warn(msg);else (this.pendingWarnings = this.pendingWarnings || []).push(msg);
|
|
2086
|
+
}
|
|
2087
|
+
this.modules[type] = module;
|
|
2088
|
+
};
|
|
2002
2089
|
if (module.type === 'backend') {
|
|
2003
|
-
|
|
2090
|
+
setModule('backend');
|
|
2004
2091
|
}
|
|
2005
2092
|
if (module.type === 'logger' || module.log && module.warn && module.error) {
|
|
2006
2093
|
this.modules.logger = module;
|
|
2007
2094
|
}
|
|
2008
2095
|
if (module.type === 'languageDetector') {
|
|
2009
|
-
|
|
2096
|
+
setModule('languageDetector');
|
|
2010
2097
|
}
|
|
2011
2098
|
if (module.type === 'i18nFormat') {
|
|
2012
|
-
|
|
2099
|
+
setModule('i18nFormat');
|
|
2013
2100
|
}
|
|
2014
2101
|
if (module.type === 'postProcessor') {
|
|
2015
2102
|
postProcessor.addPostProcessor(module);
|
|
2103
|
+
this.services.ownPostProcessors?.add(module.name);
|
|
2016
2104
|
}
|
|
2017
2105
|
if (module.type === 'formatter') {
|
|
2018
|
-
|
|
2106
|
+
setModule('formatter');
|
|
2019
2107
|
}
|
|
2020
2108
|
if (module.type === '3rdParty') {
|
|
2021
2109
|
this.modules.external.push(module);
|
|
2022
2110
|
}
|
|
2023
2111
|
return this;
|
|
2024
2112
|
}
|
|
2113
|
+
hydrate(resources, lng) {
|
|
2114
|
+
if (this.options.isClone) return this;
|
|
2115
|
+
if (!this.services.resourceStore) {
|
|
2116
|
+
this.logger.warn('hydrate: call init() first, the instance has no resource store yet');
|
|
2117
|
+
return this;
|
|
2118
|
+
}
|
|
2119
|
+
if (resources && !this.initializedStoreOnce) {
|
|
2120
|
+
this.services.resourceStore.data = resources;
|
|
2121
|
+
Object.values(resources).forEach(lngResources => {
|
|
2122
|
+
Object.keys(lngResources).forEach(ns => {
|
|
2123
|
+
if (!this.options.ns.includes(ns)) this.options.ns.push(ns);
|
|
2124
|
+
});
|
|
2125
|
+
});
|
|
2126
|
+
this.initializedStoreOnce = true;
|
|
2127
|
+
this.isInitialized = true;
|
|
2128
|
+
}
|
|
2129
|
+
if (lng && !this.initializedLanguageOnce) {
|
|
2130
|
+
this.changeLanguage(lng);
|
|
2131
|
+
this.initializedLanguageOnce = true;
|
|
2132
|
+
}
|
|
2133
|
+
return this;
|
|
2134
|
+
}
|
|
2025
2135
|
setResolvedLanguage(l) {
|
|
2026
2136
|
if (!l || !this.languages) return;
|
|
2027
2137
|
if (['cimode', 'dev'].includes(l)) return;
|
|
@@ -2067,6 +2177,12 @@ class I18n extends EventEmitter {
|
|
|
2067
2177
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2068
2178
|
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2069
2179
|
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2180
|
+
if (l && this.logger.debug && !isSafeLanguageCode(l)) {
|
|
2181
|
+
const configured = [].concat(this.options.supportedLngs || [], this.services.languageUtils.getFallbackCodes(this.options.fallbackLng, l), this.options.preload || []);
|
|
2182
|
+
if (!configured.includes(l) && !Object.prototype.hasOwnProperty.call(this.store.data, l)) {
|
|
2183
|
+
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`);
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2070
2186
|
if (l) {
|
|
2071
2187
|
if (!this.language) {
|
|
2072
2188
|
setLngProps(l);
|
|
@@ -2081,7 +2197,8 @@ class I18n extends EventEmitter {
|
|
|
2081
2197
|
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
2082
2198
|
setLng(this.services.languageDetector.detect());
|
|
2083
2199
|
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
2084
|
-
|
|
2200
|
+
const callbackApi = callbackApiOf(this.services.languageDetector);
|
|
2201
|
+
if (callbackApi === false || callbackApi === undefined && this.services.languageDetector.detect.length === 0) {
|
|
2085
2202
|
this.services.languageDetector.detect().then(setLng);
|
|
2086
2203
|
} else {
|
|
2087
2204
|
this.services.languageDetector.detect(setLng);
|
|
@@ -2136,7 +2253,11 @@ class I18n extends EventEmitter {
|
|
|
2136
2253
|
return fixedT;
|
|
2137
2254
|
}
|
|
2138
2255
|
t(...args) {
|
|
2139
|
-
|
|
2256
|
+
if (!this.translator) {
|
|
2257
|
+
this.logger.warn('t: called before init(); returning an empty string');
|
|
2258
|
+
return '';
|
|
2259
|
+
}
|
|
2260
|
+
return this.translator.translate(...args);
|
|
2140
2261
|
}
|
|
2141
2262
|
exists(...args) {
|
|
2142
2263
|
return this.translator?.exists(...args);
|
|
@@ -2208,7 +2329,7 @@ class I18n extends EventEmitter {
|
|
|
2208
2329
|
}
|
|
2209
2330
|
dir(lng) {
|
|
2210
2331
|
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2211
|
-
if (!lng) return '
|
|
2332
|
+
if (!lng) return 'ltr';
|
|
2212
2333
|
try {
|
|
2213
2334
|
const l = new Intl.Locale(lng);
|
|
2214
2335
|
if (l && l.getTextInfo) {
|
|
@@ -2254,7 +2375,9 @@ class I18n extends EventEmitter {
|
|
|
2254
2375
|
...this.services
|
|
2255
2376
|
};
|
|
2256
2377
|
clone.services.utils = {
|
|
2257
|
-
|
|
2378
|
+
...clone.services.utils,
|
|
2379
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
2380
|
+
reloadResources: clone.reloadResources.bind(clone)
|
|
2258
2381
|
};
|
|
2259
2382
|
if (forkResourceStore) {
|
|
2260
2383
|
const copyData = obj => {
|
|
@@ -2289,9 +2412,7 @@ class I18n extends EventEmitter {
|
|
|
2289
2412
|
});
|
|
2290
2413
|
clone.init(mergedOptions, callback);
|
|
2291
2414
|
clone.translator.options = mergedOptions;
|
|
2292
|
-
clone.translator.backendConnector.
|
|
2293
|
-
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2294
|
-
};
|
|
2415
|
+
clone.translator.backendConnector.newestCloneHasLoadedNamespace = clone.hasLoadedNamespace.bind(clone);
|
|
2295
2416
|
return clone;
|
|
2296
2417
|
}
|
|
2297
2418
|
toJSON() {
|
|
@@ -2307,5 +2428,6 @@ class I18n extends EventEmitter {
|
|
|
2307
2428
|
const instance = I18n.createInstance();
|
|
2308
2429
|
|
|
2309
2430
|
instance.keyFromSelector = keysFromSelector;
|
|
2431
|
+
instance.isSafeLanguageCode = isSafeLanguageCode;
|
|
2310
2432
|
|
|
2311
2433
|
module.exports = instance;
|