next-i18next 14.0.3 → 15.0.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/.size-limit.cjs CHANGED
@@ -21,8 +21,8 @@ const getSimpleExamplePageLimits = () => {
21
21
  pages: {
22
22
  '/': '85kb',
23
23
  '/404': '90kb',
24
- '/_app': '100kb',
25
- '/_error': '80Kb',
24
+ '/_app': '105kb',
25
+ '/_error': '82Kb',
26
26
  '/second-page': '85Kb',
27
27
  },
28
28
  }
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  If you are using next-i18next *(pages directory)* in production and like to unleash some super powers, you may have a look at [this blog post](https://locize.com/blog/next-i18next/).
11
11
  [![](https://locize.com/blog/next-i18next/next-i18next.jpg)](https://locize.com/blog/next-i18next/)
12
12
 
13
- If you're using Next.js 13 with app directory, there is no need for next-i18next, you can directly use i18next and react-i18next, like described [in this blog post](https://locize.com/blog/next-13-app-dir-i18n/).
13
+ If you're using Next.js 13/14 with app directory, there is no need for next-i18next, you can directly use i18next and react-i18next, like described [in this blog post](https://locize.com/blog/next-13-app-dir-i18n/).
14
14
  [![](https://locize.com/blog/next-13-app-dir-i18n/next-13-app-dir-i18n.jpg)](https://locize.com/blog/next-13-app-dir-i18n/)
15
15
 
16
16
  ## What is this?
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
 
3
- require("core-js/modules/es.object.keys.js");
4
3
  require("core-js/modules/es.symbol.js");
5
4
  require("core-js/modules/es.array.filter.js");
6
5
  require("core-js/modules/es.object.to-string.js");
@@ -40,31 +39,34 @@ Object.defineProperty(exports, "withTranslation", {
40
39
  });
41
40
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
42
41
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
42
+ require("core-js/modules/es.object.keys.js");
43
43
  var _react = _interopRequireWildcard(require("react"));
44
44
  var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
45
45
  var _reactI18next = require("react-i18next");
46
46
  var _createConfig = require("./config/createConfig");
47
47
  var _createClient = _interopRequireDefault(require("./createClient"));
48
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
49
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
48
+ var _utils = require("./utils");
49
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
50
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
50
51
  var __jsx = _react["default"].createElement;
51
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
52
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
53
- var globalI18n = null;
54
- exports.globalI18n = globalI18n;
55
- var appWithTranslation = function appWithTranslation(WrappedComponent) {
52
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
53
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
54
+ var globalI18n = exports.globalI18n = null;
55
+ var appWithTranslation = exports.appWithTranslation = function appWithTranslation(WrappedComponent) {
56
56
  var configOverride = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
57
57
  var AppWithTranslation = function AppWithTranslation(props) {
58
58
  var _nextI18Next$initialL, _props$router;
59
59
  var _ref = props.pageProps || {},
60
60
  _nextI18Next = _ref._nextI18Next; // pageProps may be undefined on strange setups, i.e. https://github.com/i18next/next-i18next/issues/2109
61
- var locale = (_nextI18Next$initialL = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.initialLocale) !== null && _nextI18Next$initialL !== void 0 ? _nextI18Next$initialL : props === null || props === void 0 ? void 0 : (_props$router = props.router) === null || _props$router === void 0 ? void 0 : _props$router.locale;
61
+ var locale = (_nextI18Next$initialL = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.initialLocale) !== null && _nextI18Next$initialL !== void 0 ? _nextI18Next$initialL : props === null || props === void 0 || (_props$router = props.router) === null || _props$router === void 0 ? void 0 : _props$router.locale;
62
62
  var ns = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.ns;
63
+ var instanceRef = (0, _react.useRef)(null);
63
64
 
64
- // Memoize the instance and only re-initialize when either:
65
- // 1. The route changes (non-shallowly)
66
- // 2. Router locale changes
67
- // 3. UserConfig override changes
65
+ /**
66
+ * Memoize i18n instance and reuse it rather than creating new instance.
67
+ * When the locale or resources are changed after instance was created,
68
+ * we will update the instance by calling addResourceBundle method on it.
69
+ */
68
70
  var i18n = (0, _react.useMemo)(function () {
69
71
  var _userConfig$i18n;
70
72
  if (!_nextI18Next && !configOverride) return null;
@@ -82,16 +84,39 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
82
84
  initialI18nStore = _ref2.initialI18nStore;
83
85
  var resources = configOverride !== null && configOverride !== void 0 && configOverride.resources ? configOverride.resources : initialI18nStore;
84
86
  if (!locale) locale = userConfig.i18n.defaultLocale;
85
- var instance = (0, _createClient["default"])(_objectSpread(_objectSpread({}, (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
86
- lng: locale
87
- }))), {}, {
88
- lng: locale,
89
- ns: ns,
90
- resources: resources
91
- })).i18n;
92
- exports.globalI18n = globalI18n = instance;
87
+ var instance = instanceRef.current;
88
+ if (instance) {
89
+ if (resources) {
90
+ for (var _i = 0, _Object$keys = Object.keys(resources); _i < _Object$keys.length; _i++) {
91
+ var _locale = _Object$keys[_i];
92
+ for (var _i2 = 0, _Object$keys2 = Object.keys(resources[_locale]); _i2 < _Object$keys2.length; _i2++) {
93
+ var _ns = _Object$keys2[_i2];
94
+ instance.addResourceBundle(_locale, _ns, resources[_locale][_ns], true, true);
95
+ }
96
+ }
97
+ }
98
+ } else {
99
+ instance = (0, _createClient["default"])(_objectSpread(_objectSpread({}, (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
100
+ lng: locale
101
+ }))), {}, {
102
+ lng: locale,
103
+ ns: ns,
104
+ resources: resources
105
+ })).i18n;
106
+ exports.globalI18n = globalI18n = instance;
107
+ instanceRef.current = instance;
108
+ }
93
109
  return instance;
94
- }, [_nextI18Next, locale, configOverride, ns]);
110
+ }, [_nextI18Next, locale, ns]);
111
+
112
+ /**
113
+ * Since calling changeLanguage method on existing i18n instance cause state update in react,
114
+ * we need to call the method in `useLayoutEffect` to prevent state update in render phase.
115
+ */
116
+ (0, _utils.useIsomorphicLayoutEffect)(function () {
117
+ if (!i18n || !locale) return;
118
+ i18n.changeLanguage(locale);
119
+ }, [i18n, locale]);
95
120
  return i18n !== null ? __jsx(_reactI18next.I18nextProvider, {
96
121
  i18n: i18n
97
122
  }, __jsx(WrappedComponent, props)) : __jsx(WrappedComponent, (0, _extends2["default"])({
@@ -99,5 +124,4 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
99
124
  }, props));
100
125
  };
101
126
  return (0, _hoistNonReactStatics["default"])(AppWithTranslation, WrappedComponent);
102
- };
103
- exports.appWithTranslation = appWithTranslation;
127
+ };
@@ -8,6 +8,8 @@ require("core-js/modules/es.object.get-own-property-descriptors.js");
8
8
  require("core-js/modules/es.object.define-properties.js");
9
9
  require("core-js/modules/es.object.define-property.js");
10
10
  require("core-js/modules/es.array.slice.js");
11
+ require("core-js/modules/es.date.to-string.js");
12
+ require("core-js/modules/es.regexp.to-string.js");
11
13
  require("core-js/modules/es.function.name.js");
12
14
  require("core-js/modules/es.array.from.js");
13
15
  require("core-js/modules/es.string.iterator.js");
@@ -46,11 +48,11 @@ var _excluded = ["i18n"],
46
48
  _excluded2 = ["i18n"];
47
49
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
48
50
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
49
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
50
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
51
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
51
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
52
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
53
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
52
54
  var deepMergeObjects = ['backend', 'detection'];
53
- var createConfig = function createConfig(userConfig) {
55
+ var createConfig = exports.createConfig = function createConfig(userConfig) {
54
56
  var _userConfig$interpola, _userConfig$interpola2, _userConfig$use;
55
57
  if (typeof (userConfig === null || userConfig === void 0 ? void 0 : userConfig.lng) !== 'string') {
56
58
  throw new Error('config.lng was not passed into createConfig');
@@ -87,8 +89,8 @@ var createConfig = function createConfig(userConfig) {
87
89
  combinedConfig.fallbackLng = _locales[0];
88
90
  }
89
91
  }
90
- var userPrefix = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$interpola = userConfig.interpolation) === null || _userConfig$interpola === void 0 ? void 0 : _userConfig$interpola.prefix;
91
- var userSuffix = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$interpola2 = userConfig.interpolation) === null || _userConfig$interpola2 === void 0 ? void 0 : _userConfig$interpola2.suffix;
92
+ var userPrefix = userConfig === null || userConfig === void 0 || (_userConfig$interpola = userConfig.interpolation) === null || _userConfig$interpola === void 0 ? void 0 : _userConfig$interpola.prefix;
93
+ var userSuffix = userConfig === null || userConfig === void 0 || (_userConfig$interpola2 = userConfig.interpolation) === null || _userConfig$interpola2 === void 0 ? void 0 : _userConfig$interpola2.suffix;
92
94
  var prefix = userPrefix !== null && userPrefix !== void 0 ? userPrefix : '{{';
93
95
  var suffix = userSuffix !== null && userSuffix !== void 0 ? userSuffix : '}}';
94
96
  if (typeof (userConfig === null || userConfig === void 0 ? void 0 : userConfig.localeStructure) !== 'string' && (userPrefix || userSuffix)) {
@@ -128,7 +130,7 @@ var createConfig = function createConfig(userConfig) {
128
130
  throw new Error('If nonExplicitSupportedLngs is true, no functions are allowed for fallbackLng');
129
131
  }
130
132
  }
131
- var hasCustomBackend = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$use = userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
133
+ var hasCustomBackend = userConfig === null || userConfig === void 0 || (_userConfig$use = userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
132
134
  return b.type === 'backend';
133
135
  });
134
136
  if (!process.browser && typeof window === 'undefined') {
@@ -274,5 +276,4 @@ var createConfig = function createConfig(userConfig) {
274
276
  }
275
277
  });
276
278
  return combinedConfig;
277
- };
278
- exports.createConfig = createConfig;
279
+ };
@@ -11,7 +11,7 @@ var DEFAULT_NAMESPACE = 'common';
11
11
  var LOCALE_PATH = './public/locales';
12
12
  var LOCALE_STRUCTURE = '{{lng}}/{{ns}}';
13
13
  var LOCALE_EXTENSION = 'json';
14
- var defaultConfig = {
14
+ var defaultConfig = exports.defaultConfig = {
15
15
  defaultNS: DEFAULT_NAMESPACE,
16
16
  errorStackTraceLimit: 0,
17
17
  i18n: {
@@ -34,5 +34,4 @@ var defaultConfig = {
34
34
  reloadOnPrerender: false,
35
35
  serializeConfig: true,
36
36
  use: []
37
- };
38
- exports.defaultConfig = defaultConfig;
37
+ };
@@ -11,13 +11,13 @@ require("core-js/modules/es.object.to-string.js");
11
11
  require("core-js/modules/web.dom-collections.for-each.js");
12
12
  require("core-js/modules/es.promise.js");
13
13
  var _i18next = _interopRequireDefault(require("i18next"));
14
- var _default = function _default(config) {
14
+ var _default = exports["default"] = function _default(config) {
15
15
  if (config.ns === undefined) config.ns = [];
16
16
  var instance = _i18next["default"].createInstance(config);
17
17
  var initPromise;
18
18
  if (!instance.isInitialized) {
19
19
  var _config$use;
20
- config === null || config === void 0 ? void 0 : (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.forEach(function (x) {
20
+ config === null || config === void 0 || (_config$use = config.use) === null || _config$use === void 0 || _config$use.forEach(function (x) {
21
21
  return instance.use(x);
22
22
  });
23
23
  if (typeof config.onPreInitI18next === 'function') {
@@ -32,6 +32,5 @@ var _default = function _default(config) {
32
32
  initPromise: initPromise
33
33
  };
34
34
  };
35
- exports["default"] = _default;
36
35
  module.exports = exports.default;
37
36
  module.exports.default = exports.default;
@@ -20,10 +20,10 @@ require("core-js/modules/es.promise.js");
20
20
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
21
21
  var _i18next = _interopRequireDefault(require("i18next"));
22
22
  var _i18nextFsBackend = _interopRequireDefault(require("i18next-fs-backend"));
23
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
24
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
24
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
25
25
  var globalInstance;
26
- var _default = function _default(config) {
26
+ var _default = exports["default"] = function _default(config) {
27
27
  if (config.ns === undefined) config.ns = [];
28
28
  var instance;
29
29
  if (!globalInstance) {
@@ -39,13 +39,13 @@ var _default = function _default(config) {
39
39
  var initPromise;
40
40
  if (!instance.isInitialized) {
41
41
  var _config$use, _config$use2;
42
- var hasCustomBackend = config === null || config === void 0 ? void 0 : (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.some(function (b) {
42
+ var hasCustomBackend = config === null || config === void 0 || (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.some(function (b) {
43
43
  return b.type === 'backend';
44
44
  });
45
45
  if (!hasCustomBackend) {
46
46
  instance.use(_i18nextFsBackend["default"]);
47
47
  }
48
- config === null || config === void 0 ? void 0 : (_config$use2 = config.use) === null || _config$use2 === void 0 ? void 0 : _config$use2.forEach(function (x) {
48
+ config === null || config === void 0 || (_config$use2 = config.use) === null || _config$use2 === void 0 || _config$use2.forEach(function (x) {
49
49
  return instance.use(x);
50
50
  });
51
51
  if (typeof config.onPreInitI18next === 'function') {
@@ -60,6 +60,5 @@ var _default = function _default(config) {
60
60
  initPromise: initPromise
61
61
  };
62
62
  };
63
- exports["default"] = _default;
64
63
  module.exports = exports.default;
65
64
  module.exports.default = exports.default;
@@ -38,10 +38,10 @@ var _createConfig = require("./config/createConfig");
38
38
  var _node = _interopRequireDefault(require("./createClient/node"));
39
39
  var _appWithTranslation = require("./appWithTranslation");
40
40
  var _utils = require("./utils");
41
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
42
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
43
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
44
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
41
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
42
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
43
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
44
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
45
45
  var DEFAULT_CONFIG_PATH = './next-i18next.config.js';
46
46
 
47
47
  /**
@@ -53,9 +53,9 @@ var DEFAULT_CONFIG_PATH = './next-i18next.config.js';
53
53
  if (process.env.I18NEXT_DEFAULT_CONFIG_PATH) {
54
54
  DEFAULT_CONFIG_PATH = process.env.I18NEXT_DEFAULT_CONFIG_PATH;
55
55
  }
56
- var serverSideTranslations = /*#__PURE__*/function () {
56
+ var serverSideTranslations = exports.serverSideTranslations = /*#__PURE__*/function () {
57
57
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(initialLocale) {
58
- var _userConfig, _userConfig$use;
58
+ var _userConfig;
59
59
  var namespacesRequired,
60
60
  configOverride,
61
61
  extraLocales,
@@ -75,110 +75,111 @@ var serverSideTranslations = /*#__PURE__*/function () {
75
75
  namespacesByLocale,
76
76
  _args = arguments;
77
77
  return _regenerator["default"].wrap(function _callee$(_context) {
78
- while (1) {
79
- switch (_context.prev = _context.next) {
80
- case 0:
81
- namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
82
- configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
83
- extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
84
- if (!(typeof initialLocale !== 'string')) {
85
- _context.next = 5;
86
- break;
87
- }
88
- throw new Error('Initial locale argument was not passed into serverSideTranslations');
89
- case 5:
90
- userConfig = configOverride;
91
- configPath = _path["default"].resolve(DEFAULT_CONFIG_PATH);
92
- if (!(!userConfig && _fs["default"].existsSync(configPath))) {
93
- _context.next = 11;
94
- break;
95
- }
96
- _context.next = 10;
97
- return Promise.resolve("".concat(configPath)).then(function (s) {
78
+ while (1) switch (_context.prev = _context.next) {
79
+ case 0:
80
+ namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
81
+ configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
82
+ extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
83
+ if (!(typeof initialLocale !== 'string')) {
84
+ _context.next = 5;
85
+ break;
86
+ }
87
+ throw new Error('Initial locale argument was not passed into serverSideTranslations');
88
+ case 5:
89
+ userConfig = configOverride;
90
+ configPath = _path["default"].resolve(DEFAULT_CONFIG_PATH);
91
+ if (!(!userConfig && _fs["default"].existsSync(configPath))) {
92
+ _context.next = 11;
93
+ break;
94
+ }
95
+ _context.next = 10;
96
+ return function (specifier) {
97
+ return new Promise(function (r) {
98
+ return r("".concat(specifier));
99
+ }).then(function (s) {
98
100
  return _interopRequireWildcard(require(s));
99
101
  });
100
- case 10:
101
- userConfig = _context.sent;
102
- case 11:
103
- if (!(userConfig === null)) {
104
- _context.next = 13;
105
- break;
106
- }
107
- throw new Error("next-i18next was unable to find a user config at ".concat(configPath));
108
- case 13:
109
- config = (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
110
- lng: initialLocale
111
- }));
112
- localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
113
- if (!reloadOnPrerender) {
114
- _context.next = 18;
115
- break;
116
- }
102
+ }(configPath);
103
+ case 10:
104
+ userConfig = _context.sent;
105
+ case 11:
106
+ if (!(userConfig === null)) {
107
+ _context.next = 13;
108
+ break;
109
+ }
110
+ throw new Error("next-i18next was unable to find a user config at ".concat(configPath));
111
+ case 13:
112
+ config = (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
113
+ lng: initialLocale
114
+ }));
115
+ localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
116
+ if (!reloadOnPrerender) {
117
117
  _context.next = 18;
118
- return _appWithTranslation.globalI18n === null || _appWithTranslation.globalI18n === void 0 ? void 0 : _appWithTranslation.globalI18n.reloadResources();
119
- case 18:
120
- _createClient = (0, _node["default"])(_objectSpread(_objectSpread({}, config), {}, {
121
- lng: initialLocale
122
- })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
123
- _context.next = 21;
124
- return initPromise;
125
- case 21:
126
- hasCustomBackend = (_userConfig = userConfig) === null || _userConfig === void 0 ? void 0 : (_userConfig$use = _userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
127
- return b.type === 'backend';
128
- });
129
- if (!(hasCustomBackend && namespacesRequired)) {
130
- _context.next = 25;
131
- break;
132
- }
118
+ break;
119
+ }
120
+ _context.next = 18;
121
+ return _appWithTranslation.globalI18n === null || _appWithTranslation.globalI18n === void 0 ? void 0 : _appWithTranslation.globalI18n.reloadResources();
122
+ case 18:
123
+ _createClient = (0, _node["default"])(_objectSpread(_objectSpread({}, config), {}, {
124
+ lng: initialLocale
125
+ })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
126
+ _context.next = 21;
127
+ return initPromise;
128
+ case 21:
129
+ hasCustomBackend = (_userConfig = userConfig) === null || _userConfig === void 0 || (_userConfig = _userConfig.use) === null || _userConfig === void 0 ? void 0 : _userConfig.some(function (b) {
130
+ return b.type === 'backend';
131
+ });
132
+ if (!(hasCustomBackend && namespacesRequired)) {
133
133
  _context.next = 25;
134
- return i18n.loadNamespaces(Array.isArray(namespacesRequired) ? namespacesRequired : namespacesRequired);
135
- case 25:
136
- initialI18nStore = (0, _defineProperty2["default"])({}, initialLocale, {});
137
- (0, _utils.getFallbackForLng)(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
138
- initialI18nStore[lng] = {};
139
- });
140
- if (Array.isArray(namespacesRequired)) {
141
- _context.next = 33;
142
- break;
134
+ break;
135
+ }
136
+ _context.next = 25;
137
+ return i18n.loadNamespaces(Array.isArray(namespacesRequired) ? namespacesRequired : namespacesRequired);
138
+ case 25:
139
+ initialI18nStore = (0, _defineProperty2["default"])({}, initialLocale, {});
140
+ (0, _utils.getFallbackForLng)(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
141
+ initialI18nStore[lng] = {};
142
+ });
143
+ if (Array.isArray(namespacesRequired)) {
144
+ _context.next = 33;
145
+ break;
146
+ }
147
+ if (!(typeof localePath === 'function')) {
148
+ _context.next = 30;
149
+ break;
150
+ }
151
+ throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
152
+ case 30:
153
+ getLocaleNamespaces = function getLocaleNamespaces(path) {
154
+ return _fs["default"].existsSync(path) ? _fs["default"].readdirSync(path).map(function (file) {
155
+ return file.replace(".".concat(localeExtension), '');
156
+ }) : [];
157
+ };
158
+ namespacesByLocale = Object.keys(initialI18nStore).map(function (locale) {
159
+ return getLocaleNamespaces(_path["default"].resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
160
+ }).flat();
161
+ namespacesRequired = (0, _utils.unique)(namespacesByLocale);
162
+ case 33:
163
+ namespacesRequired.forEach(function (ns) {
164
+ for (var locale in initialI18nStore) {
165
+ initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
143
166
  }
144
- if (!(typeof localePath === 'function')) {
145
- _context.next = 30;
146
- break;
167
+ });
168
+ return _context.abrupt("return", {
169
+ _nextI18Next: {
170
+ initialI18nStore: initialI18nStore,
171
+ initialLocale: initialLocale,
172
+ ns: namespacesRequired,
173
+ userConfig: config.serializeConfig ? userConfig : null
147
174
  }
148
- throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
149
- case 30:
150
- getLocaleNamespaces = function getLocaleNamespaces(path) {
151
- return _fs["default"].existsSync(path) ? _fs["default"].readdirSync(path).map(function (file) {
152
- return file.replace(".".concat(localeExtension), '');
153
- }) : [];
154
- };
155
- namespacesByLocale = Object.keys(initialI18nStore).map(function (locale) {
156
- return getLocaleNamespaces(_path["default"].resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
157
- }).flat();
158
- namespacesRequired = (0, _utils.unique)(namespacesByLocale);
159
- case 33:
160
- namespacesRequired.forEach(function (ns) {
161
- for (var locale in initialI18nStore) {
162
- initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
163
- }
164
- });
165
- return _context.abrupt("return", {
166
- _nextI18Next: {
167
- initialI18nStore: initialI18nStore,
168
- initialLocale: initialLocale,
169
- ns: namespacesRequired,
170
- userConfig: config.serializeConfig ? userConfig : null
171
- }
172
- });
173
- case 35:
174
- case "end":
175
- return _context.stop();
176
- }
175
+ });
176
+ case 35:
177
+ case "end":
178
+ return _context.stop();
177
179
  }
178
180
  }, _callee);
179
181
  }));
180
182
  return function serverSideTranslations(_x) {
181
183
  return _ref.apply(this, arguments);
182
184
  };
183
- }();
184
- exports.serverSideTranslations = serverSideTranslations;
185
+ }();
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.unique = exports.getFallbackForLng = void 0;
8
+ exports.useIsomorphicLayoutEffect = exports.unique = exports.getFallbackForLng = void 0;
9
9
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
10
  var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11
11
  require("core-js/modules/es.array.is-array.js");
@@ -16,7 +16,8 @@ require("core-js/modules/es.array.iterator.js");
16
16
  require("core-js/modules/es.object.to-string.js");
17
17
  require("core-js/modules/es.set.js");
18
18
  require("core-js/modules/web.dom-collections.iterator.js");
19
- var getFallbackForLng = function getFallbackForLng(lng, fallbackLng) {
19
+ var _react = require("react");
20
+ var getFallbackForLng = exports.getFallbackForLng = function getFallbackForLng(lng, fallbackLng) {
20
21
  if (typeof fallbackLng === 'string') {
21
22
  return [fallbackLng];
22
23
  }
@@ -33,8 +34,15 @@ var getFallbackForLng = function getFallbackForLng(lng, fallbackLng) {
33
34
  }
34
35
  return [];
35
36
  };
36
- exports.getFallbackForLng = getFallbackForLng;
37
- var unique = function unique(list) {
37
+ var unique = exports.unique = function unique(list) {
38
38
  return Array.from(new Set(list));
39
39
  };
40
- exports.unique = unique;
40
+
41
+ /**
42
+ * This hook behaves like `useLayoutEffect` on the client,
43
+ * and `useEffect` on the server(no effect).
44
+ *
45
+ * Since using `useLayoutEffect` on the server cause warning messages in nextjs,
46
+ * this hook is workaround for that.
47
+ */
48
+ var useIsomorphicLayoutEffect = exports.useIsomorphicLayoutEffect = typeof window !== 'undefined' ? _react.useLayoutEffect : _react.useEffect;
@@ -1,9 +1,10 @@
1
1
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
- import React, { useMemo } from 'react';
2
+ import React, { useMemo, useRef } from 'react';
3
3
  import hoistNonReactStatics from 'hoist-non-react-statics';
4
4
  import { I18nextProvider } from 'react-i18next';
5
5
  import { createConfig } from './config/createConfig';
6
6
  import createClient from './createClient';
7
+ import { useIsomorphicLayoutEffect } from './utils';
7
8
  export { Trans, useTranslation, withTranslation } from 'react-i18next';
8
9
  export let globalI18n = null;
9
10
  export const appWithTranslation = (WrappedComponent, configOverride = null) => {
@@ -13,11 +14,13 @@ export const appWithTranslation = (WrappedComponent, configOverride = null) => {
13
14
  } = props.pageProps || {}; // pageProps may be undefined on strange setups, i.e. https://github.com/i18next/next-i18next/issues/2109
14
15
  let locale = _nextI18Next?.initialLocale ?? props?.router?.locale;
15
16
  const ns = _nextI18Next?.ns;
17
+ const instanceRef = useRef(null);
16
18
 
17
- // Memoize the instance and only re-initialize when either:
18
- // 1. The route changes (non-shallowly)
19
- // 2. Router locale changes
20
- // 3. UserConfig override changes
19
+ /**
20
+ * Memoize i18n instance and reuse it rather than creating new instance.
21
+ * When the locale or resources are changed after instance was created,
22
+ * we will update the instance by calling addResourceBundle method on it.
23
+ */
21
24
  const i18n = useMemo(() => {
22
25
  if (!_nextI18Next && !configOverride) return null;
23
26
  const userConfig = configOverride ?? _nextI18Next?.userConfig;
@@ -35,18 +38,39 @@ export const appWithTranslation = (WrappedComponent, configOverride = null) => {
35
38
  } = _nextI18Next || {};
36
39
  const resources = configOverride?.resources ? configOverride.resources : initialI18nStore;
37
40
  if (!locale) locale = userConfig.i18n.defaultLocale;
38
- const instance = createClient({
39
- ...createConfig({
40
- ...userConfig,
41
- lng: locale
42
- }),
43
- lng: locale,
44
- ns,
45
- resources
46
- }).i18n;
47
- globalI18n = instance;
41
+ let instance = instanceRef.current;
42
+ if (instance) {
43
+ if (resources) {
44
+ for (const locale of Object.keys(resources)) {
45
+ for (const ns of Object.keys(resources[locale])) {
46
+ instance.addResourceBundle(locale, ns, resources[locale][ns], true, true);
47
+ }
48
+ }
49
+ }
50
+ } else {
51
+ instance = createClient({
52
+ ...createConfig({
53
+ ...userConfig,
54
+ lng: locale
55
+ }),
56
+ lng: locale,
57
+ ns,
58
+ resources
59
+ }).i18n;
60
+ globalI18n = instance;
61
+ instanceRef.current = instance;
62
+ }
48
63
  return instance;
49
- }, [_nextI18Next, locale, configOverride, ns]);
64
+ }, [_nextI18Next, locale, ns]);
65
+
66
+ /**
67
+ * Since calling changeLanguage method on existing i18n instance cause state update in react,
68
+ * we need to call the method in `useLayoutEffect` to prevent state update in render phase.
69
+ */
70
+ useIsomorphicLayoutEffect(() => {
71
+ if (!i18n || !locale) return;
72
+ i18n.changeLanguage(locale);
73
+ }, [i18n, locale]);
50
74
  return i18n !== null ? /*#__PURE__*/React.createElement(I18nextProvider, {
51
75
  i18n: i18n
52
76
  }, /*#__PURE__*/React.createElement(WrappedComponent, props)) : /*#__PURE__*/React.createElement(WrappedComponent, _extends({
package/dist/es/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { useLayoutEffect, useEffect } from 'react';
1
2
  export const getFallbackForLng = (lng, fallbackLng) => {
2
3
  if (typeof fallbackLng === 'string') {
3
4
  return [fallbackLng];
@@ -15,4 +16,13 @@ export const getFallbackForLng = (lng, fallbackLng) => {
15
16
  }
16
17
  return [];
17
18
  };
18
- export const unique = list => Array.from(new Set(list));
19
+ export const unique = list => Array.from(new Set(list));
20
+
21
+ /**
22
+ * This hook behaves like `useLayoutEffect` on the client,
23
+ * and `useEffect` on the server(no effect).
24
+ *
25
+ * Since using `useLayoutEffect` on the server cause warning messages in nextjs,
26
+ * this hook is workaround for that.
27
+ */
28
+ export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
@@ -1,13 +1,14 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
2
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
3
  var __jsx = React.createElement;
4
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
- import React, { useMemo } from 'react';
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
+ import React, { useMemo, useRef } from 'react';
7
7
  import hoistNonReactStatics from 'hoist-non-react-statics';
8
8
  import { I18nextProvider } from 'react-i18next';
9
9
  import { createConfig } from './config/createConfig';
10
10
  import createClient from './createClient';
11
+ import { useIsomorphicLayoutEffect } from './utils';
11
12
  export { Trans, useTranslation, withTranslation } from 'react-i18next';
12
13
  export var globalI18n = null;
13
14
  export var appWithTranslation = function appWithTranslation(WrappedComponent) {
@@ -16,13 +17,15 @@ export var appWithTranslation = function appWithTranslation(WrappedComponent) {
16
17
  var _nextI18Next$initialL, _props$router;
17
18
  var _ref = props.pageProps || {},
18
19
  _nextI18Next = _ref._nextI18Next; // pageProps may be undefined on strange setups, i.e. https://github.com/i18next/next-i18next/issues/2109
19
- var locale = (_nextI18Next$initialL = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.initialLocale) !== null && _nextI18Next$initialL !== void 0 ? _nextI18Next$initialL : props === null || props === void 0 ? void 0 : (_props$router = props.router) === null || _props$router === void 0 ? void 0 : _props$router.locale;
20
+ var locale = (_nextI18Next$initialL = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.initialLocale) !== null && _nextI18Next$initialL !== void 0 ? _nextI18Next$initialL : props === null || props === void 0 || (_props$router = props.router) === null || _props$router === void 0 ? void 0 : _props$router.locale;
20
21
  var ns = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.ns;
22
+ var instanceRef = useRef(null);
21
23
 
22
- // Memoize the instance and only re-initialize when either:
23
- // 1. The route changes (non-shallowly)
24
- // 2. Router locale changes
25
- // 3. UserConfig override changes
24
+ /**
25
+ * Memoize i18n instance and reuse it rather than creating new instance.
26
+ * When the locale or resources are changed after instance was created,
27
+ * we will update the instance by calling addResourceBundle method on it.
28
+ */
26
29
  var i18n = useMemo(function () {
27
30
  var _userConfig$i18n;
28
31
  if (!_nextI18Next && !configOverride) return null;
@@ -40,16 +43,39 @@ export var appWithTranslation = function appWithTranslation(WrappedComponent) {
40
43
  initialI18nStore = _ref2.initialI18nStore;
41
44
  var resources = configOverride !== null && configOverride !== void 0 && configOverride.resources ? configOverride.resources : initialI18nStore;
42
45
  if (!locale) locale = userConfig.i18n.defaultLocale;
43
- var instance = createClient(_objectSpread(_objectSpread({}, createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
44
- lng: locale
45
- }))), {}, {
46
- lng: locale,
47
- ns: ns,
48
- resources: resources
49
- })).i18n;
50
- globalI18n = instance;
46
+ var instance = instanceRef.current;
47
+ if (instance) {
48
+ if (resources) {
49
+ for (var _i = 0, _Object$keys = Object.keys(resources); _i < _Object$keys.length; _i++) {
50
+ var _locale = _Object$keys[_i];
51
+ for (var _i2 = 0, _Object$keys2 = Object.keys(resources[_locale]); _i2 < _Object$keys2.length; _i2++) {
52
+ var _ns = _Object$keys2[_i2];
53
+ instance.addResourceBundle(_locale, _ns, resources[_locale][_ns], true, true);
54
+ }
55
+ }
56
+ }
57
+ } else {
58
+ instance = createClient(_objectSpread(_objectSpread({}, createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
59
+ lng: locale
60
+ }))), {}, {
61
+ lng: locale,
62
+ ns: ns,
63
+ resources: resources
64
+ })).i18n;
65
+ globalI18n = instance;
66
+ instanceRef.current = instance;
67
+ }
51
68
  return instance;
52
- }, [_nextI18Next, locale, configOverride, ns]);
69
+ }, [_nextI18Next, locale, ns]);
70
+
71
+ /**
72
+ * Since calling changeLanguage method on existing i18n instance cause state update in react,
73
+ * we need to call the method in `useLayoutEffect` to prevent state update in render phase.
74
+ */
75
+ useIsomorphicLayoutEffect(function () {
76
+ if (!i18n || !locale) return;
77
+ i18n.changeLanguage(locale);
78
+ }, [i18n, locale]);
53
79
  return i18n !== null ? __jsx(I18nextProvider, {
54
80
  i18n: i18n
55
81
  }, __jsx(WrappedComponent, props)) : __jsx(WrappedComponent, _extends({
@@ -7,9 +7,9 @@ var _excluded = ["i18n"],
7
7
  _excluded2 = ["i18n"];
8
8
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
9
9
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
10
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
11
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
10
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
11
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
12
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
13
13
  import { defaultConfig } from './defaultConfig';
14
14
  import { getFallbackForLng, unique } from '../utils';
15
15
  var deepMergeObjects = ['backend', 'detection'];
@@ -50,8 +50,8 @@ export var createConfig = function createConfig(userConfig) {
50
50
  combinedConfig.fallbackLng = _locales[0];
51
51
  }
52
52
  }
53
- var userPrefix = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$interpola = userConfig.interpolation) === null || _userConfig$interpola === void 0 ? void 0 : _userConfig$interpola.prefix;
54
- var userSuffix = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$interpola2 = userConfig.interpolation) === null || _userConfig$interpola2 === void 0 ? void 0 : _userConfig$interpola2.suffix;
53
+ var userPrefix = userConfig === null || userConfig === void 0 || (_userConfig$interpola = userConfig.interpolation) === null || _userConfig$interpola === void 0 ? void 0 : _userConfig$interpola.prefix;
54
+ var userSuffix = userConfig === null || userConfig === void 0 || (_userConfig$interpola2 = userConfig.interpolation) === null || _userConfig$interpola2 === void 0 ? void 0 : _userConfig$interpola2.suffix;
55
55
  var prefix = userPrefix !== null && userPrefix !== void 0 ? userPrefix : '{{';
56
56
  var suffix = userSuffix !== null && userSuffix !== void 0 ? userSuffix : '}}';
57
57
  if (typeof (userConfig === null || userConfig === void 0 ? void 0 : userConfig.localeStructure) !== 'string' && (userPrefix || userSuffix)) {
@@ -91,7 +91,7 @@ export var createConfig = function createConfig(userConfig) {
91
91
  throw new Error('If nonExplicitSupportedLngs is true, no functions are allowed for fallbackLng');
92
92
  }
93
93
  }
94
- var hasCustomBackend = userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$use = userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
94
+ var hasCustomBackend = userConfig === null || userConfig === void 0 || (_userConfig$use = userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
95
95
  return b.type === 'backend';
96
96
  });
97
97
  if (!process.browser && typeof window === 'undefined') {
@@ -5,7 +5,7 @@ export default (function (config) {
5
5
  var initPromise;
6
6
  if (!instance.isInitialized) {
7
7
  var _config$use;
8
- config === null || config === void 0 ? void 0 : (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.forEach(function (x) {
8
+ config === null || config === void 0 || (_config$use = config.use) === null || _config$use === void 0 || _config$use.forEach(function (x) {
9
9
  return instance.use(x);
10
10
  });
11
11
  if (typeof config.onPreInitI18next === 'function') {
@@ -1,6 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
4
  import i18n from 'i18next';
5
5
  import i18nextFSBackend from 'i18next-fs-backend';
6
6
  var globalInstance;
@@ -20,13 +20,13 @@ export default (function (config) {
20
20
  var initPromise;
21
21
  if (!instance.isInitialized) {
22
22
  var _config$use, _config$use2;
23
- var hasCustomBackend = config === null || config === void 0 ? void 0 : (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.some(function (b) {
23
+ var hasCustomBackend = config === null || config === void 0 || (_config$use = config.use) === null || _config$use === void 0 ? void 0 : _config$use.some(function (b) {
24
24
  return b.type === 'backend';
25
25
  });
26
26
  if (!hasCustomBackend) {
27
27
  instance.use(i18nextFSBackend);
28
28
  }
29
- config === null || config === void 0 ? void 0 : (_config$use2 = config.use) === null || _config$use2 === void 0 ? void 0 : _config$use2.forEach(function (x) {
29
+ config === null || config === void 0 || (_config$use2 = config.use) === null || _config$use2 === void 0 || _config$use2.forEach(function (x) {
30
30
  return instance.use(x);
31
31
  });
32
32
  if (typeof config.onPreInitI18next === 'function') {
@@ -1,8 +1,8 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
6
  import fs from 'fs';
7
7
  import path from 'path';
8
8
  import { createConfig } from './config/createConfig';
@@ -22,7 +22,7 @@ if (process.env.I18NEXT_DEFAULT_CONFIG_PATH) {
22
22
  }
23
23
  export var serverSideTranslations = /*#__PURE__*/function () {
24
24
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(initialLocale) {
25
- var _userConfig, _userConfig$use;
25
+ var _userConfig;
26
26
  var namespacesRequired,
27
27
  configOverride,
28
28
  extraLocales,
@@ -42,103 +42,101 @@ export var serverSideTranslations = /*#__PURE__*/function () {
42
42
  namespacesByLocale,
43
43
  _args = arguments;
44
44
  return _regeneratorRuntime.wrap(function _callee$(_context) {
45
- while (1) {
46
- switch (_context.prev = _context.next) {
47
- case 0:
48
- namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
49
- configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
50
- extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
51
- if (!(typeof initialLocale !== 'string')) {
52
- _context.next = 5;
53
- break;
54
- }
55
- throw new Error('Initial locale argument was not passed into serverSideTranslations');
56
- case 5:
57
- userConfig = configOverride;
58
- configPath = path.resolve(DEFAULT_CONFIG_PATH);
59
- if (!(!userConfig && fs.existsSync(configPath))) {
60
- _context.next = 11;
61
- break;
62
- }
63
- _context.next = 10;
64
- return import(configPath);
65
- case 10:
66
- userConfig = _context.sent;
67
- case 11:
68
- if (!(userConfig === null)) {
69
- _context.next = 13;
70
- break;
71
- }
72
- throw new Error("next-i18next was unable to find a user config at ".concat(configPath));
73
- case 13:
74
- config = createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
75
- lng: initialLocale
76
- }));
77
- localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
78
- if (!reloadOnPrerender) {
79
- _context.next = 18;
80
- break;
81
- }
45
+ while (1) switch (_context.prev = _context.next) {
46
+ case 0:
47
+ namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
48
+ configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
49
+ extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
50
+ if (!(typeof initialLocale !== 'string')) {
51
+ _context.next = 5;
52
+ break;
53
+ }
54
+ throw new Error('Initial locale argument was not passed into serverSideTranslations');
55
+ case 5:
56
+ userConfig = configOverride;
57
+ configPath = path.resolve(DEFAULT_CONFIG_PATH);
58
+ if (!(!userConfig && fs.existsSync(configPath))) {
59
+ _context.next = 11;
60
+ break;
61
+ }
62
+ _context.next = 10;
63
+ return import(configPath);
64
+ case 10:
65
+ userConfig = _context.sent;
66
+ case 11:
67
+ if (!(userConfig === null)) {
68
+ _context.next = 13;
69
+ break;
70
+ }
71
+ throw new Error("next-i18next was unable to find a user config at ".concat(configPath));
72
+ case 13:
73
+ config = createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
74
+ lng: initialLocale
75
+ }));
76
+ localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
77
+ if (!reloadOnPrerender) {
82
78
  _context.next = 18;
83
- return globalI18n === null || globalI18n === void 0 ? void 0 : globalI18n.reloadResources();
84
- case 18:
85
- _createClient = createClient(_objectSpread(_objectSpread({}, config), {}, {
86
- lng: initialLocale
87
- })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
88
- _context.next = 21;
89
- return initPromise;
90
- case 21:
91
- hasCustomBackend = (_userConfig = userConfig) === null || _userConfig === void 0 ? void 0 : (_userConfig$use = _userConfig.use) === null || _userConfig$use === void 0 ? void 0 : _userConfig$use.some(function (b) {
92
- return b.type === 'backend';
93
- });
94
- if (!(hasCustomBackend && namespacesRequired)) {
95
- _context.next = 25;
96
- break;
97
- }
79
+ break;
80
+ }
81
+ _context.next = 18;
82
+ return globalI18n === null || globalI18n === void 0 ? void 0 : globalI18n.reloadResources();
83
+ case 18:
84
+ _createClient = createClient(_objectSpread(_objectSpread({}, config), {}, {
85
+ lng: initialLocale
86
+ })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
87
+ _context.next = 21;
88
+ return initPromise;
89
+ case 21:
90
+ hasCustomBackend = (_userConfig = userConfig) === null || _userConfig === void 0 || (_userConfig = _userConfig.use) === null || _userConfig === void 0 ? void 0 : _userConfig.some(function (b) {
91
+ return b.type === 'backend';
92
+ });
93
+ if (!(hasCustomBackend && namespacesRequired)) {
98
94
  _context.next = 25;
99
- return i18n.loadNamespaces(Array.isArray(namespacesRequired) ? namespacesRequired : namespacesRequired);
100
- case 25:
101
- initialI18nStore = _defineProperty({}, initialLocale, {});
102
- getFallbackForLng(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
103
- initialI18nStore[lng] = {};
104
- });
105
- if (Array.isArray(namespacesRequired)) {
106
- _context.next = 33;
107
- break;
95
+ break;
96
+ }
97
+ _context.next = 25;
98
+ return i18n.loadNamespaces(Array.isArray(namespacesRequired) ? namespacesRequired : namespacesRequired);
99
+ case 25:
100
+ initialI18nStore = _defineProperty({}, initialLocale, {});
101
+ getFallbackForLng(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
102
+ initialI18nStore[lng] = {};
103
+ });
104
+ if (Array.isArray(namespacesRequired)) {
105
+ _context.next = 33;
106
+ break;
107
+ }
108
+ if (!(typeof localePath === 'function')) {
109
+ _context.next = 30;
110
+ break;
111
+ }
112
+ throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
113
+ case 30:
114
+ getLocaleNamespaces = function getLocaleNamespaces(path) {
115
+ return fs.existsSync(path) ? fs.readdirSync(path).map(function (file) {
116
+ return file.replace(".".concat(localeExtension), '');
117
+ }) : [];
118
+ };
119
+ namespacesByLocale = Object.keys(initialI18nStore).map(function (locale) {
120
+ return getLocaleNamespaces(path.resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
121
+ }).flat();
122
+ namespacesRequired = unique(namespacesByLocale);
123
+ case 33:
124
+ namespacesRequired.forEach(function (ns) {
125
+ for (var locale in initialI18nStore) {
126
+ initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
108
127
  }
109
- if (!(typeof localePath === 'function')) {
110
- _context.next = 30;
111
- break;
128
+ });
129
+ return _context.abrupt("return", {
130
+ _nextI18Next: {
131
+ initialI18nStore: initialI18nStore,
132
+ initialLocale: initialLocale,
133
+ ns: namespacesRequired,
134
+ userConfig: config.serializeConfig ? userConfig : null
112
135
  }
113
- throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
114
- case 30:
115
- getLocaleNamespaces = function getLocaleNamespaces(path) {
116
- return fs.existsSync(path) ? fs.readdirSync(path).map(function (file) {
117
- return file.replace(".".concat(localeExtension), '');
118
- }) : [];
119
- };
120
- namespacesByLocale = Object.keys(initialI18nStore).map(function (locale) {
121
- return getLocaleNamespaces(path.resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
122
- }).flat();
123
- namespacesRequired = unique(namespacesByLocale);
124
- case 33:
125
- namespacesRequired.forEach(function (ns) {
126
- for (var locale in initialI18nStore) {
127
- initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
128
- }
129
- });
130
- return _context.abrupt("return", {
131
- _nextI18Next: {
132
- initialI18nStore: initialI18nStore,
133
- initialLocale: initialLocale,
134
- ns: namespacesRequired,
135
- userConfig: config.serializeConfig ? userConfig : null
136
- }
137
- });
138
- case 35:
139
- case "end":
140
- return _context.stop();
141
- }
136
+ });
137
+ case 35:
138
+ case "end":
139
+ return _context.stop();
142
140
  }
143
141
  }, _callee);
144
142
  }));
package/dist/esm/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  import _typeof from "@babel/runtime/helpers/typeof";
3
+ import { useLayoutEffect, useEffect } from 'react';
3
4
  export var getFallbackForLng = function getFallbackForLng(lng, fallbackLng) {
4
5
  if (typeof fallbackLng === 'string') {
5
6
  return [fallbackLng];
@@ -19,4 +20,13 @@ export var getFallbackForLng = function getFallbackForLng(lng, fallbackLng) {
19
20
  };
20
21
  export var unique = function unique(list) {
21
22
  return Array.from(new Set(list));
22
- };
23
+ };
24
+
25
+ /**
26
+ * This hook behaves like `useLayoutEffect` on the client,
27
+ * and `useEffect` on the server(no effect).
28
+ *
29
+ * Since using `useLayoutEffect` on the server cause warning messages in nextjs,
30
+ * this hook is workaround for that.
31
+ */
32
+ export var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
@@ -5,6 +5,6 @@ import { SSRConfig, UserConfig } from './types';
5
5
  import { i18n as I18NextClient } from 'i18next';
6
6
  export { Trans, useTranslation, withTranslation, } from 'react-i18next';
7
7
  export declare let globalI18n: I18NextClient | null;
8
- export declare const appWithTranslation: <Props extends NextJsAppProps<any>>(WrappedComponent: React.ComponentType<Props>, configOverride?: UserConfig | null) => ((props: Props & {
8
+ export declare const appWithTranslation: <Props extends NextJsAppProps>(WrappedComponent: React.ComponentType<Props>, configOverride?: UserConfig | null) => ((props: Props & {
9
9
  pageProps: Props["pageProps"] & SSRConfig;
10
- }) => JSX.Element) & hoistNonReactStatics.NonReactStatics<React.ComponentType<Props>, {}>;
10
+ }) => React.JSX.Element) & hoistNonReactStatics.NonReactStatics<React.ComponentType<Props>, {}>;
@@ -1,3 +1,12 @@
1
1
  import { FallbackLng } from 'i18next';
2
+ import { useLayoutEffect } from 'react';
2
3
  export declare const getFallbackForLng: (lng: string, fallbackLng: false | FallbackLng) => string[];
3
4
  export declare const unique: (list: string[]) => string[];
5
+ /**
6
+ * This hook behaves like `useLayoutEffect` on the client,
7
+ * and `useEffect` on the server(no effect).
8
+ *
9
+ * Since using `useLayoutEffect` on the server cause warning messages in nextjs,
10
+ * this hook is workaround for that.
11
+ */
12
+ export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "14.0.3",
3
+ "version": "15.0.0",
4
4
  "repository": "git@github.com:i18next/next-i18next.git",
5
5
  "author": "i18next",
6
6
  "funding": [
@@ -79,67 +79,67 @@
79
79
  "prepare": "husky install"
80
80
  },
81
81
  "devDependencies": {
82
- "@babel/cli": "7.20.7",
83
- "@babel/core": "7.20.12",
82
+ "@babel/cli": "7.23.0",
83
+ "@babel/core": "7.23.2",
84
84
  "@babel/plugin-proposal-class-properties": "7.18.6",
85
- "@babel/plugin-transform-runtime": "7.19.6",
86
- "@babel/preset-env": "7.20.2",
87
- "@babel/preset-react": "7.18.6",
88
- "@babel/preset-typescript": "7.18.6",
89
- "@size-limit/file": "^8.1.2",
90
- "@size-limit/webpack": "^8.1.2",
91
- "@size-limit/webpack-why": "^8.1.2",
92
- "@testing-library/react": "^13.4.0",
93
- "@types/jest": "^29.4.0",
94
- "@types/node": "^18.13.0",
95
- "@types/react": "^18.0.27",
96
- "@types/react-dom": "^18.0.10",
97
- "@types/testing-library__cypress": "^5.0.9",
98
- "@typescript-eslint/eslint-plugin": "^5.51.0",
99
- "@typescript-eslint/parser": "^5.51.0",
100
- "all-contributors-cli": "^6.24.0",
85
+ "@babel/plugin-transform-runtime": "7.23.2",
86
+ "@babel/preset-env": "7.23.2",
87
+ "@babel/preset-react": "7.22.15",
88
+ "@babel/preset-typescript": "7.23.2",
89
+ "@size-limit/file": "^10.0.2",
90
+ "@size-limit/webpack": "^10.0.2",
91
+ "@size-limit/webpack-why": "^10.0.2",
92
+ "@testing-library/react": "^14.0.0",
93
+ "@types/jest": "^29.5.7",
94
+ "@types/node": "^20.8.10",
95
+ "@types/react": "^18.2.33",
96
+ "@types/react-dom": "^18.2.14",
97
+ "@types/testing-library__cypress": "^5.0.12",
98
+ "@typescript-eslint/eslint-plugin": "^6.9.1",
99
+ "@typescript-eslint/parser": "^6.9.1",
100
+ "all-contributors-cli": "^6.26.1",
101
101
  "babel-core": "7.0.0-bridge.0",
102
102
  "babel-plugin-add-module-exports": "1.0.4",
103
103
  "babel-plugin-transform-async-to-generator": "6.24.1",
104
- "cpy-cli": "^4.2.0",
105
- "cypress": "^11.0.1",
106
- "es-check": "^7.1.0",
107
- "eslint": "^8.33.0",
108
- "eslint-config-prettier": "^8.6.0",
109
- "eslint-plugin-cypress": "^2.12.1",
110
- "eslint-plugin-import": "^2.27.5",
111
- "eslint-plugin-jest": "^27.2.1",
104
+ "cpy-cli": "^5.0.0",
105
+ "cypress": "^13.4.0",
106
+ "es-check": "^7.1.1",
107
+ "eslint": "^8.52.0",
108
+ "eslint-config-prettier": "^9.0.0",
109
+ "eslint-plugin-cypress": "^2.15.1",
110
+ "eslint-plugin-import": "^2.29.0",
111
+ "eslint-plugin-jest": "^27.6.0",
112
112
  "eslint-plugin-prefer-arrow": "^1.2.3",
113
- "eslint-plugin-react": "^7.32.2",
114
- "eslint-plugin-typescript-sort-keys": "^2.1.0",
113
+ "eslint-plugin-react": "^7.33.2",
114
+ "eslint-plugin-typescript-sort-keys": "^3.1.0",
115
115
  "gh-release": "7.0.2",
116
116
  "husky": "^8.0.3",
117
- "i18next": "^23.4.6",
118
- "jest": "^29.5.0",
119
- "jest-environment-jsdom": "^29.5.0",
120
- "next": "^13.1.6",
117
+ "i18next": "^23.6.0",
118
+ "jest": "^29.7.0",
119
+ "jest-environment-jsdom": "^29.7.0",
120
+ "next": "^14.0.1",
121
121
  "npm-run-all": "^4.1.5",
122
- "prettier": "2.8.3",
122
+ "prettier": "3.0.3",
123
123
  "react": "^18.2.0",
124
124
  "react-dom": "^18.2.0",
125
- "react-i18next": "^13.2.1",
126
- "rimraf": "^4.1.2",
127
- "size-limit": "^8.1.2",
128
- "start-server-and-test": "^1.15.3",
129
- "typescript": "^4.9.5",
130
- "webpack": "^5.75.0"
125
+ "react-i18next": "^13.3.1",
126
+ "rimraf": "^5.0.5",
127
+ "size-limit": "^10.0.2",
128
+ "start-server-and-test": "^2.0.1",
129
+ "typescript": "^5.2.2",
130
+ "webpack": "^5.89.0"
131
131
  },
132
132
  "dependencies": {
133
- "@babel/runtime": "^7.20.13",
134
- "@types/hoist-non-react-statics": "^3.3.1",
133
+ "@babel/runtime": "^7.23.2",
134
+ "@types/hoist-non-react-statics": "^3.3.4",
135
135
  "core-js": "^3",
136
136
  "hoist-non-react-statics": "^3.3.2",
137
- "i18next-fs-backend": "^2.1.5"
137
+ "i18next-fs-backend": "^2.2.0"
138
138
  },
139
139
  "peerDependencies": {
140
- "i18next": "^23.4.6",
140
+ "i18next": "^23.6.0",
141
141
  "next": ">= 12.0.0",
142
142
  "react": ">= 17.0.2",
143
- "react-i18next": "^13.2.1"
143
+ "react-i18next": "^13.3.1"
144
144
  }
145
145
  }