next-i18next 14.0.3 → 15.1.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,8 +10,8 @@
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/).
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/)
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-app-dir-i18n/).
14
+ [![](https://locize.com/blog/next-app-dir-i18n/next-app-dir-i18n.jpg)](https://locize.com/blog/next-app-dir-i18n/)
15
15
 
16
16
  ## What is this?
17
17
 
@@ -146,7 +146,7 @@ The `serverSideTranslations` HOC is primarily responsible for passing translatio
146
146
 
147
147
  This is the hook which you'll actually use to do the translation itself. The `useTranslation` hook [comes from `react-i18next`](https://react.i18next.com/latest/usetranslation-hook), but needs to be imported from `next-i18next` directly.
148
148
  <br/>
149
- **Do NOT use the `useTranslation` export of `react-i18next`, but ONLY use the on of `next-i18next`!**
149
+ **Do NOT use the `useTranslation` export of `react-i18next`, but ONLY use the one from `next-i18next`!**
150
150
 
151
151
  ```tsx
152
152
  import { useTranslation } from 'next-i18next'
@@ -282,6 +282,35 @@ export const getStaticProps = async ({ locale }) => ({
282
282
  })
283
283
  ```
284
284
 
285
+ #### Usage with fallback SSG pages
286
+
287
+ When using on server-side generated pages with [`getStaticPaths`](https://nextjs.org/docs/pages/api-reference/functions/get-static-paths) and [`fallback: true`](https://nextjs.org/docs/pages/api-reference/functions/get-static-paths#fallback-true) or [`fallback: 'blocking'`](https://nextjs.org/docs/pages/api-reference/functions/get-static-paths#fallback-blocking), the default setup indicated above will cause the app to be unmounted and remounted on every load, causing various adverse consequences like calling every `useEffect(() => {...}, [])` hook twice and slight performance degradation.
288
+
289
+ This is due to the fact that, for those pages, Next.js does a first render with empty `serverSideProps` and then a second render with the `serverSideProps` that include the `next-i18next` translations. With the default setup, the `i18n` instance is initially `undefined` when `serverSideProps` is `empty`, causing the unmount-remount.
290
+
291
+ To mitigate this issue, you can do the following:
292
+
293
+ ```tsx
294
+ import { UserConfig } from 'next-i18next';
295
+ import nextI18NextConfig from '../next-i18next.config.js'
296
+
297
+ const emptyInitialI18NextConfig: UserConfig = {
298
+ i18n: {
299
+ defaultLocale: nextI18NextConfig.i18n.defaultLocale,
300
+ locales: nextI18NextConfig.i18n.locales,
301
+ },
302
+ };
303
+
304
+ const MyApp = ({ Component, pageProps }) => (
305
+ <Component {...pageProps} />
306
+ )
307
+
308
+ export default appWithTranslation(MyApp, emptyInitialI18NextConfig) // Makes sure the initial i18n instance is not undefined
309
+ ```
310
+
311
+ This will work as long as you make sure that, in the fallback page state, your client-side code is not trying to display any translation since otherwise you will get a "server-client mismatch" error from Next.js (due to the fact that the server has an actual translation in its html while the client html has the translation key in the same place).
312
+ This is normal and fine: you shouldn't be displaying a translation key to your user anyway!
313
+
285
314
  #### Client side loading of translations via HTTP
286
315
 
287
316
  Since [v11.0.0](https://github.com/i18next/next-i18next/releases/tag/v11.0.0) next-i18next also provides support for client side loading of translations.
@@ -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,51 @@ 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 addResourcesToI18next = function addResourcesToI18next(instance, resources) {
56
+ if (resources && instance.isInitialized) {
57
+ for (var _i = 0, _Object$keys = Object.keys(resources); _i < _Object$keys.length; _i++) {
58
+ var locale = _Object$keys[_i];
59
+ for (var _i2 = 0, _Object$keys2 = Object.keys(resources[locale]); _i2 < _Object$keys2.length; _i2++) {
60
+ var ns = _Object$keys2[_i2];
61
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
62
+ // @ts-ignore
63
+ if (instance.hasLoadedNamespace(ns, {
64
+ lng: locale
65
+ })) {
66
+ instance.addResourceBundle(locale, ns, resources[locale][ns], true, true);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ };
72
+ var appWithTranslation = exports.appWithTranslation = function appWithTranslation(WrappedComponent) {
56
73
  var configOverride = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
57
74
  var AppWithTranslation = function AppWithTranslation(props) {
58
75
  var _nextI18Next$initialL, _props$router;
59
76
  var _ref = props.pageProps || {},
60
77
  _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;
78
+ 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
79
  var ns = _nextI18Next === null || _nextI18Next === void 0 ? void 0 : _nextI18Next.ns;
80
+ var instanceRef = (0, _react.useRef)(null);
63
81
 
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
82
+ /**
83
+ * Memoize i18n instance and reuse it rather than creating new instance.
84
+ * When the locale or resources are changed after instance was created,
85
+ * we will update the instance by calling addResourceBundle method on it.
86
+ */
68
87
  var i18n = (0, _react.useMemo)(function () {
69
88
  var _userConfig$i18n;
70
89
  if (!_nextI18Next && !configOverride) return null;
@@ -82,16 +101,32 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
82
101
  initialI18nStore = _ref2.initialI18nStore;
83
102
  var resources = configOverride !== null && configOverride !== void 0 && configOverride.resources ? configOverride.resources : initialI18nStore;
84
103
  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;
104
+ var instance = instanceRef.current;
105
+ if (instance) {
106
+ addResourcesToI18next(instance, resources);
107
+ } else {
108
+ instance = (0, _createClient["default"])(_objectSpread(_objectSpread({}, (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
109
+ lng: locale
110
+ }))), {}, {
111
+ lng: locale,
112
+ ns: ns,
113
+ resources: resources
114
+ })).i18n;
115
+ addResourcesToI18next(instance, resources);
116
+ exports.globalI18n = globalI18n = instance;
117
+ instanceRef.current = instance;
118
+ }
93
119
  return instance;
94
- }, [_nextI18Next, locale, configOverride, ns]);
120
+ }, [_nextI18Next, locale, ns]);
121
+
122
+ /**
123
+ * Since calling changeLanguage method on existing i18n instance cause state update in react,
124
+ * we need to call the method in `useLayoutEffect` to prevent state update in render phase.
125
+ */
126
+ (0, _utils.useIsomorphicLayoutEffect)(function () {
127
+ if (!i18n || !locale) return;
128
+ i18n.changeLanguage(locale);
129
+ }, [i18n, locale]);
95
130
  return i18n !== null ? __jsx(_reactI18next.I18nextProvider, {
96
131
  i18n: i18n
97
132
  }, __jsx(WrappedComponent, props)) : __jsx(WrappedComponent, (0, _extends2["default"])({
@@ -99,5 +134,4 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
99
134
  }, props));
100
135
  };
101
136
  return (0, _hoistNonReactStatics["default"])(AppWithTranslation, WrappedComponent);
102
- };
103
- exports.appWithTranslation = appWithTranslation;
137
+ };
@@ -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;