i18next 26.6.0 → 26.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/i18next.js +156 -31
- package/dist/esm/i18next.js +156 -32
- package/dist/esm/package.json +1 -1
- package/dist/umd/i18next.js +156 -31
- package/dist/umd/i18next.min.js +1 -1
- package/i18next.js +156 -31
- package/i18next.min.js +1 -1
- package/index.d.mts +8 -0
- package/index.d.ts +47 -2
- package/package.json +10 -1
- package/typescript/options.d.ts +40 -4
- package/typescript/t.d.ts +54 -14
package/dist/esm/i18next.js
CHANGED
|
@@ -14,6 +14,8 @@ const makeString = object => {
|
|
|
14
14
|
if (object == null) return '';
|
|
15
15
|
return String(object);
|
|
16
16
|
};
|
|
17
|
+
const isSafeLanguageCode = code => isString(code) && /^[A-Za-z0-9_@.-]{1,128}$/.test(code) && !code.includes('..') && !['__proto__', 'constructor', 'prototype'].includes(code);
|
|
18
|
+
const callbackApiOf = plugin => plugin?.callbackApi ?? plugin?.constructor?.callbackApi;
|
|
17
19
|
const copy = (a, s, t) => {
|
|
18
20
|
a.forEach(m => {
|
|
19
21
|
if (s[m]) t[m] = s[m];
|
|
@@ -217,6 +219,7 @@ class Logger {
|
|
|
217
219
|
this.logger = concreteLogger || consoleLogger;
|
|
218
220
|
this.options = options;
|
|
219
221
|
this.debug = options.debug;
|
|
222
|
+
this.seen = new Set();
|
|
220
223
|
}
|
|
221
224
|
log(...args) {
|
|
222
225
|
return this.forward(args, 'log', '', true);
|
|
@@ -230,6 +233,11 @@ class Logger {
|
|
|
230
233
|
deprecate(...args) {
|
|
231
234
|
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
|
|
232
235
|
}
|
|
236
|
+
once(key, lvl, ...args) {
|
|
237
|
+
if (!this.debug || this.seen.has(key) || this.seen.size >= 1000) return null;
|
|
238
|
+
this.seen.add(key);
|
|
239
|
+
return this[lvl](...args);
|
|
240
|
+
}
|
|
233
241
|
forward(args, lvl, prefix, debugOnly) {
|
|
234
242
|
if (debugOnly && !this.debug) return null;
|
|
235
243
|
args = args.map(a => isString(a) ? a.replace(/[\r\n\x00-\x1F\x7F]/g, ' ') : a);
|
|
@@ -264,13 +272,15 @@ class EventEmitter {
|
|
|
264
272
|
});
|
|
265
273
|
return this;
|
|
266
274
|
}
|
|
267
|
-
off(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
275
|
+
off(events, listener) {
|
|
276
|
+
(typeof events === 'string' ? events.split(' ') : [events]).forEach(event => {
|
|
277
|
+
if (!this.observers[event]) return;
|
|
278
|
+
if (!listener) {
|
|
279
|
+
delete this.observers[event];
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
this.observers[event].delete(listener);
|
|
283
|
+
});
|
|
274
284
|
}
|
|
275
285
|
once(event, listener) {
|
|
276
286
|
const wrapper = (...args) => {
|
|
@@ -309,6 +319,7 @@ class ResourceStore extends EventEmitter {
|
|
|
309
319
|
super();
|
|
310
320
|
this.data = data || {};
|
|
311
321
|
this.options = options;
|
|
322
|
+
this.logger = baseLogger.create('resourceStore');
|
|
312
323
|
if (this.options.keySeparator === undefined) {
|
|
313
324
|
this.options.keySeparator = '.';
|
|
314
325
|
}
|
|
@@ -327,11 +338,15 @@ class ResourceStore extends EventEmitter {
|
|
|
327
338
|
this.options.ns.splice(index, 1);
|
|
328
339
|
}
|
|
329
340
|
}
|
|
341
|
+
deprecateDottedLng(method) {
|
|
342
|
+
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`);
|
|
343
|
+
}
|
|
330
344
|
getResource(lng, ns, key, options = {}) {
|
|
331
345
|
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
332
346
|
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
333
347
|
let path;
|
|
334
348
|
if (lng.includes('.')) {
|
|
349
|
+
this.deprecateDottedLng('getResource');
|
|
335
350
|
path = lng.split('.');
|
|
336
351
|
} else {
|
|
337
352
|
path = [lng, ns];
|
|
@@ -361,6 +376,7 @@ class ResourceStore extends EventEmitter {
|
|
|
361
376
|
let path = [lng, ns];
|
|
362
377
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
363
378
|
if (lng.includes('.')) {
|
|
379
|
+
this.deprecateDottedLng('addResource');
|
|
364
380
|
path = lng.split('.');
|
|
365
381
|
value = ns;
|
|
366
382
|
ns = path[1];
|
|
@@ -385,6 +401,7 @@ class ResourceStore extends EventEmitter {
|
|
|
385
401
|
}) {
|
|
386
402
|
let path = [lng, ns];
|
|
387
403
|
if (lng.includes('.')) {
|
|
404
|
+
this.deprecateDottedLng('addResourceBundle');
|
|
388
405
|
path = lng.split('.');
|
|
389
406
|
deep = resources;
|
|
390
407
|
resources = ns;
|
|
@@ -488,7 +505,7 @@ const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' &
|
|
|
488
505
|
class Translator extends EventEmitter {
|
|
489
506
|
constructor(services, options = {}) {
|
|
490
507
|
super();
|
|
491
|
-
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
|
|
508
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils', 'ownPostProcessors'], services, this);
|
|
492
509
|
this.options = options;
|
|
493
510
|
if (this.options.keySeparator === undefined) {
|
|
494
511
|
this.options.keySeparator = '.';
|
|
@@ -621,6 +638,7 @@ class Translator extends EventEmitter {
|
|
|
621
638
|
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
622
639
|
if (!this.options.returnedObjectHandler) {
|
|
623
640
|
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
|
641
|
+
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)");
|
|
624
642
|
}
|
|
625
643
|
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
626
644
|
...opt,
|
|
@@ -705,7 +723,7 @@ class Translator extends EventEmitter {
|
|
|
705
723
|
if (this.options.missingKeyHandler) {
|
|
706
724
|
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
707
725
|
} else if (this.backendConnector?.saveMissing) {
|
|
708
|
-
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
726
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt, undefined, this.utils?.hasLoadedNamespace);
|
|
709
727
|
}
|
|
710
728
|
this.emit('missingKey', l, namespace, k, res);
|
|
711
729
|
};
|
|
@@ -788,6 +806,11 @@ class Translator extends EventEmitter {
|
|
|
788
806
|
const postProcess = opt.postProcess || this.options.postProcess;
|
|
789
807
|
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
790
808
|
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
809
|
+
postProcessorNames.forEach(name => {
|
|
810
|
+
if (this.logger.debug && postProcessor.processors[name] && this.ownPostProcessors && !this.ownPostProcessors.has(name)) {
|
|
811
|
+
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`);
|
|
812
|
+
}
|
|
813
|
+
});
|
|
791
814
|
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
792
815
|
i18nResolved: {
|
|
793
816
|
...resolved,
|
|
@@ -963,7 +986,20 @@ class LanguageUtil {
|
|
|
963
986
|
}
|
|
964
987
|
return code;
|
|
965
988
|
}
|
|
966
|
-
|
|
989
|
+
const formatted = this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
990
|
+
if (this.logger.debug && isString(code)) this.deprecateBareCode(code, formatted);
|
|
991
|
+
return formatted;
|
|
992
|
+
}
|
|
993
|
+
deprecateBareCode(code, formatted) {
|
|
994
|
+
let v27;
|
|
995
|
+
try {
|
|
996
|
+
v27 = Intl.getCanonicalLocales(code)[0];
|
|
997
|
+
} catch (e) {
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
if (this.options.lowerCaseLng) v27 = v27.toLowerCase();
|
|
1001
|
+
if (v27 === formatted) return;
|
|
1002
|
+
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`);
|
|
967
1003
|
}
|
|
968
1004
|
isSupportedCode(code) {
|
|
969
1005
|
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
|
@@ -1172,6 +1208,15 @@ class Interpolator {
|
|
|
1172
1208
|
maxReplaces,
|
|
1173
1209
|
alwaysFormat
|
|
1174
1210
|
} = options.interpolation;
|
|
1211
|
+
if (prefixEscaped || suffixEscaped || nestingPrefixEscaped || nestingSuffixEscaped) {
|
|
1212
|
+
const names = Object.entries({
|
|
1213
|
+
prefixEscaped,
|
|
1214
|
+
suffixEscaped,
|
|
1215
|
+
nestingPrefixEscaped,
|
|
1216
|
+
nestingSuffixEscaped
|
|
1217
|
+
}).filter(([, v]) => v).map(([k]) => k);
|
|
1218
|
+
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`);
|
|
1219
|
+
}
|
|
1175
1220
|
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
|
1176
1221
|
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
|
1177
1222
|
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
|
@@ -1206,6 +1251,7 @@ class Interpolator {
|
|
|
1206
1251
|
let match;
|
|
1207
1252
|
let value;
|
|
1208
1253
|
let replaces;
|
|
1254
|
+
const original = str;
|
|
1209
1255
|
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1210
1256
|
const handleFormat = key => {
|
|
1211
1257
|
if (!key.includes(this.formatSeparator)) {
|
|
@@ -1236,7 +1282,13 @@ class Interpolator {
|
|
|
1236
1282
|
safeValue: val => val
|
|
1237
1283
|
}, {
|
|
1238
1284
|
regex: this.regexp,
|
|
1239
|
-
safeValue: val =>
|
|
1285
|
+
safeValue: val => {
|
|
1286
|
+
if (!this.escapeValue) return val;
|
|
1287
|
+
if (this.escape === escape && this.options?.interpolation?.escapeValue !== false && isString(val) && val.includes('/')) {
|
|
1288
|
+
this.logger.once('/', 'deprecate', "escapeValue renders '/' in a value as '/' today; from v27 the default escape leaves '/' as it is");
|
|
1289
|
+
}
|
|
1290
|
+
return this.escape(val);
|
|
1291
|
+
}
|
|
1240
1292
|
}];
|
|
1241
1293
|
const unescapedValues = [];
|
|
1242
1294
|
let shift = 0;
|
|
@@ -1258,6 +1310,9 @@ class Interpolator {
|
|
|
1258
1310
|
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1259
1311
|
value = '';
|
|
1260
1312
|
} else if (skipOnVariables) {
|
|
1313
|
+
if (this.logger.debug) {
|
|
1314
|
+
this.logger.once(`${original}\u0000${match[0]}`, 'warn', `missed to pass in variable ${matchedVar} for interpolating ${original}`);
|
|
1315
|
+
}
|
|
1261
1316
|
value = match[0];
|
|
1262
1317
|
continue;
|
|
1263
1318
|
} else {
|
|
@@ -1524,6 +1579,10 @@ class Connector extends EventEmitter {
|
|
|
1524
1579
|
this.state = {};
|
|
1525
1580
|
this.queue = [];
|
|
1526
1581
|
this.backend?.init?.(services, options.backend, options);
|
|
1582
|
+
this.callbackApi = callbackApiOf(this.backend);
|
|
1583
|
+
if (this.backend && this.callbackApi === undefined && (this.backend.read || this.backend.create)) {
|
|
1584
|
+
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');
|
|
1585
|
+
}
|
|
1527
1586
|
}
|
|
1528
1587
|
queueLoad(languages, namespaces, options, callback) {
|
|
1529
1588
|
const toLoad = {};
|
|
@@ -1536,7 +1595,7 @@ class Connector extends EventEmitter {
|
|
|
1536
1595
|
const name = `${lng}|${ns}`;
|
|
1537
1596
|
if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
|
|
1538
1597
|
this.state[name] = 2;
|
|
1539
|
-
} else if (this.state[name] < 0) ; else if (this.state[name] === 1) {
|
|
1598
|
+
} else if (!options.reload && this.state[name] < 0) ; else if (this.state[name] === 1) {
|
|
1540
1599
|
if (pending[name] === undefined) pending[name] = true;
|
|
1541
1600
|
} else {
|
|
1542
1601
|
this.state[name] = 1;
|
|
@@ -1631,7 +1690,7 @@ class Connector extends EventEmitter {
|
|
|
1631
1690
|
callback(err, data);
|
|
1632
1691
|
};
|
|
1633
1692
|
const fc = this.backend[fcName].bind(this.backend);
|
|
1634
|
-
if (fc.length === 2) {
|
|
1693
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length === 2) {
|
|
1635
1694
|
try {
|
|
1636
1695
|
const r = fc(lng, ns);
|
|
1637
1696
|
if (r && typeof r.then === 'function') {
|
|
@@ -1670,32 +1729,38 @@ class Connector extends EventEmitter {
|
|
|
1670
1729
|
reload: true
|
|
1671
1730
|
}, callback);
|
|
1672
1731
|
}
|
|
1732
|
+
noticeDevFallback(lng) {
|
|
1733
|
+
if (lng !== 'dev' || !this.devIsDefaultFallback) return;
|
|
1734
|
+
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");
|
|
1735
|
+
}
|
|
1673
1736
|
loadOne(name, prefix = '') {
|
|
1674
1737
|
const s = name.split('|');
|
|
1675
1738
|
const lng = s[0];
|
|
1676
1739
|
const ns = s[1];
|
|
1740
|
+
this.noticeDevFallback(lng);
|
|
1677
1741
|
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
|
1678
1742
|
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1679
1743
|
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1680
1744
|
this.loaded(name, err, data);
|
|
1681
1745
|
});
|
|
1682
1746
|
}
|
|
1683
|
-
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
|
|
1684
|
-
if (
|
|
1747
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}, hasLoadedNamespace = this.newestCloneHasLoadedNamespace || this.services?.utils?.hasLoadedNamespace) {
|
|
1748
|
+
if (hasLoadedNamespace && !hasLoadedNamespace(namespace)) {
|
|
1685
1749
|
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!!!');
|
|
1686
1750
|
return;
|
|
1687
1751
|
}
|
|
1688
1752
|
if (key === undefined || key === null || key === '') return;
|
|
1689
1753
|
if (this.backend?.create) {
|
|
1754
|
+
languages?.forEach?.(l => this.noticeDevFallback(l));
|
|
1690
1755
|
const opts = {
|
|
1691
1756
|
...options,
|
|
1692
1757
|
isUpdate
|
|
1693
1758
|
};
|
|
1694
1759
|
const fc = this.backend.create.bind(this.backend);
|
|
1695
|
-
if (fc.length < 6) {
|
|
1760
|
+
if (this.callbackApi === false || this.callbackApi === undefined && fc.length < 6) {
|
|
1696
1761
|
try {
|
|
1697
1762
|
let r;
|
|
1698
|
-
if (fc.length === 5) {
|
|
1763
|
+
if (this.callbackApi === false || fc.length === 5) {
|
|
1699
1764
|
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1700
1765
|
} else {
|
|
1701
1766
|
r = fc(languages, namespace, key, fallbackValue);
|
|
@@ -1788,6 +1853,7 @@ const transformOptions = options => {
|
|
|
1788
1853
|
};
|
|
1789
1854
|
|
|
1790
1855
|
const noop = () => {};
|
|
1856
|
+
let loggerOwner;
|
|
1791
1857
|
const bindMemberFunctions = inst => {
|
|
1792
1858
|
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1793
1859
|
mems.forEach(mem => {
|
|
@@ -1800,7 +1866,9 @@ class I18n extends EventEmitter {
|
|
|
1800
1866
|
constructor(options = {}, callback) {
|
|
1801
1867
|
super();
|
|
1802
1868
|
this.options = transformOptions(options);
|
|
1803
|
-
this.services = {
|
|
1869
|
+
this.services = {
|
|
1870
|
+
ownPostProcessors: new Set()
|
|
1871
|
+
};
|
|
1804
1872
|
this.logger = baseLogger;
|
|
1805
1873
|
this.modules = {
|
|
1806
1874
|
external: []
|
|
@@ -1829,6 +1897,7 @@ class I18n extends EventEmitter {
|
|
|
1829
1897
|
options.defaultNS = options.ns[0];
|
|
1830
1898
|
}
|
|
1831
1899
|
}
|
|
1900
|
+
const fallbackLngIsDefault = options.fallbackLng === undefined && this.options.fallbackLng === undefined;
|
|
1832
1901
|
const defOpts = get();
|
|
1833
1902
|
this.options = {
|
|
1834
1903
|
...defOpts,
|
|
@@ -1854,11 +1923,17 @@ class I18n extends EventEmitter {
|
|
|
1854
1923
|
return ClassOrObject;
|
|
1855
1924
|
};
|
|
1856
1925
|
if (!this.options.isClone) {
|
|
1926
|
+
if (loggerOwner && loggerOwner !== this && loggerOwner.modules.logger !== this.modules.logger) {
|
|
1927
|
+
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');
|
|
1928
|
+
}
|
|
1929
|
+
loggerOwner = this.modules.logger ? this : undefined;
|
|
1857
1930
|
if (this.modules.logger) {
|
|
1858
1931
|
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1859
1932
|
} else {
|
|
1860
1933
|
baseLogger.init(null, this.options);
|
|
1861
1934
|
}
|
|
1935
|
+
this.pendingWarnings?.forEach(msg => this.logger.warn(msg));
|
|
1936
|
+
this.pendingWarnings = undefined;
|
|
1862
1937
|
let formatter;
|
|
1863
1938
|
if (this.modules.formatter) {
|
|
1864
1939
|
formatter = this.modules.formatter;
|
|
@@ -1881,15 +1956,20 @@ class I18n extends EventEmitter {
|
|
|
1881
1956
|
}
|
|
1882
1957
|
s.interpolator = new Interpolator(this.options);
|
|
1883
1958
|
s.utils = {
|
|
1884
|
-
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1959
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this),
|
|
1960
|
+
reloadResources: this.reloadResources.bind(this)
|
|
1885
1961
|
};
|
|
1886
1962
|
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1963
|
+
s.backendConnector.devIsDefaultFallback = fallbackLngIsDefault;
|
|
1887
1964
|
s.backendConnector.on('*', (event, ...args) => {
|
|
1888
1965
|
this.emit(event, ...args);
|
|
1889
1966
|
});
|
|
1890
1967
|
if (this.modules.languageDetector) {
|
|
1891
1968
|
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1892
1969
|
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1970
|
+
if (s.languageDetector.async && callbackApiOf(s.languageDetector) === undefined) {
|
|
1971
|
+
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');
|
|
1972
|
+
}
|
|
1893
1973
|
}
|
|
1894
1974
|
if (this.modules.i18nFormat) {
|
|
1895
1975
|
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
@@ -1985,7 +2065,10 @@ class I18n extends EventEmitter {
|
|
|
1985
2065
|
callback = ns;
|
|
1986
2066
|
ns = undefined;
|
|
1987
2067
|
}
|
|
1988
|
-
if (!lngs)
|
|
2068
|
+
if (!lngs) {
|
|
2069
|
+
const requested = Object.keys(this.services.backendConnector.state).map(name => name.split('|')[0]);
|
|
2070
|
+
lngs = [...new Set([...(this.languages || []), ...requested])].filter(l => l !== 'cimode');
|
|
2071
|
+
}
|
|
1989
2072
|
if (!ns) ns = this.options.ns;
|
|
1990
2073
|
if (!callback) callback = noop;
|
|
1991
2074
|
this.services.backendConnector.reload(lngs, ns, err => {
|
|
@@ -1997,29 +2080,59 @@ class I18n extends EventEmitter {
|
|
|
1997
2080
|
use(module) {
|
|
1998
2081
|
if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
|
|
1999
2082
|
if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
|
|
2083
|
+
const setModule = type => {
|
|
2084
|
+
if (this.modules[type] && this.modules[type] !== module) {
|
|
2085
|
+
const msg = `use: this ${type} replaces the ${type} passed to an earlier use() call; only one ${type} is used per instance`;
|
|
2086
|
+
if (this.isInitializing || this.isInitialized) this.logger.warn(msg);else (this.pendingWarnings = this.pendingWarnings || []).push(msg);
|
|
2087
|
+
}
|
|
2088
|
+
this.modules[type] = module;
|
|
2089
|
+
};
|
|
2000
2090
|
if (module.type === 'backend') {
|
|
2001
|
-
|
|
2091
|
+
setModule('backend');
|
|
2002
2092
|
}
|
|
2003
2093
|
if (module.type === 'logger' || module.log && module.warn && module.error) {
|
|
2004
2094
|
this.modules.logger = module;
|
|
2005
2095
|
}
|
|
2006
2096
|
if (module.type === 'languageDetector') {
|
|
2007
|
-
|
|
2097
|
+
setModule('languageDetector');
|
|
2008
2098
|
}
|
|
2009
2099
|
if (module.type === 'i18nFormat') {
|
|
2010
|
-
|
|
2100
|
+
setModule('i18nFormat');
|
|
2011
2101
|
}
|
|
2012
2102
|
if (module.type === 'postProcessor') {
|
|
2013
2103
|
postProcessor.addPostProcessor(module);
|
|
2104
|
+
this.services.ownPostProcessors?.add(module.name);
|
|
2014
2105
|
}
|
|
2015
2106
|
if (module.type === 'formatter') {
|
|
2016
|
-
|
|
2107
|
+
setModule('formatter');
|
|
2017
2108
|
}
|
|
2018
2109
|
if (module.type === '3rdParty') {
|
|
2019
2110
|
this.modules.external.push(module);
|
|
2020
2111
|
}
|
|
2021
2112
|
return this;
|
|
2022
2113
|
}
|
|
2114
|
+
hydrate(resources, lng) {
|
|
2115
|
+
if (this.options.isClone) return this;
|
|
2116
|
+
if (!this.services.resourceStore) {
|
|
2117
|
+
this.logger.warn('hydrate: call init() first, the instance has no resource store yet');
|
|
2118
|
+
return this;
|
|
2119
|
+
}
|
|
2120
|
+
if (resources && !this.initializedStoreOnce) {
|
|
2121
|
+
this.services.resourceStore.data = resources;
|
|
2122
|
+
Object.values(resources).forEach(lngResources => {
|
|
2123
|
+
Object.keys(lngResources).forEach(ns => {
|
|
2124
|
+
if (!this.options.ns.includes(ns)) this.options.ns.push(ns);
|
|
2125
|
+
});
|
|
2126
|
+
});
|
|
2127
|
+
this.initializedStoreOnce = true;
|
|
2128
|
+
this.isInitialized = true;
|
|
2129
|
+
}
|
|
2130
|
+
if (lng && !this.initializedLanguageOnce) {
|
|
2131
|
+
this.changeLanguage(lng);
|
|
2132
|
+
this.initializedLanguageOnce = true;
|
|
2133
|
+
}
|
|
2134
|
+
return this;
|
|
2135
|
+
}
|
|
2023
2136
|
setResolvedLanguage(l) {
|
|
2024
2137
|
if (!l || !this.languages) return;
|
|
2025
2138
|
if (['cimode', 'dev'].includes(l)) return;
|
|
@@ -2065,6 +2178,12 @@ class I18n extends EventEmitter {
|
|
|
2065
2178
|
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2066
2179
|
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2067
2180
|
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2181
|
+
if (l && this.logger.debug && !isSafeLanguageCode(l)) {
|
|
2182
|
+
const configured = [].concat(this.options.supportedLngs || [], this.services.languageUtils.getFallbackCodes(this.options.fallbackLng, l), this.options.preload || []);
|
|
2183
|
+
if (!configured.includes(l) && !Object.prototype.hasOwnProperty.call(this.store.data, l)) {
|
|
2184
|
+
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`);
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2068
2187
|
if (l) {
|
|
2069
2188
|
if (!this.language) {
|
|
2070
2189
|
setLngProps(l);
|
|
@@ -2079,7 +2198,8 @@ class I18n extends EventEmitter {
|
|
|
2079
2198
|
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
2080
2199
|
setLng(this.services.languageDetector.detect());
|
|
2081
2200
|
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
2082
|
-
|
|
2201
|
+
const callbackApi = callbackApiOf(this.services.languageDetector);
|
|
2202
|
+
if (callbackApi === false || callbackApi === undefined && this.services.languageDetector.detect.length === 0) {
|
|
2083
2203
|
this.services.languageDetector.detect().then(setLng);
|
|
2084
2204
|
} else {
|
|
2085
2205
|
this.services.languageDetector.detect(setLng);
|
|
@@ -2134,7 +2254,11 @@ class I18n extends EventEmitter {
|
|
|
2134
2254
|
return fixedT;
|
|
2135
2255
|
}
|
|
2136
2256
|
t(...args) {
|
|
2137
|
-
|
|
2257
|
+
if (!this.translator) {
|
|
2258
|
+
this.logger.warn('t: called before init(); returning an empty string');
|
|
2259
|
+
return '';
|
|
2260
|
+
}
|
|
2261
|
+
return this.translator.translate(...args);
|
|
2138
2262
|
}
|
|
2139
2263
|
exists(...args) {
|
|
2140
2264
|
return this.translator?.exists(...args);
|
|
@@ -2206,7 +2330,7 @@ class I18n extends EventEmitter {
|
|
|
2206
2330
|
}
|
|
2207
2331
|
dir(lng) {
|
|
2208
2332
|
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2209
|
-
if (!lng) return '
|
|
2333
|
+
if (!lng) return 'ltr';
|
|
2210
2334
|
try {
|
|
2211
2335
|
const l = new Intl.Locale(lng);
|
|
2212
2336
|
if (l && l.getTextInfo) {
|
|
@@ -2252,7 +2376,9 @@ class I18n extends EventEmitter {
|
|
|
2252
2376
|
...this.services
|
|
2253
2377
|
};
|
|
2254
2378
|
clone.services.utils = {
|
|
2255
|
-
|
|
2379
|
+
...clone.services.utils,
|
|
2380
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone),
|
|
2381
|
+
reloadResources: clone.reloadResources.bind(clone)
|
|
2256
2382
|
};
|
|
2257
2383
|
if (forkResourceStore) {
|
|
2258
2384
|
const copyData = obj => {
|
|
@@ -2287,9 +2413,7 @@ class I18n extends EventEmitter {
|
|
|
2287
2413
|
});
|
|
2288
2414
|
clone.init(mergedOptions, callback);
|
|
2289
2415
|
clone.translator.options = mergedOptions;
|
|
2290
|
-
clone.translator.backendConnector.
|
|
2291
|
-
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2292
|
-
};
|
|
2416
|
+
clone.translator.backendConnector.newestCloneHasLoadedNamespace = clone.hasLoadedNamespace.bind(clone);
|
|
2293
2417
|
return clone;
|
|
2294
2418
|
}
|
|
2295
2419
|
toJSON() {
|
|
@@ -2319,4 +2443,4 @@ const hasLoadedNamespace = instance.hasLoadedNamespace;
|
|
|
2319
2443
|
const loadNamespaces = instance.loadNamespaces;
|
|
2320
2444
|
const loadLanguages = instance.loadLanguages;
|
|
2321
2445
|
|
|
2322
|
-
export { changeLanguage, createInstance, instance as default, dir, exists, getFixedT, hasLoadedNamespace, init, keysFromSelector as keyFromSelector, loadLanguages, loadNamespaces, loadResources, reloadResources, setDefaultNamespace, t, use };
|
|
2446
|
+
export { changeLanguage, createInstance, instance as default, dir, exists, getFixedT, hasLoadedNamespace, init, isSafeLanguageCode, keysFromSelector as keyFromSelector, loadLanguages, loadNamespaces, loadResources, reloadResources, setDefaultNamespace, t, use };
|
package/dist/esm/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"26.
|
|
1
|
+
{"type":"module","version":"26.7.1"}
|