next-i18next 5.0.0-beta.4 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # next-i18next
2
2
  [![npm version](https://badge.fury.io/js/next-i18next.svg)](https://badge.fury.io/js/next-i18next)
3
3
  [![CircleCI](https://circleci.com/gh/isaachinman/next-i18next.svg?style=shield)](https://circleci.com/gh/isaachinman/next-i18next)
4
- [![dependencies Status](https://david-dm.org/isaachinman/next-i18next/status.svg)](https://david-dm.org/isaachinman/next-i18next)
5
4
  [![Package Quality](https://npm.packagequality.com/shield/next-i18next.svg)](https://packagequality.com/#?package=next-i18next)
6
5
 
7
6
  **The easiest way to translate your NextJs apps.**
@@ -184,15 +183,12 @@ const SomeButton = () => (
184
183
 
185
184
  In many cases, you'll need to know the currently active language. Most of the time, to accomplish this, you should use the `withTranslation` HOC, which will pass an `i18n` prop to the wrapped component and further asserts your component will get re-rendered on language change or changes to the translation catalog itself (loaded translations). More info can be found [here](https://react.i18next.com/latest/withtranslation-hoc).
186
185
 
187
- If for some reason you need to access the current language inside `getInitialProps`, you'll need to switch over server and client contexts. For example:
186
+ If for some reason you need to access the current language and `withTranslation` doesn't suit your needs, you can use the `I18nContext`:
188
187
 
189
188
  ```jsx
190
- // This is our initialised `NextI18Next` instance
191
- import { i18n } from '../i18n'
189
+ import { I18nContext } from 'next-i18next'
192
190
 
193
- MyPage.getInitialProps = async({ req }) => {
194
- const currentLanguage = req ? req.language : i18n.language
195
- }
191
+ const { i18n: { language } } = useContext(I18nContext)
196
192
  ```
197
193
 
198
194
  ## Options
@@ -218,7 +214,7 @@ _This table contains options which are specific to next-i18next. All other [i18n
218
214
 
219
215
  ## Notes
220
216
 
221
- - [`next export` will result in a _client-side only_ React application.](https://github.com/isaachinman/next-i18next/issues/10)
217
+ - [`next export` is not supported.](https://github.com/isaachinman/next-i18next/issues/780)
222
218
  - [To add a `lang` attribute to your top-level html DOM node, you must create a `_document.js` file.](https://github.com/isaachinman/next-i18next/issues/20#issuecomment-443461652)
223
219
  - [Localising `next/head` requires special consideration due to NextJs internals](https://github.com/isaachinman/next-i18next/issues/251#issuecomment-479421852).
224
220
 
package/babel.config.json CHANGED
@@ -37,7 +37,7 @@
37
37
  "helpers": true,
38
38
  "regenerator": true,
39
39
  "useESModules": false
40
- },
40
+ }
41
41
  }
42
42
  ]
43
43
  ],
@@ -76,6 +76,6 @@
76
76
  }
77
77
  ]
78
78
  ]
79
- },
79
+ }
80
80
  }
81
81
  }
@@ -35,9 +35,10 @@ var defaultConfig = {
35
35
  ignoreRoutes: ['/_next/', '/static/', '/public/', '/api/'],
36
36
  customDetectors: [],
37
37
  detection: {
38
+ caches: ['cookie'],
39
+ cookieSameSite: 'strict',
38
40
  lookupCookie: 'next-i18next',
39
- order: ['cookie', 'header', 'querystring'],
40
- caches: ['cookie']
41
+ order: ['cookie', 'header', 'querystring']
41
42
  },
42
43
  react: {
43
44
  wait: true,
@@ -206,7 +206,7 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
206
206
  key: "getInitialProps",
207
207
  value: function () {
208
208
  var _getInitialProps = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(ctx) {
209
- var wrappedComponentProps, _ctx$ctx, req, res, initialI18nStore, initialLanguage, i18nServerInstance, middlewares, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, namespacesRequired, fallbackLng, languagesToLoad;
209
+ var _ctx$ctx, req, res, initialI18nStore, initialLanguage, i18nServerInstance, middlewares, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, wrappedComponentProps, namespacesRequired, fallbackLng, languagesToLoad;
210
210
 
211
211
  return _regenerator["default"].wrap(function _callee2$(_context3) {
212
212
  while (1) {
@@ -216,30 +216,9 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
216
216
  return initPromise;
217
217
 
218
218
  case 2:
219
- wrappedComponentProps = {
220
- pageProps: {}
221
- };
222
-
223
- if (!WrappedComponent.getInitialProps) {
224
- _context3.next = 7;
225
- break;
226
- }
227
-
228
- _context3.next = 6;
229
- return WrappedComponent.getInitialProps(ctx);
230
-
231
- case 6:
232
- wrappedComponentProps = _context3.sent;
233
-
234
- case 7:
235
- if (typeof wrappedComponentProps.pageProps === 'undefined') {
236
- consoleMessage('error', 'If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more information, see: https://github.com/zeit/next.js#custom-app');
237
- }
238
219
  /*
239
220
  Initiate vars to return
240
221
  */
241
-
242
-
243
222
  _ctx$ctx = ctx.ctx, req = _ctx$ctx.req, res = _ctx$ctx.res;
244
223
  initialI18nStore = {};
245
224
  initialLanguage = null;
@@ -249,7 +228,7 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
249
228
  */
250
229
 
251
230
  if (!(req && res)) {
252
- _context3.next = 39;
231
+ _context3.next = 33;
253
232
  break;
254
233
  }
255
234
 
@@ -257,7 +236,7 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
257
236
  _iteratorNormalCompletion = true;
258
237
  _didIteratorError = false;
259
238
  _iteratorError = undefined;
260
- _context3.prev = 17;
239
+ _context3.prev = 11;
261
240
  _loop = /*#__PURE__*/_regenerator["default"].mark(function _loop() {
262
241
  var middleware;
263
242
  return _regenerator["default"].wrap(function _loop$(_context2) {
@@ -279,54 +258,79 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
279
258
  });
280
259
  _iterator = middlewares[Symbol.iterator]();
281
260
 
282
- case 20:
261
+ case 14:
283
262
  if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
284
- _context3.next = 25;
263
+ _context3.next = 19;
285
264
  break;
286
265
  }
287
266
 
288
- return _context3.delegateYield(_loop(), "t0", 22);
267
+ return _context3.delegateYield(_loop(), "t0", 16);
289
268
 
290
- case 22:
269
+ case 16:
291
270
  _iteratorNormalCompletion = true;
292
- _context3.next = 20;
271
+ _context3.next = 14;
293
272
  break;
294
273
 
295
- case 25:
296
- _context3.next = 31;
274
+ case 19:
275
+ _context3.next = 25;
297
276
  break;
298
277
 
299
- case 27:
300
- _context3.prev = 27;
301
- _context3.t1 = _context3["catch"](17);
278
+ case 21:
279
+ _context3.prev = 21;
280
+ _context3.t1 = _context3["catch"](11);
302
281
  _didIteratorError = true;
303
282
  _iteratorError = _context3.t1;
304
283
 
305
- case 31:
306
- _context3.prev = 31;
307
- _context3.prev = 32;
284
+ case 25:
285
+ _context3.prev = 25;
286
+ _context3.prev = 26;
308
287
 
309
288
  if (!_iteratorNormalCompletion && _iterator["return"] != null) {
310
289
  _iterator["return"]();
311
290
  }
312
291
 
313
- case 34:
314
- _context3.prev = 34;
292
+ case 28:
293
+ _context3.prev = 28;
315
294
 
316
295
  if (!_didIteratorError) {
317
- _context3.next = 37;
296
+ _context3.next = 31;
318
297
  break;
319
298
  }
320
299
 
321
300
  throw _iteratorError;
322
301
 
302
+ case 31:
303
+ return _context3.finish(28);
304
+
305
+ case 32:
306
+ return _context3.finish(25);
307
+
308
+ case 33:
309
+ /* Call getInitialProps on our wrapped _app */
310
+ wrappedComponentProps = {
311
+ pageProps: {}
312
+ };
313
+
314
+ if (!WrappedComponent.getInitialProps) {
315
+ _context3.next = 38;
316
+ break;
317
+ }
318
+
319
+ _context3.next = 37;
320
+ return WrappedComponent.getInitialProps(ctx);
321
+
323
322
  case 37:
324
- return _context3.finish(34);
323
+ wrappedComponentProps = _context3.sent;
325
324
 
326
325
  case 38:
327
- return _context3.finish(31);
326
+ if (typeof wrappedComponentProps.pageProps === 'undefined') {
327
+ consoleMessage('error', 'If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more information, see: https://github.com/zeit/next.js#custom-app');
328
+ }
329
+ /*
330
+ Step 1: Determine initial language
331
+ */
332
+
328
333
 
329
- case 39:
330
334
  if (!(req && req.i18n)) {
331
335
  _context3.next = 45;
332
336
  break;
@@ -439,7 +443,7 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
439
443
  return _context3.stop();
440
444
  }
441
445
  }
442
- }, _callee2, null, [[17, 27, 31, 39], [32,, 34, 38]]);
446
+ }, _callee2, null, [[11, 21, 25, 33], [26,, 28, 32]]);
443
447
  }));
444
448
 
445
449
  return function getInitialProps(_x2) {
@@ -7,6 +7,12 @@ require("core-js/modules/es.function.bind");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
+ Object.defineProperty(exports, "I18nContext", {
11
+ enumerable: true,
12
+ get: function get() {
13
+ return _reactI18next.I18nContext;
14
+ }
15
+ });
10
16
  Object.defineProperty(exports, "withTranslation", {
11
17
  enumerable: true,
12
18
  get: function get() {
@@ -52,10 +58,6 @@ var NextI18Next = function NextI18Next(userConfig) {
52
58
  this.consoleMessage = _utils.consoleMessage.bind(this);
53
59
  /* Validation */
54
60
 
55
- if (this.config.otherLanguages.length <= 0) {
56
- throw new Error('To properly initialise a next-i18next instance you must provide one or more locale codes in config.otherLanguages.');
57
- }
58
-
59
61
  this.withNamespaces = function () {
60
62
  throw new Error('next-i18next has upgraded to react-i18next v10 - please rename withNamespaces to withTranslation.');
61
63
  };
@@ -1,16 +1,12 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.isServer = void 0;
9
7
 
10
- var _detectNode = _interopRequireDefault(require("detect-node"));
11
-
12
8
  var isServer = function isServer() {
13
- return _detectNode["default"] && typeof window === 'undefined';
9
+ return typeof window === 'undefined';
14
10
  };
15
11
 
16
12
  exports.isServer = isServer;
@@ -25,9 +25,10 @@ export const defaultConfig = {
25
25
  ignoreRoutes: ['/_next/', '/static/', '/public/', '/api/'],
26
26
  customDetectors: [],
27
27
  detection: {
28
+ caches: ['cookie'],
29
+ cookieSameSite: 'strict',
28
30
  lookupCookie: 'next-i18next',
29
- order: ['cookie', 'header', 'querystring'],
30
- caches: ['cookie']
31
+ order: ['cookie', 'header', 'querystring']
31
32
  },
32
33
  react: {
33
34
  wait: true,
@@ -73,22 +73,10 @@ export const appWithTranslation = function (WrappedComponent) {
73
73
 
74
74
  static async getInitialProps(ctx) {
75
75
  await initPromise;
76
- let wrappedComponentProps = {
77
- pageProps: {}
78
- };
79
-
80
- if (WrappedComponent.getInitialProps) {
81
- wrappedComponentProps = await WrappedComponent.getInitialProps(ctx);
82
- }
83
-
84
- if (typeof wrappedComponentProps.pageProps === 'undefined') {
85
- consoleMessage('error', 'If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more information, see: https://github.com/zeit/next.js#custom-app');
86
- }
87
76
  /*
88
77
  Initiate vars to return
89
78
  */
90
79
 
91
-
92
80
  const {
93
81
  req,
94
82
  res
@@ -107,6 +95,20 @@ export const appWithTranslation = function (WrappedComponent) {
107
95
  await new Promise(resolve => middleware(req, res, resolve));
108
96
  }
109
97
  }
98
+ /* Call getInitialProps on our wrapped _app */
99
+
100
+
101
+ let wrappedComponentProps = {
102
+ pageProps: {}
103
+ };
104
+
105
+ if (WrappedComponent.getInitialProps) {
106
+ wrappedComponentProps = await WrappedComponent.getInitialProps(ctx);
107
+ }
108
+
109
+ if (typeof wrappedComponentProps.pageProps === 'undefined') {
110
+ consoleMessage('error', 'If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more information, see: https://github.com/zeit/next.js#custom-app');
111
+ }
110
112
  /*
111
113
  Step 1: Determine initial language
112
114
  */
package/dist/es/index.js CHANGED
@@ -6,17 +6,13 @@ import { appWithTranslation, withInternals } from './hocs';
6
6
  import { consoleMessage } from './utils';
7
7
  import { Link } from './components';
8
8
  import { wrapRouter } from './router';
9
- export { withTranslation } from 'react-i18next';
9
+ export { I18nContext, withTranslation } from 'react-i18next';
10
10
  export default class NextI18Next {
11
11
  constructor(userConfig) {
12
12
  this.config = createConfig(userConfig);
13
13
  this.consoleMessage = consoleMessage.bind(this);
14
14
  /* Validation */
15
15
 
16
- if (this.config.otherLanguages.length <= 0) {
17
- throw new Error('To properly initialise a next-i18next instance you must provide one or more locale codes in config.otherLanguages.');
18
- }
19
-
20
16
  this.withNamespaces = () => {
21
17
  throw new Error('next-i18next has upgraded to react-i18next v10 - please rename withNamespaces to withTranslation.');
22
18
  };
@@ -1,2 +1 @@
1
- import isNode from 'detect-node';
2
- export const isServer = () => isNode && typeof window === 'undefined';
1
+ export const isServer = () => typeof window === 'undefined';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "5.0.0-beta.4",
3
+ "version": "6.0.1",
4
4
  "repository": "git@github.com:isaachinman/next-i18next.git",
5
5
  "author": "Isaac Hinman <isaac@isaachinman.com>",
6
6
  "funding": {
@@ -50,15 +50,15 @@
50
50
  "bundlesize": [
51
51
  {
52
52
  "path": "./examples/simple/.next/static/chunks/commons*.js",
53
- "maxSize": "62 kB"
53
+ "maxSize": "65 kB"
54
54
  },
55
55
  {
56
- "path": "./examples/simple/.next/static/runtime/main*.js",
57
- "maxSize": "6 kB"
56
+ "path": "./examples/simple/.next/static/chunks/main*.js",
57
+ "maxSize": "7 kB"
58
58
  },
59
59
  {
60
- "path": "./examples/simple/.next/static/runtime/webpack*.js",
61
- "maxSize": "750 B"
60
+ "path": "./examples/simple/.next/static/chunks/webpack*.js",
61
+ "maxSize": "760 B"
62
62
  }
63
63
  ],
64
64
  "devDependencies": {
@@ -69,7 +69,6 @@
69
69
  "@babel/preset-env": "^7.10.4",
70
70
  "@babel/preset-react": "^7.10.4",
71
71
  "@babel/preset-typescript": "^7.10.4",
72
- "@types/express": "^4.16.1",
73
72
  "@types/jest": "^24.0.16",
74
73
  "@types/jest-environment-puppeteer": "^4.0.0",
75
74
  "@types/react": "^16.8.4",
@@ -100,21 +99,21 @@
100
99
  "typescript": "^3.5.3"
101
100
  },
102
101
  "dependencies": {
102
+ "@types/express": "^4.16.1",
103
103
  "core-js": "^3",
104
- "detect-node": "^2.0.4",
105
104
  "hoist-non-react-statics": "^3.2.0",
106
- "i18next": "^19.0.3",
107
- "i18next-browser-languagedetector": "^4.0.0",
108
- "i18next-fs-backend": "^1.0.2",
109
- "i18next-http-backend": "^1.0.8",
110
- "i18next-http-middleware": ">=1.0.2",
105
+ "i18next": "^19.6.3",
106
+ "i18next-browser-languagedetector": "^5.0.0",
107
+ "i18next-fs-backend": "^1.0.7",
108
+ "i18next-http-backend": "^1.0.17",
109
+ "i18next-http-middleware": "^3.0.2",
111
110
  "path-match": "^1.2.4",
112
111
  "prop-types": "^15.6.2",
113
- "react-i18next": "^11.0.0",
112
+ "react-i18next": "^11.7.0",
114
113
  "url": "^0.11.0"
115
114
  },
116
115
  "peerDependencies": {
117
- "next": ">= 9.3.0",
116
+ "next": ">= 9.5.0",
118
117
  "react": ">= 16.8.0"
119
118
  }
120
119
  }
package/types.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  import * as React from 'react'
4
4
  import {
5
+ I18nContext,
5
6
  useTranslation,
6
7
  TransProps,
7
8
  withTranslation,
@@ -18,6 +19,7 @@ export type InitConfig = {
18
19
  strictMode?: boolean;
19
20
  defaultLanguage: string;
20
21
  ignoreRoutes?: string[];
22
+ localeExtension?: string;
21
23
  localePath?: string;
22
24
  localeStructure?: string;
23
25
  otherLanguages: string[];
@@ -43,6 +45,7 @@ export type Trans = (props: TransProps) => any
43
45
  export type Link = React.ComponentClass<LinkProps>
44
46
  export type Router = SingletonRouter
45
47
  export type UseTranslation = typeof useTranslation
48
+ export type I18nContext = typeof I18nContext
46
49
  export type AppWithTranslation = <P extends object>(Component: React.ComponentType<P> | React.ElementType<P>) => any
47
50
  export type TFunction = I18NextTFunction
48
51
  export type I18n = i18n