next-i18next 3.0.1 → 4.2.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
@@ -73,7 +73,8 @@ It's recommended to export this `NextI18Next` instance from a single file in you
73
73
 
74
74
  After creating and exporting your `NextI18Next` instance, you need to take the following steps to get things working:
75
75
 
76
- 1. Create an `_app.js` file inside your `pages` directory, and wrap it with the `NextI18Next.appWithTranslation` higher order component (HOC). You can see this approach in the [examples/simple/pages/_app.js](./examples/simple/pages/_app.js).
76
+ 1. Create an `_app.js` file inside your `pages` directory, and wrap it with the `NextI18Next.appWithTranslation` higher order component (HOC). You can see this approach in the [examples/simple/pages/_app.js](./examples/simple/pages/_app.js).
77
+ Your app component must either extend `App` if it's a class component or define a `getInitialProps` if it's a function component [(explanation here)](https://github.com/isaachinman/next-i18next/issues/615#issuecomment-575578375).
77
78
  2. Create a `server.js` file inside your root directory, initialise an [express](https://www.npmjs.com/package/express) server, and use the `nextI18NextMiddleware` middleware with your `nextI18Next` instance passed in. You can see this approach in the [examples/simple/server.js](./examples/simple/server.js).
78
79
  3. Update the scripts in `package.json` to:
79
80
  ```
@@ -87,6 +88,8 @@ After creating and exporting your `NextI18Next` instance, you need to take the f
87
88
  ```
88
89
  For more info, see [the NextJs section on custom servers](https://github.com/zeit/next.js#custom-server-and-routing).
89
90
 
91
+ Note: You can pass `shallowRender: true` into config options to avoid triggering getInitialProps when `changeLanguage` method is invoked.
92
+
90
93
  That's it! Your app is ready to go. You can now use the `NextI18Next.withTranslation` HOC to make your components or pages translatable, based on namespaces:
91
94
 
92
95
  ```jsx
@@ -239,7 +242,7 @@ MyPage.getInitialProps = async({ req }) => {
239
242
  | `browserLanguageDetection` | `true` |
240
243
  | `defaultNS` | `'common'` |
241
244
  | `defaultLanguage` | `'en'` |
242
- | `ignoreRoutes` | `['/_next/', '/static/', '/public/']` |
245
+ | `ignoreRoutes` | `['/_next/', '/static/', '/public/', '/api/']` |
243
246
  | `otherLanguages` (required) | `[]` |
244
247
  | `localeExtension` | `'json'` |
245
248
  | `localePath` | `'public/static/locales'` |
@@ -249,6 +252,7 @@ MyPage.getInitialProps = async({ req }) => {
249
252
  | `strictMode` | `true` |
250
253
  | `use` (for plugins) | `[]` |
251
254
  | `customDetectors` | `[]` |
255
+ | `shallowRender` | `false` |
252
256
 
253
257
  _This table contains options which are specific to next-i18next. All other [i18next options](https://www.i18next.com/overview/configuration-options) can be passed in as well._
254
258
 
@@ -34,7 +34,7 @@ var defaultConfig = {
34
34
  },
35
35
  browserLanguageDetection: true,
36
36
  serverLanguageDetection: true,
37
- ignoreRoutes: ['/_next/', '/static/', '/public/'],
37
+ ignoreRoutes: ['/_next/', '/static/', '/public/', '/api/'],
38
38
  customDetectors: [],
39
39
  detection: {
40
40
  lookupCookie: 'next-i18next',
@@ -47,6 +47,7 @@ var defaultConfig = {
47
47
  },
48
48
  strictMode: true,
49
49
  errorStackTraceLimit: 0,
50
+ shallowRender: false,
50
51
 
51
52
  get initImmediate() {
52
53
  return !(0, _utils.isServer)();
@@ -20,6 +20,8 @@ var _i18nextXhrBackend = _interopRequireDefault(require("i18next-xhr-backend"));
20
20
  var _i18nextBrowserLanguagedetector = _interopRequireDefault(require("i18next-browser-languagedetector"));
21
21
 
22
22
  var _default = function _default(config) {
23
+ var initPromise;
24
+
23
25
  if (!_i18next["default"].isInitialized) {
24
26
  if (_detectNode["default"]) {
25
27
  var i18nextNodeBackend = eval("require('i18next-node-fs-backend')");
@@ -51,11 +53,13 @@ var _default = function _default(config) {
51
53
  config.use.forEach(function (x) {
52
54
  return _i18next["default"].use(x);
53
55
  });
54
-
55
- _i18next["default"].init(config);
56
+ initPromise = _i18next["default"].init(config);
56
57
  }
57
58
 
58
- return _i18next["default"];
59
+ return {
60
+ i18n: _i18next["default"],
61
+ initPromise: initPromise
62
+ };
59
63
  };
60
64
 
61
65
  exports["default"] = _default;
@@ -117,7 +117,9 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
117
117
  as = _lngPathCorrector.as,
118
118
  href = _lngPathCorrector.href;
119
119
 
120
- router.replace(href, as);
120
+ router.replace(href, as, {
121
+ shallow: config.shallowRender
122
+ });
121
123
  }
122
124
  };
123
125
 
@@ -43,6 +43,7 @@ var NextI18Next = function NextI18Next(userConfig) {
43
43
  (0, _defineProperty2["default"])(this, "Link", void 0);
44
44
  (0, _defineProperty2["default"])(this, "Router", void 0);
45
45
  (0, _defineProperty2["default"])(this, "i18n", void 0);
46
+ (0, _defineProperty2["default"])(this, "initPromise", void 0);
46
47
  (0, _defineProperty2["default"])(this, "config", void 0);
47
48
  (0, _defineProperty2["default"])(this, "useTranslation", void 0);
48
49
  (0, _defineProperty2["default"])(this, "withTranslation", void 0);
@@ -61,7 +62,12 @@ var NextI18Next = function NextI18Next(userConfig) {
61
62
  throw new Error('next-i18next has upgraded to react-i18next v10 - please rename withNamespaces to withTranslation.');
62
63
  };
63
64
 
64
- this.i18n = (0, _createI18nextClient["default"])(this.config);
65
+ var _createI18NextClient = (0, _createI18nextClient["default"])(this.config),
66
+ i18n = _createI18NextClient.i18n,
67
+ initPromise = _createI18NextClient.initPromise;
68
+
69
+ this.i18n = i18n;
70
+ this.initPromise = initPromise;
65
71
  this.appWithTranslation = _hocs.appWithTranslation.bind(this);
66
72
 
67
73
  this.withTranslation = function (namespace, options) {
@@ -22,7 +22,7 @@ export const defaultConfig = {
22
22
  },
23
23
  browserLanguageDetection: true,
24
24
  serverLanguageDetection: true,
25
- ignoreRoutes: ['/_next/', '/static/', '/public/'],
25
+ ignoreRoutes: ['/_next/', '/static/', '/public/', '/api/'],
26
26
  customDetectors: [],
27
27
  detection: {
28
28
  lookupCookie: 'next-i18next',
@@ -35,6 +35,7 @@ export const defaultConfig = {
35
35
  },
36
36
  strictMode: true,
37
37
  errorStackTraceLimit: 0,
38
+ shallowRender: false,
38
39
 
39
40
  get initImmediate() {
40
41
  return !isServer();
@@ -3,6 +3,8 @@ import i18n from 'i18next';
3
3
  import i18nextXHRBackend from 'i18next-xhr-backend';
4
4
  import I18nextBrowserLanguageDetector from 'i18next-browser-languagedetector';
5
5
  export default (config => {
6
+ let initPromise;
7
+
6
8
  if (!i18n.isInitialized) {
7
9
  if (isNode) {
8
10
  const i18nextNodeBackend = eval("require('i18next-node-fs-backend')");
@@ -25,8 +27,11 @@ export default (config => {
25
27
  }
26
28
 
27
29
  config.use.forEach(x => i18n.use(x));
28
- i18n.init(config);
30
+ initPromise = i18n.init(config);
29
31
  }
30
32
 
31
- return i18n;
33
+ return {
34
+ i18n,
35
+ initPromise
36
+ };
32
37
  });
@@ -44,7 +44,9 @@ export const appWithTranslation = function (WrappedComponent) {
44
44
  as: asPath,
45
45
  href: routeInfo
46
46
  }, newLng);
47
- router.replace(href, as);
47
+ router.replace(href, as, {
48
+ shallow: config.shallowRender
49
+ });
48
50
  }
49
51
  };
50
52
 
package/dist/es/index.js CHANGED
@@ -21,7 +21,12 @@ export default class NextI18Next {
21
21
  throw new Error('next-i18next has upgraded to react-i18next v10 - please rename withNamespaces to withTranslation.');
22
22
  };
23
23
 
24
- this.i18n = createI18NextClient(this.config);
24
+ const {
25
+ i18n,
26
+ initPromise
27
+ } = createI18NextClient(this.config);
28
+ this.i18n = i18n;
29
+ this.initPromise = initPromise;
25
30
  this.appWithTranslation = appWithTranslation.bind(this);
26
31
 
27
32
  this.withTranslation = (namespace, options) => Component => hoistNonReactStatics(withTranslation(namespace, options)(Component), Component);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "3.0.1",
3
+ "version": "4.2.1",
4
4
  "repository": "git@github.com:isaachinman/next-i18next.git",
5
5
  "author": "Isaac Hinman <isaac@isaachinman.com>",
6
6
  "funding": {
@@ -103,7 +103,7 @@
103
103
  "core-js": "^2",
104
104
  "detect-node": "^2.0.4",
105
105
  "hoist-non-react-statics": "^3.2.0",
106
- "i18next": "^18.0.1",
106
+ "i18next": "^19.0.3",
107
107
  "i18next-browser-languagedetector": "^4.0.0",
108
108
  "i18next-express-middleware": "^1.5.0",
109
109
  "i18next-node-fs-backend": "^2.1.0",
package/types.d.ts CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  } from 'react-i18next'
10
10
  import { LinkProps } from 'next/link'
11
11
  import { SingletonRouter } from 'next/router'
12
- import i18next from 'i18next'
12
+ import { InitOptions, i18n, TFunction as I18NextTFunction } from 'i18next'
13
13
 
14
14
  export type InitConfig = {
15
15
  browserLanguageDetection?: boolean;
@@ -23,7 +23,8 @@ export type InitConfig = {
23
23
  localeSubpaths?: Record<string, string>;
24
24
  use?: any[];
25
25
  customDetectors?: any[];
26
- } & i18next.InitOptions
26
+ shallowRender?: boolean;
27
+ } & InitOptions
27
28
 
28
29
  export type Config = {
29
30
  fallbackLng: boolean;
@@ -42,10 +43,11 @@ export type Link = React.ComponentClass<LinkProps>
42
43
  export type Router = SingletonRouter
43
44
  export type UseTranslation = typeof useTranslation
44
45
  export type AppWithTranslation = <P extends object>(Component: React.ComponentType<P> | React.ElementType<P>) => any
45
- export type TFunction = i18next.TFunction
46
- export type I18n = i18next.i18n
46
+ export type TFunction = I18NextTFunction
47
+ export type I18n = i18n
47
48
  export type WithTranslationHocType = typeof withTranslation
48
49
  export type WithTranslation = ReactI18nextWithTranslation
50
+ export type InitPromise = Promise<TFunction>
49
51
 
50
52
  declare class NextI18Next {
51
53
  constructor(config: InitConfig);
@@ -53,6 +55,7 @@ declare class NextI18Next {
53
55
  Link: Link
54
56
  Router: Router
55
57
  i18n: I18n
58
+ initPromise: InitPromise
56
59
  config: Config
57
60
  useTranslation: UseTranslation
58
61
  withTranslation: WithTranslationHocType