i18next 17.3.1 → 19.0.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/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ ### 19.0.1
2
+
3
+ - ignore non valid keys in saveMissing [1359](https://github.com/i18next/i18next/pull/1359)
4
+
5
+ ### 19.0.0
6
+
7
+ - typescript: Typescript use `export default` for esm-first approach [1352](https://github.com/i18next/i18next/pull/1352)
8
+
9
+ ### 18.0.1
10
+
11
+ - check loadedNamespace only once per lng-ns inside using `t` for better performance
12
+
13
+ ### 18.0.0
14
+
15
+ - When calling `i18next.changeLanguage()` both `i18next.language` and `i18next.languages` will be set to the new language after calling `loadResources` -> means when accessing `t` function meanwhile you will get still the translations for the previous language instead of the fallback.
16
+
17
+ - **When is this breaking?** this does not break any current test - but if you depend on accessing i18next.language or i18next.dir during language change and expect the new language this will break your app.
18
+
19
+ - Reasoning: In react-i18next we get in a not ready state for loaded translations while we would prefer just waiting for the new language ready and trigger a rerender then - also a triggered rerender outside of the bound events would end in Suspense...
20
+
21
+ - How can I get the language i18next will be set to? `i18next.isLanguageChangingTo` is set to the language called
22
+
1
23
  ### 17.3.1
2
24
 
3
25
  - typescript: Add missing `cleanCode` option to TypeScript def [1344](https://github.com/i18next/i18next/pull/1344)
@@ -478,6 +478,8 @@ var postProcessor = {
478
478
  }
479
479
  };
480
480
 
481
+ var checkedLoadedFor = {};
482
+
481
483
  var Translator =
482
484
  /*#__PURE__*/
483
485
  function (_EventEmitter) {
@@ -550,7 +552,9 @@ function (_EventEmitter) {
550
552
 
551
553
  if (!options) options = {}; // non valid keys handling
552
554
 
553
- if (keys === undefined || keys === null) return '';
555
+ if (keys === undefined || keys === null
556
+ /* || keys === ''*/
557
+ ) return '';
554
558
  if (!Array.isArray(keys)) keys = [String(keys)]; // separators
555
559
 
556
560
  var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; // get namespace(s)
@@ -763,8 +767,10 @@ function (_EventEmitter) {
763
767
  if (_this4.isValidLookup(found)) return;
764
768
  usedNS = ns;
765
769
 
766
- if (_this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
767
- _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
770
+ if (!checkedLoadedFor["".concat(codes[0], "-").concat(ns)] && _this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
771
+ checkedLoadedFor["".concat(codes[0], "-").concat(ns)] = true;
772
+
773
+ _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" for languages \"").concat(codes.join(', '), "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
768
774
  }
769
775
 
770
776
  codes.forEach(function (code) {
@@ -1652,7 +1658,10 @@ function (_EventEmitter) {
1652
1658
  if (this.services.utils && this.services.utils.hasLoadedNamespace && !this.services.utils.hasLoadedNamespace(namespace)) {
1653
1659
  this.logger.warn("did not save key \"".concat(key, "\" for namespace \"").concat(namespace, "\" as the namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
1654
1660
  return;
1655
- }
1661
+ } // ignore non valid keys
1662
+
1663
+
1664
+ if (key === undefined || key === null || key === '') return;
1656
1665
 
1657
1666
  if (this.backend && this.backend.create) {
1658
1667
  this.backend.create(languages, namespace, key, fallbackValue, null
@@ -1933,13 +1942,16 @@ function (_EventEmitter) {
1933
1942
 
1934
1943
  }, {
1935
1944
  key: "loadResources",
1936
- value: function loadResources() {
1945
+ value: function loadResources(language) {
1937
1946
  var _this3 = this;
1938
1947
 
1939
- var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
1948
+ var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
1949
+ var usedCallback = callback;
1950
+ var usedLng = typeof language === 'string' ? language : this.language;
1951
+ if (typeof language === 'function') usedCallback = language;
1940
1952
 
1941
1953
  if (!this.options.resources || this.options.partialBundledLanguages) {
1942
- if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode
1954
+ if (usedLng && usedLng.toLowerCase() === 'cimode') return usedCallback(); // avoid loading resources for cimode
1943
1955
 
1944
1956
  var toLoad = [];
1945
1957
 
@@ -1953,14 +1965,14 @@ function (_EventEmitter) {
1953
1965
  });
1954
1966
  };
1955
1967
 
1956
- if (!this.language) {
1968
+ if (!usedLng) {
1957
1969
  // at least load fallbacks in this case
1958
1970
  var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1959
1971
  fallbacks.forEach(function (l) {
1960
1972
  return append(l);
1961
1973
  });
1962
1974
  } else {
1963
- append(this.language);
1975
+ append(usedLng);
1964
1976
  }
1965
1977
 
1966
1978
  if (this.options.preload) {
@@ -1969,9 +1981,9 @@ function (_EventEmitter) {
1969
1981
  });
1970
1982
  }
1971
1983
 
1972
- this.services.backendConnector.load(toLoad, this.options.ns, callback);
1984
+ this.services.backendConnector.load(toLoad, this.options.ns, usedCallback);
1973
1985
  } else {
1974
- callback(null);
1986
+ usedCallback(null);
1975
1987
  }
1976
1988
  }
1977
1989
  }, {
@@ -2022,16 +2034,24 @@ function (_EventEmitter) {
2022
2034
  value: function changeLanguage(lng, callback) {
2023
2035
  var _this4 = this;
2024
2036
 
2037
+ this.isLanguageChangingTo = lng;
2025
2038
  var deferred = defer();
2026
2039
  this.emit('languageChanging', lng);
2027
2040
 
2028
2041
  var done = function done(err, l) {
2029
- _this4.translator.changeLanguage(l);
2030
-
2031
2042
  if (l) {
2043
+ _this4.language = l;
2044
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2045
+
2046
+ _this4.translator.changeLanguage(l);
2047
+
2048
+ _this4.isLanguageChangingTo = undefined;
2049
+
2032
2050
  _this4.emit('languageChanged', l);
2033
2051
 
2034
2052
  _this4.logger.log('languageChanged', l);
2053
+ } else {
2054
+ _this4.isLanguageChangingTo = undefined;
2035
2055
  }
2036
2056
 
2037
2057
  deferred.resolve(function () {
@@ -2044,13 +2064,16 @@ function (_EventEmitter) {
2044
2064
 
2045
2065
  var setLng = function setLng(l) {
2046
2066
  if (l) {
2047
- _this4.language = l;
2048
- _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2067
+ if (!_this4.language) {
2068
+ _this4.language = l;
2069
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2070
+ }
2071
+
2049
2072
  if (!_this4.translator.language) _this4.translator.changeLanguage(l);
2050
2073
  if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l);
2051
2074
  }
2052
2075
 
2053
- _this4.loadResources(function (err) {
2076
+ _this4.loadResources(l, function (err) {
2054
2077
  done(err, l);
2055
2078
  });
2056
2079
  };
@@ -474,6 +474,8 @@ var postProcessor = {
474
474
  }
475
475
  };
476
476
 
477
+ var checkedLoadedFor = {};
478
+
477
479
  var Translator =
478
480
  /*#__PURE__*/
479
481
  function (_EventEmitter) {
@@ -546,7 +548,9 @@ function (_EventEmitter) {
546
548
 
547
549
  if (!options) options = {}; // non valid keys handling
548
550
 
549
- if (keys === undefined || keys === null) return '';
551
+ if (keys === undefined || keys === null
552
+ /* || keys === ''*/
553
+ ) return '';
550
554
  if (!Array.isArray(keys)) keys = [String(keys)]; // separators
551
555
 
552
556
  var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; // get namespace(s)
@@ -759,8 +763,10 @@ function (_EventEmitter) {
759
763
  if (_this4.isValidLookup(found)) return;
760
764
  usedNS = ns;
761
765
 
762
- if (_this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
763
- _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
766
+ if (!checkedLoadedFor["".concat(codes[0], "-").concat(ns)] && _this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
767
+ checkedLoadedFor["".concat(codes[0], "-").concat(ns)] = true;
768
+
769
+ _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" for languages \"").concat(codes.join(', '), "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
764
770
  }
765
771
 
766
772
  codes.forEach(function (code) {
@@ -1648,7 +1654,10 @@ function (_EventEmitter) {
1648
1654
  if (this.services.utils && this.services.utils.hasLoadedNamespace && !this.services.utils.hasLoadedNamespace(namespace)) {
1649
1655
  this.logger.warn("did not save key \"".concat(key, "\" for namespace \"").concat(namespace, "\" as the namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
1650
1656
  return;
1651
- }
1657
+ } // ignore non valid keys
1658
+
1659
+
1660
+ if (key === undefined || key === null || key === '') return;
1652
1661
 
1653
1662
  if (this.backend && this.backend.create) {
1654
1663
  this.backend.create(languages, namespace, key, fallbackValue, null
@@ -1929,13 +1938,16 @@ function (_EventEmitter) {
1929
1938
 
1930
1939
  }, {
1931
1940
  key: "loadResources",
1932
- value: function loadResources() {
1941
+ value: function loadResources(language) {
1933
1942
  var _this3 = this;
1934
1943
 
1935
- var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
1944
+ var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
1945
+ var usedCallback = callback;
1946
+ var usedLng = typeof language === 'string' ? language : this.language;
1947
+ if (typeof language === 'function') usedCallback = language;
1936
1948
 
1937
1949
  if (!this.options.resources || this.options.partialBundledLanguages) {
1938
- if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode
1950
+ if (usedLng && usedLng.toLowerCase() === 'cimode') return usedCallback(); // avoid loading resources for cimode
1939
1951
 
1940
1952
  var toLoad = [];
1941
1953
 
@@ -1949,14 +1961,14 @@ function (_EventEmitter) {
1949
1961
  });
1950
1962
  };
1951
1963
 
1952
- if (!this.language) {
1964
+ if (!usedLng) {
1953
1965
  // at least load fallbacks in this case
1954
1966
  var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1955
1967
  fallbacks.forEach(function (l) {
1956
1968
  return append(l);
1957
1969
  });
1958
1970
  } else {
1959
- append(this.language);
1971
+ append(usedLng);
1960
1972
  }
1961
1973
 
1962
1974
  if (this.options.preload) {
@@ -1965,9 +1977,9 @@ function (_EventEmitter) {
1965
1977
  });
1966
1978
  }
1967
1979
 
1968
- this.services.backendConnector.load(toLoad, this.options.ns, callback);
1980
+ this.services.backendConnector.load(toLoad, this.options.ns, usedCallback);
1969
1981
  } else {
1970
- callback(null);
1982
+ usedCallback(null);
1971
1983
  }
1972
1984
  }
1973
1985
  }, {
@@ -2018,16 +2030,24 @@ function (_EventEmitter) {
2018
2030
  value: function changeLanguage(lng, callback) {
2019
2031
  var _this4 = this;
2020
2032
 
2033
+ this.isLanguageChangingTo = lng;
2021
2034
  var deferred = defer();
2022
2035
  this.emit('languageChanging', lng);
2023
2036
 
2024
2037
  var done = function done(err, l) {
2025
- _this4.translator.changeLanguage(l);
2026
-
2027
2038
  if (l) {
2039
+ _this4.language = l;
2040
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2041
+
2042
+ _this4.translator.changeLanguage(l);
2043
+
2044
+ _this4.isLanguageChangingTo = undefined;
2045
+
2028
2046
  _this4.emit('languageChanged', l);
2029
2047
 
2030
2048
  _this4.logger.log('languageChanged', l);
2049
+ } else {
2050
+ _this4.isLanguageChangingTo = undefined;
2031
2051
  }
2032
2052
 
2033
2053
  deferred.resolve(function () {
@@ -2040,13 +2060,16 @@ function (_EventEmitter) {
2040
2060
 
2041
2061
  var setLng = function setLng(l) {
2042
2062
  if (l) {
2043
- _this4.language = l;
2044
- _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2063
+ if (!_this4.language) {
2064
+ _this4.language = l;
2065
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2066
+ }
2067
+
2045
2068
  if (!_this4.translator.language) _this4.translator.changeLanguage(l);
2046
2069
  if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l);
2047
2070
  }
2048
2071
 
2049
- _this4.loadResources(function (err) {
2072
+ _this4.loadResources(l, function (err) {
2050
2073
  done(err, l);
2051
2074
  });
2052
2075
  };
@@ -610,6 +610,8 @@
610
610
  }
611
611
  };
612
612
 
613
+ var checkedLoadedFor = {};
614
+
613
615
  var Translator =
614
616
  /*#__PURE__*/
615
617
  function (_EventEmitter) {
@@ -682,7 +684,9 @@
682
684
 
683
685
  if (!options) options = {}; // non valid keys handling
684
686
 
685
- if (keys === undefined || keys === null) return '';
687
+ if (keys === undefined || keys === null
688
+ /* || keys === ''*/
689
+ ) return '';
686
690
  if (!Array.isArray(keys)) keys = [String(keys)]; // separators
687
691
 
688
692
  var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; // get namespace(s)
@@ -895,8 +899,10 @@
895
899
  if (_this4.isValidLookup(found)) return;
896
900
  usedNS = ns;
897
901
 
898
- if (_this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
899
- _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
902
+ if (!checkedLoadedFor["".concat(codes[0], "-").concat(ns)] && _this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
903
+ checkedLoadedFor["".concat(codes[0], "-").concat(ns)] = true;
904
+
905
+ _this4.logger.warn("key \"".concat(usedKey, "\" for namespace \"").concat(usedNS, "\" for languages \"").concat(codes.join(', '), "\" won't get resolved as namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
900
906
  }
901
907
 
902
908
  codes.forEach(function (code) {
@@ -1822,7 +1828,10 @@
1822
1828
  if (this.services.utils && this.services.utils.hasLoadedNamespace && !this.services.utils.hasLoadedNamespace(namespace)) {
1823
1829
  this.logger.warn("did not save key \"".concat(key, "\" for namespace \"").concat(namespace, "\" as the namespace was not yet loaded"), 'This means something IS WRONG in your application 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!!!');
1824
1830
  return;
1825
- }
1831
+ } // ignore non valid keys
1832
+
1833
+
1834
+ if (key === undefined || key === null || key === '') return;
1826
1835
 
1827
1836
  if (this.backend && this.backend.create) {
1828
1837
  this.backend.create(languages, namespace, key, fallbackValue, null
@@ -2103,13 +2112,16 @@
2103
2112
 
2104
2113
  }, {
2105
2114
  key: "loadResources",
2106
- value: function loadResources() {
2115
+ value: function loadResources(language) {
2107
2116
  var _this3 = this;
2108
2117
 
2109
- var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
2118
+ var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
2119
+ var usedCallback = callback;
2120
+ var usedLng = typeof language === 'string' ? language : this.language;
2121
+ if (typeof language === 'function') usedCallback = language;
2110
2122
 
2111
2123
  if (!this.options.resources || this.options.partialBundledLanguages) {
2112
- if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode
2124
+ if (usedLng && usedLng.toLowerCase() === 'cimode') return usedCallback(); // avoid loading resources for cimode
2113
2125
 
2114
2126
  var toLoad = [];
2115
2127
 
@@ -2123,14 +2135,14 @@
2123
2135
  });
2124
2136
  };
2125
2137
 
2126
- if (!this.language) {
2138
+ if (!usedLng) {
2127
2139
  // at least load fallbacks in this case
2128
2140
  var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2129
2141
  fallbacks.forEach(function (l) {
2130
2142
  return append(l);
2131
2143
  });
2132
2144
  } else {
2133
- append(this.language);
2145
+ append(usedLng);
2134
2146
  }
2135
2147
 
2136
2148
  if (this.options.preload) {
@@ -2139,9 +2151,9 @@
2139
2151
  });
2140
2152
  }
2141
2153
 
2142
- this.services.backendConnector.load(toLoad, this.options.ns, callback);
2154
+ this.services.backendConnector.load(toLoad, this.options.ns, usedCallback);
2143
2155
  } else {
2144
- callback(null);
2156
+ usedCallback(null);
2145
2157
  }
2146
2158
  }
2147
2159
  }, {
@@ -2192,16 +2204,24 @@
2192
2204
  value: function changeLanguage(lng, callback) {
2193
2205
  var _this4 = this;
2194
2206
 
2207
+ this.isLanguageChangingTo = lng;
2195
2208
  var deferred = defer();
2196
2209
  this.emit('languageChanging', lng);
2197
2210
 
2198
2211
  var done = function done(err, l) {
2199
- _this4.translator.changeLanguage(l);
2200
-
2201
2212
  if (l) {
2213
+ _this4.language = l;
2214
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2215
+
2216
+ _this4.translator.changeLanguage(l);
2217
+
2218
+ _this4.isLanguageChangingTo = undefined;
2219
+
2202
2220
  _this4.emit('languageChanged', l);
2203
2221
 
2204
2222
  _this4.logger.log('languageChanged', l);
2223
+ } else {
2224
+ _this4.isLanguageChangingTo = undefined;
2205
2225
  }
2206
2226
 
2207
2227
  deferred.resolve(function () {
@@ -2214,13 +2234,16 @@
2214
2234
 
2215
2235
  var setLng = function setLng(l) {
2216
2236
  if (l) {
2217
- _this4.language = l;
2218
- _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2237
+ if (!_this4.language) {
2238
+ _this4.language = l;
2239
+ _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
2240
+ }
2241
+
2219
2242
  if (!_this4.translator.language) _this4.translator.changeLanguage(l);
2220
2243
  if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l);
2221
2244
  }
2222
2245
 
2223
- _this4.loadResources(function (err) {
2246
+ _this4.loadResources(l, function (err) {
2224
2247
  done(err, l);
2225
2248
  });
2226
2249
  };