i18next 21.2.5 → 21.3.2

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.
@@ -6,6 +6,7 @@ import _possibleConstructorReturn from '@babel/runtime/helpers/esm/possibleConst
6
6
  import _getPrototypeOf from '@babel/runtime/helpers/esm/getPrototypeOf';
7
7
  import _assertThisInitialized from '@babel/runtime/helpers/esm/assertThisInitialized';
8
8
  import _inherits from '@babel/runtime/helpers/esm/inherits';
9
+ import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
9
10
 
10
11
  var consoleLogger = {
11
12
  type: 'logger',
@@ -595,7 +596,7 @@ var Translator = function (_EventEmitter) {
595
596
  var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
596
597
  var namespaces = options.ns || this.options.defaultNS;
597
598
  var wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
598
- var seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !this.options.userDefinedNsSeparator && !options.keySeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
599
+ var seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !options.keySeparator && !this.options.userDefinedNsSeparator && !options.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
599
600
 
600
601
  if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
601
602
  var m = key.match(this.interpolator.nestingRegexp);
@@ -1614,6 +1615,124 @@ var Interpolator = function () {
1614
1615
  return Interpolator;
1615
1616
  }();
1616
1617
 
1618
+ function parseFormatStr(formatStr) {
1619
+ var formatName = formatStr.toLowerCase();
1620
+ var formatOptions = {};
1621
+
1622
+ if (formatStr.indexOf('(') > -1) {
1623
+ var p = formatStr.split('(');
1624
+ formatName = p[0].toLowerCase();
1625
+ var optStr = p[1].substring(0, p[1].length - 1);
1626
+
1627
+ if (formatName === 'currency' && optStr.indexOf(':') < 0) {
1628
+ if (!formatOptions.currency) formatOptions.currency = optStr.trim();
1629
+ } else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
1630
+ if (!formatOptions.range) formatOptions.range = optStr.trim();
1631
+ } else {
1632
+ var opts = optStr.split(';');
1633
+ opts.forEach(function (opt) {
1634
+ if (!opt) return;
1635
+
1636
+ var _opt$split = opt.split(':'),
1637
+ _opt$split2 = _slicedToArray(_opt$split, 2),
1638
+ key = _opt$split2[0],
1639
+ val = _opt$split2[1];
1640
+
1641
+ if (val.trim() === 'false') formatOptions[key.trim()] = false;
1642
+ if (val.trim() === 'true') formatOptions[key.trim()] = true;
1643
+ if (!isNaN(val.trim())) formatOptions[key.trim()] = parseInt(val.trim(), 10);
1644
+ if (!formatOptions[key.trim()]) formatOptions[key.trim()] = val.trim();
1645
+ });
1646
+ }
1647
+ }
1648
+
1649
+ return {
1650
+ formatName: formatName,
1651
+ formatOptions: formatOptions
1652
+ };
1653
+ }
1654
+
1655
+ var Formatter = function () {
1656
+ function Formatter() {
1657
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1658
+
1659
+ _classCallCheck(this, Formatter);
1660
+
1661
+ this.logger = baseLogger.create('formatter');
1662
+ this.options = options;
1663
+ this.formats = {
1664
+ number: function number(val, lng, options) {
1665
+ return new Intl.NumberFormat(lng, options).format(val);
1666
+ },
1667
+ currency: function currency(val, lng, options) {
1668
+ return new Intl.NumberFormat(lng, _objectSpread({}, options, {
1669
+ style: 'currency'
1670
+ })).format(val);
1671
+ },
1672
+ datetime: function datetime(val, lng, options) {
1673
+ return new Intl.DateTimeFormat(lng, _objectSpread({}, options)).format(val);
1674
+ },
1675
+ relativetime: function relativetime(val, lng, options) {
1676
+ return new Intl.RelativeTimeFormat(lng, _objectSpread({}, options)).format(val, options.range || 'day');
1677
+ },
1678
+ list: function list(val, lng, options) {
1679
+ return new Intl.ListFormat(lng, _objectSpread({}, options)).format(val);
1680
+ }
1681
+ };
1682
+ this.init(options);
1683
+ }
1684
+
1685
+ _createClass(Formatter, [{
1686
+ key: "init",
1687
+ value: function init(services) {
1688
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
1689
+ interpolation: {}
1690
+ };
1691
+ var iOpts = options.interpolation;
1692
+ this.formatSeparator = iOpts.formatSeparator ? iOpts.formatSeparator : iOpts.formatSeparator || ',';
1693
+ }
1694
+ }, {
1695
+ key: "add",
1696
+ value: function add(name, fc) {
1697
+ this.formats[name] = fc;
1698
+ }
1699
+ }, {
1700
+ key: "format",
1701
+ value: function format(value, _format, lng, options) {
1702
+ var _this = this;
1703
+
1704
+ var formats = _format.split(this.formatSeparator);
1705
+
1706
+ var result = formats.reduce(function (mem, f) {
1707
+ var _parseFormatStr = parseFormatStr(f),
1708
+ formatName = _parseFormatStr.formatName,
1709
+ formatOptions = _parseFormatStr.formatOptions;
1710
+
1711
+ if (_this.formats[formatName]) {
1712
+ var formatted = mem;
1713
+
1714
+ try {
1715
+ var valOptions = options && options.formatParams && options.formatParams[options.interpolationkey] || {};
1716
+ var l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
1717
+ formatted = _this.formats[formatName](mem, l, _objectSpread({}, formatOptions, options, valOptions));
1718
+ } catch (error) {
1719
+ _this.logger.warn(error);
1720
+ }
1721
+
1722
+ return formatted;
1723
+ } else {
1724
+ _this.logger.warn("there was no format function for ".concat(formatName));
1725
+ }
1726
+
1727
+ return mem;
1728
+ }, value);
1729
+ return result;
1730
+ }
1731
+ }]);
1732
+
1733
+ return Formatter;
1734
+ }();
1735
+
1617
1736
  function remove(arr, what) {
1618
1737
  var found = arr.indexOf(what);
1619
1738
 
@@ -1983,7 +2102,8 @@ var I18n = function (_EventEmitter) {
1983
2102
  }
1984
2103
  }
1985
2104
 
1986
- this.options = _objectSpread({}, get(), this.options, transformOptions(options));
2105
+ var defOpts = get();
2106
+ this.options = _objectSpread({}, defOpts, this.options, transformOptions(options));
1987
2107
 
1988
2108
  if (options.keySeparator !== undefined) {
1989
2109
  this.options.userDefinedKeySeparator = options.keySeparator;
@@ -1993,9 +2113,6 @@ var I18n = function (_EventEmitter) {
1993
2113
  this.options.userDefinedNsSeparator = options.nsSeparator;
1994
2114
  }
1995
2115
 
1996
- this.format = this.options.interpolation.format;
1997
- if (!callback) callback = noop;
1998
-
1999
2116
  function createClassOnDemand(ClassOrObject) {
2000
2117
  if (!ClassOrObject) return null;
2001
2118
  if (typeof ClassOrObject === 'function') return new ClassOrObject();
@@ -2009,6 +2126,14 @@ var I18n = function (_EventEmitter) {
2009
2126
  baseLogger.init(null, this.options);
2010
2127
  }
2011
2128
 
2129
+ var formatter;
2130
+
2131
+ if (this.modules.formatter) {
2132
+ formatter = this.modules.formatter;
2133
+ } else if (typeof Intl !== 'undefined') {
2134
+ formatter = Formatter;
2135
+ }
2136
+
2012
2137
  var lu = new LanguageUtil(this.options);
2013
2138
  this.store = new ResourceStore(this.options.resources, this.options);
2014
2139
  var s = this.services;
@@ -2020,6 +2145,13 @@ var I18n = function (_EventEmitter) {
2020
2145
  compatibilityJSON: this.options.compatibilityJSON,
2021
2146
  simplifyPluralSuffix: this.options.simplifyPluralSuffix
2022
2147
  });
2148
+
2149
+ if (formatter && this.options.interpolation.format === defOpts.interpolation.format) {
2150
+ s.formatter = createClassOnDemand(formatter);
2151
+ s.formatter.init(s, this.options);
2152
+ this.options.interpolation.format = s.formatter.format.bind(s.formatter);
2153
+ }
2154
+
2023
2155
  s.interpolator = new Interpolator(this.options);
2024
2156
  s.utils = {
2025
2157
  hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
@@ -2056,6 +2188,9 @@ var I18n = function (_EventEmitter) {
2056
2188
  });
2057
2189
  }
2058
2190
 
2191
+ this.format = this.options.interpolation.format;
2192
+ if (!callback) callback = noop;
2193
+
2059
2194
  if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
2060
2195
  var codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2061
2196
  if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
@@ -2193,6 +2328,10 @@ var I18n = function (_EventEmitter) {
2193
2328
  postProcessor.addPostProcessor(module);
2194
2329
  }
2195
2330
 
2331
+ if (module.type === 'formatter') {
2332
+ this.modules.formatter = module;
2333
+ }
2334
+
2196
2335
  if (module.type === '3rdParty') {
2197
2336
  this.modules.external.push(module);
2198
2337
  }
@@ -711,7 +711,7 @@
711
711
  var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
712
712
  var namespaces = options.ns || this.options.defaultNS;
713
713
  var wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
714
- var seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !this.options.userDefinedNsSeparator && !options.keySeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
714
+ var seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !options.keySeparator && !this.options.userDefinedNsSeparator && !options.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
715
715
 
716
716
  if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
717
717
  var m = key.match(this.interpolator.nestingRegexp);
@@ -1730,6 +1730,182 @@
1730
1730
  return Interpolator;
1731
1731
  }();
1732
1732
 
1733
+ function _arrayWithHoles(arr) {
1734
+ if (Array.isArray(arr)) return arr;
1735
+ }
1736
+
1737
+ function _iterableToArrayLimit(arr, i) {
1738
+ if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
1739
+ var _arr = [];
1740
+ var _n = true;
1741
+ var _d = false;
1742
+ var _e = undefined;
1743
+
1744
+ try {
1745
+ for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
1746
+ _arr.push(_s.value);
1747
+
1748
+ if (i && _arr.length === i) break;
1749
+ }
1750
+ } catch (err) {
1751
+ _d = true;
1752
+ _e = err;
1753
+ } finally {
1754
+ try {
1755
+ if (!_n && _i["return"] != null) _i["return"]();
1756
+ } finally {
1757
+ if (_d) throw _e;
1758
+ }
1759
+ }
1760
+
1761
+ return _arr;
1762
+ }
1763
+
1764
+ function _arrayLikeToArray(arr, len) {
1765
+ if (len == null || len > arr.length) len = arr.length;
1766
+
1767
+ for (var i = 0, arr2 = new Array(len); i < len; i++) {
1768
+ arr2[i] = arr[i];
1769
+ }
1770
+
1771
+ return arr2;
1772
+ }
1773
+
1774
+ function _unsupportedIterableToArray(o, minLen) {
1775
+ if (!o) return;
1776
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
1777
+ var n = Object.prototype.toString.call(o).slice(8, -1);
1778
+ if (n === "Object" && o.constructor) n = o.constructor.name;
1779
+ if (n === "Map" || n === "Set") return Array.from(o);
1780
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
1781
+ }
1782
+
1783
+ function _nonIterableRest() {
1784
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1785
+ }
1786
+
1787
+ function _slicedToArray(arr, i) {
1788
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
1789
+ }
1790
+
1791
+ function parseFormatStr(formatStr) {
1792
+ var formatName = formatStr.toLowerCase();
1793
+ var formatOptions = {};
1794
+
1795
+ if (formatStr.indexOf('(') > -1) {
1796
+ var p = formatStr.split('(');
1797
+ formatName = p[0].toLowerCase();
1798
+ var optStr = p[1].substring(0, p[1].length - 1);
1799
+
1800
+ if (formatName === 'currency' && optStr.indexOf(':') < 0) {
1801
+ if (!formatOptions.currency) formatOptions.currency = optStr.trim();
1802
+ } else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
1803
+ if (!formatOptions.range) formatOptions.range = optStr.trim();
1804
+ } else {
1805
+ var opts = optStr.split(';');
1806
+ opts.forEach(function (opt) {
1807
+ if (!opt) return;
1808
+
1809
+ var _opt$split = opt.split(':'),
1810
+ _opt$split2 = _slicedToArray(_opt$split, 2),
1811
+ key = _opt$split2[0],
1812
+ val = _opt$split2[1];
1813
+
1814
+ if (val.trim() === 'false') formatOptions[key.trim()] = false;
1815
+ if (val.trim() === 'true') formatOptions[key.trim()] = true;
1816
+ if (!isNaN(val.trim())) formatOptions[key.trim()] = parseInt(val.trim(), 10);
1817
+ if (!formatOptions[key.trim()]) formatOptions[key.trim()] = val.trim();
1818
+ });
1819
+ }
1820
+ }
1821
+
1822
+ return {
1823
+ formatName: formatName,
1824
+ formatOptions: formatOptions
1825
+ };
1826
+ }
1827
+
1828
+ var Formatter = function () {
1829
+ function Formatter() {
1830
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1831
+
1832
+ _classCallCheck(this, Formatter);
1833
+
1834
+ this.logger = baseLogger.create('formatter');
1835
+ this.options = options;
1836
+ this.formats = {
1837
+ number: function number(val, lng, options) {
1838
+ return new Intl.NumberFormat(lng, options).format(val);
1839
+ },
1840
+ currency: function currency(val, lng, options) {
1841
+ return new Intl.NumberFormat(lng, _objectSpread({}, options, {
1842
+ style: 'currency'
1843
+ })).format(val);
1844
+ },
1845
+ datetime: function datetime(val, lng, options) {
1846
+ return new Intl.DateTimeFormat(lng, _objectSpread({}, options)).format(val);
1847
+ },
1848
+ relativetime: function relativetime(val, lng, options) {
1849
+ return new Intl.RelativeTimeFormat(lng, _objectSpread({}, options)).format(val, options.range || 'day');
1850
+ },
1851
+ list: function list(val, lng, options) {
1852
+ return new Intl.ListFormat(lng, _objectSpread({}, options)).format(val);
1853
+ }
1854
+ };
1855
+ this.init(options);
1856
+ }
1857
+
1858
+ _createClass(Formatter, [{
1859
+ key: "init",
1860
+ value: function init(services) {
1861
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
1862
+ interpolation: {}
1863
+ };
1864
+ var iOpts = options.interpolation;
1865
+ this.formatSeparator = iOpts.formatSeparator ? iOpts.formatSeparator : iOpts.formatSeparator || ',';
1866
+ }
1867
+ }, {
1868
+ key: "add",
1869
+ value: function add(name, fc) {
1870
+ this.formats[name] = fc;
1871
+ }
1872
+ }, {
1873
+ key: "format",
1874
+ value: function format(value, _format, lng, options) {
1875
+ var _this = this;
1876
+
1877
+ var formats = _format.split(this.formatSeparator);
1878
+
1879
+ var result = formats.reduce(function (mem, f) {
1880
+ var _parseFormatStr = parseFormatStr(f),
1881
+ formatName = _parseFormatStr.formatName,
1882
+ formatOptions = _parseFormatStr.formatOptions;
1883
+
1884
+ if (_this.formats[formatName]) {
1885
+ var formatted = mem;
1886
+
1887
+ try {
1888
+ var valOptions = options && options.formatParams && options.formatParams[options.interpolationkey] || {};
1889
+ var l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
1890
+ formatted = _this.formats[formatName](mem, l, _objectSpread({}, formatOptions, options, valOptions));
1891
+ } catch (error) {
1892
+ _this.logger.warn(error);
1893
+ }
1894
+
1895
+ return formatted;
1896
+ } else {
1897
+ _this.logger.warn("there was no format function for ".concat(formatName));
1898
+ }
1899
+
1900
+ return mem;
1901
+ }, value);
1902
+ return result;
1903
+ }
1904
+ }]);
1905
+
1906
+ return Formatter;
1907
+ }();
1908
+
1733
1909
  function remove(arr, what) {
1734
1910
  var found = arr.indexOf(what);
1735
1911
 
@@ -2099,7 +2275,8 @@
2099
2275
  }
2100
2276
  }
2101
2277
 
2102
- this.options = _objectSpread({}, get(), this.options, transformOptions(options));
2278
+ var defOpts = get();
2279
+ this.options = _objectSpread({}, defOpts, this.options, transformOptions(options));
2103
2280
 
2104
2281
  if (options.keySeparator !== undefined) {
2105
2282
  this.options.userDefinedKeySeparator = options.keySeparator;
@@ -2109,9 +2286,6 @@
2109
2286
  this.options.userDefinedNsSeparator = options.nsSeparator;
2110
2287
  }
2111
2288
 
2112
- this.format = this.options.interpolation.format;
2113
- if (!callback) callback = noop;
2114
-
2115
2289
  function createClassOnDemand(ClassOrObject) {
2116
2290
  if (!ClassOrObject) return null;
2117
2291
  if (typeof ClassOrObject === 'function') return new ClassOrObject();
@@ -2125,6 +2299,14 @@
2125
2299
  baseLogger.init(null, this.options);
2126
2300
  }
2127
2301
 
2302
+ var formatter;
2303
+
2304
+ if (this.modules.formatter) {
2305
+ formatter = this.modules.formatter;
2306
+ } else if (typeof Intl !== 'undefined') {
2307
+ formatter = Formatter;
2308
+ }
2309
+
2128
2310
  var lu = new LanguageUtil(this.options);
2129
2311
  this.store = new ResourceStore(this.options.resources, this.options);
2130
2312
  var s = this.services;
@@ -2136,6 +2318,13 @@
2136
2318
  compatibilityJSON: this.options.compatibilityJSON,
2137
2319
  simplifyPluralSuffix: this.options.simplifyPluralSuffix
2138
2320
  });
2321
+
2322
+ if (formatter && this.options.interpolation.format === defOpts.interpolation.format) {
2323
+ s.formatter = createClassOnDemand(formatter);
2324
+ s.formatter.init(s, this.options);
2325
+ this.options.interpolation.format = s.formatter.format.bind(s.formatter);
2326
+ }
2327
+
2139
2328
  s.interpolator = new Interpolator(this.options);
2140
2329
  s.utils = {
2141
2330
  hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
@@ -2172,6 +2361,9 @@
2172
2361
  });
2173
2362
  }
2174
2363
 
2364
+ this.format = this.options.interpolation.format;
2365
+ if (!callback) callback = noop;
2366
+
2175
2367
  if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
2176
2368
  var codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2177
2369
  if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
@@ -2309,6 +2501,10 @@
2309
2501
  postProcessor.addPostProcessor(module);
2310
2502
  }
2311
2503
 
2504
+ if (module.type === 'formatter') {
2505
+ this.modules.formatter = module;
2506
+ }
2507
+
2312
2508
  if (module.type === '3rdParty') {
2313
2509
  this.modules.external.push(module);
2314
2510
  }