next-i18next 13.2.2 → 13.3.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/README.md CHANGED
@@ -5,11 +5,14 @@
5
5
  [![npm version](https://img.shields.io/npm/v/next-i18next.svg?style=flat-square)](https://www.npmjs.com/package/next-i18next)
6
6
  ![npm](https://img.shields.io/npm/dw/next-i18next)
7
7
 
8
- **The easiest way to translate your Next.js apps.**
8
+ **The easiest way to translate your Next.js apps *(with pages setup)*.**
9
9
 
10
- If you are using next-i18next in production and like to unleash some super powers, you may have a look at [this blog post](https://locize.com/blog/next-i18next/).
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/)
15
+
13
16
  ## What is this?
14
17
 
15
18
  Although Next.js [provides internationalised routing directly](https://nextjs.org/docs/advanced-features/i18n-routing), it does not handle any management of translation content, or the actual translation functionality itself. All Next.js does is keep your locales and URLs in sync.
@@ -74,6 +77,7 @@ This tells `next-i18next` what your `defaultLocale` and other locales are, so th
74
77
  #### `next-i18next.config.js`
75
78
 
76
79
  ```js
80
+ /** @type {import('next-i18next').UserConfig} */
77
81
  module.exports = {
78
82
  i18n: {
79
83
  defaultLocale: 'en',
@@ -234,7 +238,7 @@ module.exports = {
234
238
  typeof window === 'undefined'
235
239
  ? require('path').resolve('./my-custom/path')
236
240
  : '/public/my-custom/path',
237
- ns: ['common']
241
+ ns: ['common'],
238
242
  }
239
243
  ```
240
244
 
@@ -339,6 +343,7 @@ For example, if you want to use `{` and `}` the config would look like this:
339
343
  If you want to change the default config path, you can set the environment variable `I18NEXT_DEFAULT_CONFIG_PATH`.
340
344
 
341
345
  For example, inside the `.env` file you can set a static path:
346
+
342
347
  ```
343
348
  I18NEXT_DEFAULT_CONFIG_PATH=/path/to/project/apps/my-app/next-i18next.config.js
344
349
  ```
@@ -403,7 +408,6 @@ But there's a way to workaround that with the help of [next-language-detector](h
403
408
  Check out [this blog post](https://locize.com/blog/next-i18n-static/) and [this example project](./examples/ssg/).
404
409
  [![](https://locize.com/blog/next-i18n-static/title.jpg)](https://locize.com/blog/next-i18n-static/)
405
410
 
406
-
407
411
  ### Translate in child components
408
412
 
409
413
  You have multiple ways to use the t function in your child component:
@@ -413,7 +417,7 @@ You have multiple ways to use the t function in your child component:
413
417
  3. Use the [`useTranslation`](https://react.i18next.com/latest/usetranslation-hook) function, like in this example: https://github.com/i18next/next-i18next/blob/e6b5085b5e92004afa9516bd444b19b2c8cf5758/examples/simple/components/Footer.tsx#L6
414
418
  4. Use the [`withTranslation`](https://react.i18next.com/latest/withtranslation-hoc) function
415
419
 
416
- *And in general, you always needs to be sure serverSideTranslations contains all namespaces you need in the tree.*
420
+ _And in general, you always needs to be sure serverSideTranslations contains all namespaces you need in the tree._
417
421
 
418
422
  ## Contributors
419
423
 
@@ -19,6 +19,7 @@ exports.serverSideTranslations = void 0;
19
19
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
20
20
  require("core-js/modules/es.object.to-string.js");
21
21
  require("core-js/modules/es.promise.js");
22
+ require("core-js/modules/es.array.some.js");
22
23
  require("core-js/modules/es.array.for-each.js");
23
24
  require("core-js/modules/web.dom-collections.for-each.js");
24
25
  require("core-js/modules/es.array.concat.js");
@@ -54,6 +55,7 @@ if (process.env.I18NEXT_DEFAULT_CONFIG_PATH) {
54
55
  }
55
56
  var serverSideTranslations = /*#__PURE__*/function () {
56
57
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(initialLocale) {
58
+ var _userConfig, _userConfig$use;
57
59
  var namespacesRequired,
58
60
  configOverride,
59
61
  extraLocales,
@@ -67,6 +69,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
67
69
  _createClient,
68
70
  i18n,
69
71
  initPromise,
72
+ hasCustomBackend,
70
73
  initialI18nStore,
71
74
  getLocaleNamespaces,
72
75
  namespacesByLocale,
@@ -120,20 +123,30 @@ var serverSideTranslations = /*#__PURE__*/function () {
120
123
  _context.next = 21;
121
124
  return initPromise;
122
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
+ }
133
+ _context.next = 25;
134
+ return i18n.loadNamespaces(namespacesRequired);
135
+ case 25:
123
136
  initialI18nStore = (0, _defineProperty2["default"])({}, initialLocale, {});
124
137
  (0, _utils.getFallbackForLng)(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
125
138
  initialI18nStore[lng] = {};
126
139
  });
127
140
  if (Array.isArray(namespacesRequired)) {
128
- _context.next = 29;
141
+ _context.next = 33;
129
142
  break;
130
143
  }
131
144
  if (!(typeof localePath === 'function')) {
132
- _context.next = 26;
145
+ _context.next = 30;
133
146
  break;
134
147
  }
135
148
  throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
136
- case 26:
149
+ case 30:
137
150
  getLocaleNamespaces = function getLocaleNamespaces(path) {
138
151
  return _fs["default"].existsSync(path) ? _fs["default"].readdirSync(path).map(function (file) {
139
152
  return file.replace(".".concat(localeExtension), '');
@@ -143,7 +156,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
143
156
  return getLocaleNamespaces(_path["default"].resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
144
157
  }).flat();
145
158
  namespacesRequired = (0, _utils.unique)(namespacesByLocale);
146
- case 29:
159
+ case 33:
147
160
  namespacesRequired.forEach(function (ns) {
148
161
  for (var locale in initialI18nStore) {
149
162
  initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
@@ -157,7 +170,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
157
170
  userConfig: config.serializeConfig ? userConfig : null
158
171
  }
159
172
  });
160
- case 31:
173
+ case 35:
161
174
  case "end":
162
175
  return _context.stop();
163
176
  }
@@ -48,6 +48,10 @@ export const serverSideTranslations = async (initialLocale, namespacesRequired =
48
48
  lng: initialLocale
49
49
  });
50
50
  await initPromise;
51
+ const hasCustomBackend = userConfig?.use?.some(b => b.type === 'backend');
52
+ if (hasCustomBackend && namespacesRequired) {
53
+ await i18n.loadNamespaces(namespacesRequired);
54
+ }
51
55
  const initialI18nStore = {
52
56
  [initialLocale]: {}
53
57
  };
@@ -22,6 +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
26
  var namespacesRequired,
26
27
  configOverride,
27
28
  extraLocales,
@@ -35,6 +36,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
35
36
  _createClient,
36
37
  i18n,
37
38
  initPromise,
39
+ hasCustomBackend,
38
40
  initialI18nStore,
39
41
  getLocaleNamespaces,
40
42
  namespacesByLocale,
@@ -86,20 +88,30 @@ export var serverSideTranslations = /*#__PURE__*/function () {
86
88
  _context.next = 21;
87
89
  return initPromise;
88
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
+ }
98
+ _context.next = 25;
99
+ return i18n.loadNamespaces(namespacesRequired);
100
+ case 25:
89
101
  initialI18nStore = _defineProperty({}, initialLocale, {});
90
102
  getFallbackForLng(initialLocale, fallbackLng !== null && fallbackLng !== void 0 ? fallbackLng : false).concat(extraLocales || []).forEach(function (lng) {
91
103
  initialI18nStore[lng] = {};
92
104
  });
93
105
  if (Array.isArray(namespacesRequired)) {
94
- _context.next = 29;
106
+ _context.next = 33;
95
107
  break;
96
108
  }
97
109
  if (!(typeof localePath === 'function')) {
98
- _context.next = 26;
110
+ _context.next = 30;
99
111
  break;
100
112
  }
101
113
  throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
102
- case 26:
114
+ case 30:
103
115
  getLocaleNamespaces = function getLocaleNamespaces(path) {
104
116
  return fs.existsSync(path) ? fs.readdirSync(path).map(function (file) {
105
117
  return file.replace(".".concat(localeExtension), '');
@@ -109,7 +121,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
109
121
  return getLocaleNamespaces(path.resolve(process.cwd(), "".concat(localePath, "/").concat(locale)));
110
122
  }).flat();
111
123
  namespacesRequired = unique(namespacesByLocale);
112
- case 29:
124
+ case 33:
113
125
  namespacesRequired.forEach(function (ns) {
114
126
  for (var locale in initialI18nStore) {
115
127
  initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
@@ -123,7 +135,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
123
135
  userConfig: config.serializeConfig ? userConfig : null
124
136
  }
125
137
  });
126
- case 31:
138
+ case 35:
127
139
  case "end":
128
140
  return _context.stop();
129
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "13.2.2",
3
+ "version": "13.3.0",
4
4
  "repository": "git@github.com:i18next/next-i18next.git",
5
5
  "author": "i18next",
6
6
  "funding": [
@@ -36,6 +36,7 @@
36
36
  "locale"
37
37
  ],
38
38
  "scripts": {
39
+ "prettier": "prettier --ignore-path .gitignore --write .",
39
40
  "lint": "eslint src examples",
40
41
  "clean": "rm -rf ./examples/simple/.next ./examples/ssg/.next ./dist && mkdir dist",
41
42
  "build:es": "BABEL_ENV=es babel src --extensions '.ts,.tsx' --out-dir dist/es --copy-files",
@@ -113,8 +114,8 @@
113
114
  "gh-release": "7.0.2",
114
115
  "husky": "^8.0.3",
115
116
  "i18next": "^22.4.10",
116
- "jest": "^29.4.2",
117
- "jest-environment-jsdom": "^29.4.2",
117
+ "jest": "^29.5.0",
118
+ "jest-environment-jsdom": "^29.5.0",
118
119
  "next": "^13.1.6",
119
120
  "npm-run-all": "^4.1.5",
120
121
  "prettier": "2.8.3",
@@ -6,13 +6,13 @@
6
6
 
7
7
  module.exports = {
8
8
  // These settings are duplicated in .editorconfig:
9
- tabWidth: 2, // indent_size = 2
10
- useTabs: false, // indent_style = space
9
+ arrowParens: 'avoid',
10
+ bracketSpacing: true,
11
11
  endOfLine: 'lf', // end_of_line = lf
12
+ printWidth: 69, // default: 80
12
13
  semi: false, // default: true
13
14
  singleQuote: true, // default: false
14
- printWidth: 69, // default: 80
15
+ tabWidth: 2, // indent_size = 2
15
16
  trailingComma: 'es5',
16
- bracketSpacing: true,
17
- arrowParens: 'avoid',
17
+ useTabs: false, // indent_style = space
18
18
  }