next-i18next 3.0.0 → 4.2.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
@@ -87,6 +87,8 @@ After creating and exporting your `NextI18Next` instance, you need to take the f
87
87
  ```
88
88
  For more info, see [the NextJs section on custom servers](https://github.com/zeit/next.js#custom-server-and-routing).
89
89
 
90
+ Note: You can pass `shallowRender: true` into config options to avoid triggering getInitialProps when `changeLanguage` method is invoked.
91
+
90
92
  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
93
 
92
94
  ```jsx
@@ -239,7 +241,7 @@ MyPage.getInitialProps = async({ req }) => {
239
241
  | `browserLanguageDetection` | `true` |
240
242
  | `defaultNS` | `'common'` |
241
243
  | `defaultLanguage` | `'en'` |
242
- | `ignoreRoutes` | `['/_next/', '/static/', '/public/']` |
244
+ | `ignoreRoutes` | `['/_next/', '/static/', '/public/', '/api/']` |
243
245
  | `otherLanguages` (required) | `[]` |
244
246
  | `localeExtension` | `'json'` |
245
247
  | `localePath` | `'public/static/locales'` |
@@ -249,6 +251,7 @@ MyPage.getInitialProps = async({ req }) => {
249
251
  | `strictMode` | `true` |
250
252
  | `use` (for plugins) | `[]` |
251
253
  | `customDetectors` | `[]` |
254
+ | `shallowRender` | `false` |
252
255
 
253
256
  _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
257
 
@@ -266,7 +269,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
266
269
 
267
270
  <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
268
271
  <!-- prettier-ignore -->
269
- <table cellspacing="0" cellpadding="1"><tr><td><a href="https://github.com/capellini"><img src="https://avatars3.githubusercontent.com/u/75311?v=4" width="100px;" height="100px;" alt="Rob Capellini"/><br /><sub><b>Rob Capellini</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Tests">⚠️</a></td><td><a href="https://en.kachkaev.ru"><img src="https://avatars3.githubusercontent.com/u/608862?v=4" width="100px;" height="100px;" alt="Alexander Kachkaev"/><br /><sub><b>Alexander Kachkaev</b></sub></a><br /><a href="#talk-kachkaev" title="Talks">📢</a> <a href="#question-kachkaev" title="Answering Questions">💬</a> <a href="#ideas-kachkaev" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Tests">⚠️</a></td><td><a href="https://kandelborg.dk"><img src="https://avatars1.githubusercontent.com/u/33042011?v=4" width="100px;" height="100px;" alt="Mathias Wøbbe"/><br /><sub><b>Mathias Wøbbe</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Code">💻</a> <a href="#ideas-MathiasKandelborg" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Tests">⚠️</a></td><td><a href="http://lucasfeliciano.com"><img src="https://avatars3.githubusercontent.com/u/968014?v=4" width="100px;" height="100px;" alt="Lucas Feliciano"/><br /><sub><b>Lucas Feliciano</b></sub></a><br /><a href="#ideas-lucasfeliciano" title="Ideas, Planning, & Feedback">🤔</a> <a href="#review-lucasfeliciano" title="Reviewed Pull Requests">👀</a></td><td><a href="http://www.fifteenprospects.com"><img src="https://avatars2.githubusercontent.com/u/6932550?v=4" width="100px;" height="100px;" alt="Ryan Leung"/><br /><sub><b>Ryan Leung</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=minocys" title="Code">💻</a></td></tr></table>
272
+ <table cellspacing="0" cellpadding="1"><tr><td><a href="https://github.com/capellini"><img src="https://avatars3.githubusercontent.com/u/75311?v=4" width="100px;" height="100px;" alt="Rob Capellini"/><br /><sub><b>Rob Capellini</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Tests">⚠️</a></td><td><a href="https://en.kachkaev.ru"><img src="https://avatars3.githubusercontent.com/u/608862?v=4" width="100px;" height="100px;" alt="Alexander Kachkaev"/><br /><sub><b>Alexander Kachkaev</b></sub></a><br /><a href="#talk-kachkaev" title="Talks">📢</a> <a href="#question-kachkaev" title="Answering Questions">💬</a> <a href="#ideas-kachkaev" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Tests">⚠️</a></td><td><a href="https://kandelborg.dk"><img src="https://avatars1.githubusercontent.com/u/33042011?v=4" width="100px;" height="100px;" alt="Mathias Wøbbe"/><br /><sub><b>Mathias Wøbbe</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Code">💻</a> <a href="#ideas-MathiasKandelborg" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Tests">⚠️</a></td><td><a href="http://lucasfeliciano.com"><img src="https://avatars3.githubusercontent.com/u/968014?v=4" width="100px;" height="100px;" alt="Lucas Feliciano"/><br /><sub><b>Lucas Feliciano</b></sub></a><br /><a href="#ideas-lucasfeliciano" title="Ideas, Planning, & Feedback">🤔</a> <a href="#review-lucasfeliciano" title="Reviewed Pull Requests">👀</a></td><td><a href="http://www.fifteenprospects.com"><img src="https://avatars2.githubusercontent.com/u/6932550?v=4" width="100px;" height="100px;" alt="Ryan Leung"/><br /><sub><b>Ryan Leung</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=minocys" title="Code">💻</a></td><td><a href="http://nathanfriemel.com"><img src="https://avatars3.githubusercontent.com/u/1325835?v=4" width="100px;" height="100px;" alt="Nathan Friemel"/><br /><sub><b>Nathan Friemel</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=nathanfriemel" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=nathanfriemel" title="Documentation">📖</a> <a href="#example-nathanfriemel" title="Examples">💡</a> <a href="#ideas-nathanfriemel" title="Ideas, Planning, & Feedback">🤔</a></td></tr></table>
270
273
  <!-- ALL-CONTRIBUTORS-LIST:END -->
271
274
 
272
275
  This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
@@ -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) {
@@ -19,7 +19,7 @@ var subpathIsPresent = function subpathIsPresent(url, subpath) {
19
19
  var _parseUrl = (0, _url.parse)(url),
20
20
  pathname = _parseUrl.pathname;
21
21
 
22
- return pathname.length === subpath.length + 1 && pathname === "/".concat(subpath) || pathname.startsWith("/".concat(subpath, "/"));
22
+ return typeof pathname === 'string' && (pathname.length === subpath.length + 1 && pathname === "/".concat(subpath) || pathname.startsWith("/".concat(subpath, "/")));
23
23
  };
24
24
 
25
25
  exports.subpathIsPresent = subpathIsPresent;
@@ -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);
@@ -7,5 +7,5 @@ export const subpathIsPresent = (url, subpath) => {
7
7
  const {
8
8
  pathname
9
9
  } = parseUrl(url);
10
- return pathname.length === subpath.length + 1 && pathname === `/${subpath}` || pathname.startsWith(`/${subpath}/`);
10
+ return typeof pathname === 'string' && (pathname.length === subpath.length + 1 && pathname === `/${subpath}` || pathname.startsWith(`/${subpath}/`));
11
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "3.0.0",
3
+ "version": "4.2.0",
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