next-i18next 6.0.1 → 7.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
@@ -69,7 +69,7 @@ After creating and exporting your `NextI18Next` instance, you need to take the f
69
69
 
70
70
  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).
71
71
  Your app component must either extend `App` if it's a class component or define a `getInitialProps` if it's a functional component [(explanation here)](https://github.com/isaachinman/next-i18next/issues/615#issuecomment-575578375).
72
- 2. Create a `next.config.js` file inside your root directory if you want to use locale subpaths. You can see this approach in the [examples/simple/next.config.js](./examples/simple/next.config.js).
72
+ 2. Create a `next.config.js` file inside your root directory if you want to use locale subpaths. You can see this approach in the [examples/simple/next.config.js](./examples/simple/next.config.js) (Next.js 9.5+ required).
73
73
 
74
74
  Note: You can pass `shallowRender: true` into config options to avoid triggering getInitialProps when `changeLanguage` method is invoked.
75
75
 
@@ -217,6 +217,11 @@ _This table contains options which are specific to next-i18next. All other [i18n
217
217
  - [`next export` is not supported.](https://github.com/isaachinman/next-i18next/issues/780)
218
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)
219
219
  - [Localising `next/head` requires special consideration due to NextJs internals](https://github.com/isaachinman/next-i18next/issues/251#issuecomment-479421852).
220
+ - [How to use multiple namespaces in the same component](https://github.com/isaachinman/next-i18next/issues/762#issuecomment-661348457)
221
+
222
+ ## Usage with TypeScript
223
+
224
+ `next-i18next` is written in TypeScript and has full support for it. Refer to the usage guide [here](./TYPESCRIPT.md).
220
225
 
221
226
  ## Contributors
222
227
 
@@ -6,6 +6,8 @@ require("core-js/modules/es.array.concat");
6
6
 
7
7
  require("core-js/modules/es.array.filter");
8
8
 
9
+ require("core-js/modules/es.array.find");
10
+
9
11
  require("core-js/modules/es.array.for-each");
10
12
 
11
13
  require("core-js/modules/es.array.index-of");
@@ -55,69 +57,89 @@ var createConfig = function createConfig(userConfig) {
55
57
  Sensible defaults to prevent user duplication
56
58
  */
57
59
 
58
- combinedConfig.allLanguages = dedupe(combinedConfig.otherLanguages.concat([combinedConfig.defaultLanguage]));
59
- combinedConfig.whitelist = combinedConfig.allLanguages;
60
+ combinedConfig.allLanguages = dedupe(combinedConfig.otherLanguages.concat([combinedConfig.defaultLanguage])); // https://github.com/i18next/i18next/blob/master/CHANGELOG.md#1950
61
+
62
+ combinedConfig.supportedLngs = combinedConfig.allLanguages; // temporal backwards compatibility WHITELIST REMOVAL
63
+
64
+ combinedConfig.whitelist = combinedConfig.allLanguages; // end temporal backwards compatibility WHITELIST REMOVAL
65
+
60
66
  var allLanguages = combinedConfig.allLanguages,
61
67
  defaultLanguage = combinedConfig.defaultLanguage,
62
68
  localeExtension = combinedConfig.localeExtension,
63
69
  localePath = combinedConfig.localePath,
64
70
  localeStructure = combinedConfig.localeStructure;
71
+ /**
72
+ * Skips translation file resolution while in cimode
73
+ * https://github.com/isaachinman/next-i18next/pull/851#discussion_r503113620
74
+ */
65
75
 
66
- if ((0, _utils.isServer)()) {
67
- var fs = eval("require('fs')");
68
-
69
- var path = require('path');
76
+ if ((0, _utils.isCIMode)(defaultLanguage)) {
77
+ return combinedConfig;
78
+ }
70
79
 
71
- var serverLocalePath = localePath;
80
+ if ((0, _utils.isServer)()) {
72
81
  /*
73
- Validate defaultNS
74
- https://github.com/isaachinman/next-i18next/issues/358
82
+ On Server side preload (languages)
75
83
  */
76
-
77
- if (typeof combinedConfig.defaultNS === 'string') {
78
- var defaultFile = "/".concat(defaultLanguage, "/").concat(combinedConfig.defaultNS, ".").concat(localeExtension);
79
- var defaultNSPath = path.join(localePath, defaultFile);
80
- var defaultNSExists = fs.existsSync(defaultNSPath);
81
-
82
- if (!defaultNSExists) {
83
- /*
84
- If defaultNS doesn't exist, try to fall back to the deprecated static folder
85
- https://github.com/isaachinman/next-i18next/issues/523
86
- */
87
- var staticDirPath = path.resolve(process.cwd(), STATIC_LOCALE_PATH, defaultFile);
88
- var staticDirExists = fs.existsSync(staticDirPath);
89
-
90
- if (staticDirExists) {
91
- (0, _utils.consoleMessage)('warn', 'next-i18next: Falling back to /static folder, deprecated in next@9.1.*', combinedConfig);
92
- serverLocalePath = STATIC_LOCALE_PATH;
93
- } else if (process.env.NODE_ENV !== 'production') {
94
- throw new Error("Default namespace not found at ".concat(defaultNSPath));
84
+ combinedConfig.preload = allLanguages;
85
+ var hasCustomBackend = userConfig.use && userConfig.use.find(function (b) {
86
+ return b.type === 'backend';
87
+ });
88
+
89
+ if (!hasCustomBackend) {
90
+ var fs = eval("require('fs')");
91
+
92
+ var path = require('path');
93
+
94
+ var serverLocalePath = localePath;
95
+ /*
96
+ Validate defaultNS
97
+ https://github.com/isaachinman/next-i18next/issues/358
98
+ */
99
+
100
+ if (typeof combinedConfig.defaultNS === 'string') {
101
+ var defaultFile = "/".concat(defaultLanguage, "/").concat(combinedConfig.defaultNS, ".").concat(localeExtension);
102
+ var defaultNSPath = path.join(localePath, defaultFile);
103
+ var defaultNSExists = fs.existsSync(defaultNSPath);
104
+
105
+ if (!defaultNSExists) {
106
+ /*
107
+ If defaultNS doesn't exist, try to fall back to the deprecated static folder
108
+ https://github.com/isaachinman/next-i18next/issues/523
109
+ */
110
+ var staticDirPath = path.resolve(process.cwd(), STATIC_LOCALE_PATH, defaultFile);
111
+ var staticDirExists = fs.existsSync(staticDirPath);
112
+
113
+ if (staticDirExists) {
114
+ (0, _utils.consoleMessage)('warn', 'next-i18next: Falling back to /static folder, deprecated in next@9.1.*', combinedConfig);
115
+ serverLocalePath = STATIC_LOCALE_PATH;
116
+ } else if (process.env.NODE_ENV !== 'production') {
117
+ throw new Error("Default namespace not found at ".concat(defaultNSPath));
118
+ }
95
119
  }
96
120
  }
97
- }
98
- /*
99
- Set server side backend
100
- */
121
+ /*
122
+ Set server side backend
123
+ */
101
124
 
102
125
 
103
- combinedConfig.backend = {
104
- loadPath: path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(localeStructure, ".").concat(localeExtension)),
105
- addPath: path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(localeStructure, ".missing.").concat(localeExtension))
106
- };
107
- /*
108
- Set server side preload (languages and namespaces)
109
- */
110
-
111
- combinedConfig.preload = allLanguages;
112
-
113
- if (!combinedConfig.ns) {
114
- var getAllNamespaces = function getAllNamespaces(p) {
115
- return fs.readdirSync(p).map(function (file) {
116
- return file.replace(".".concat(localeExtension), '');
117
- });
126
+ combinedConfig.backend = {
127
+ loadPath: path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(localeStructure, ".").concat(localeExtension)),
128
+ addPath: path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(localeStructure, ".missing.").concat(localeExtension))
118
129
  };
119
-
120
- combinedConfig.ns = getAllNamespaces(path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(defaultLanguage)));
130
+ /*
131
+ Set server side preload (namespaces)
132
+ */
133
+
134
+ if (!combinedConfig.ns) {
135
+ var getAllNamespaces = function getAllNamespaces(p) {
136
+ return fs.readdirSync(p).map(function (file) {
137
+ return file.replace(".".concat(localeExtension), '');
138
+ });
139
+ };
140
+
141
+ combinedConfig.ns = getAllNamespaces(path.resolve(process.cwd(), "".concat(serverLocalePath, "/").concat(defaultLanguage)));
142
+ }
121
143
  }
122
144
  } else {
123
145
  var clientLocalePath = localePath;
@@ -69,6 +69,12 @@ Object.defineProperty(exports, "subpathIsRequired", {
69
69
  return _subpathIsRequired.subpathIsRequired;
70
70
  }
71
71
  });
72
+ Object.defineProperty(exports, "isCIMode", {
73
+ enumerable: true,
74
+ get: function get() {
75
+ return _isCimode.isCIMode;
76
+ }
77
+ });
72
78
 
73
79
  var _addSubpath = require("./add-subpath");
74
80
 
@@ -90,4 +96,6 @@ var _subpathFromLng = require("./subpath-from-lng");
90
96
 
91
97
  var _subpathIsPresent = require("./subpath-is-present");
92
98
 
93
- var _subpathIsRequired = require("./subpath-is-required");
99
+ var _subpathIsRequired = require("./subpath-is-required");
100
+
101
+ var _isCimode = require("./is-cimode");
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isCIMode = void 0;
7
+
8
+ // Detects if in CI mode due language used
9
+ // https://www.i18next.com/overview/api#changelanguage
10
+ var isCIMode = function isCIMode(language) {
11
+ return language === 'cimode';
12
+ };
13
+
14
+ exports.isCIMode = isCIMode;
@@ -74,7 +74,7 @@ var lngPathCorrector = function lngPathCorrector(config, currentRoute, currentLa
74
74
  var originalAs = currentRoute.as,
75
75
  originalHref = currentRoute.href;
76
76
 
77
- if (!allLanguages.includes(currentLanguage)) {
77
+ if (!(0, _index.isCIMode)(currentLanguage) && !allLanguages.includes(currentLanguage)) {
78
78
  throw new Error('Invalid configuration: Current language is not included in all languages array');
79
79
  }
80
80
 
@@ -1,5 +1,5 @@
1
1
  import { defaultConfig } from './default-config';
2
- import { consoleMessage, isServer } from '../utils';
2
+ import { consoleMessage, isServer, isCIMode } from '../utils';
3
3
  const deepMergeObjects = ['backend', 'detection'];
4
4
 
5
5
  const dedupe = names => names.filter((v, i) => names.indexOf(v) === i);
@@ -21,8 +21,12 @@ export const createConfig = userConfig => {
21
21
  Sensible defaults to prevent user duplication
22
22
  */
23
23
 
24
- combinedConfig.allLanguages = dedupe(combinedConfig.otherLanguages.concat([combinedConfig.defaultLanguage]));
25
- combinedConfig.whitelist = combinedConfig.allLanguages;
24
+ combinedConfig.allLanguages = dedupe(combinedConfig.otherLanguages.concat([combinedConfig.defaultLanguage])); // https://github.com/i18next/i18next/blob/master/CHANGELOG.md#1950
25
+
26
+ combinedConfig.supportedLngs = combinedConfig.allLanguages; // temporal backwards compatibility WHITELIST REMOVAL
27
+
28
+ combinedConfig.whitelist = combinedConfig.allLanguages; // end temporal backwards compatibility WHITELIST REMOVAL
29
+
26
30
  const {
27
31
  allLanguages,
28
32
  defaultLanguage,
@@ -30,58 +34,72 @@ export const createConfig = userConfig => {
30
34
  localePath,
31
35
  localeStructure
32
36
  } = combinedConfig;
37
+ /**
38
+ * Skips translation file resolution while in cimode
39
+ * https://github.com/isaachinman/next-i18next/pull/851#discussion_r503113620
40
+ */
33
41
 
34
- if (isServer()) {
35
- const fs = eval("require('fs')");
36
-
37
- const path = require('path');
42
+ if (isCIMode(defaultLanguage)) {
43
+ return combinedConfig;
44
+ }
38
45
 
39
- let serverLocalePath = localePath;
46
+ if (isServer()) {
40
47
  /*
41
- Validate defaultNS
42
- https://github.com/isaachinman/next-i18next/issues/358
48
+ On Server side preload (languages)
43
49
  */
44
-
45
- if (typeof combinedConfig.defaultNS === 'string') {
46
- const defaultFile = `/${defaultLanguage}/${combinedConfig.defaultNS}.${localeExtension}`;
47
- const defaultNSPath = path.join(localePath, defaultFile);
48
- const defaultNSExists = fs.existsSync(defaultNSPath);
49
-
50
- if (!defaultNSExists) {
51
- /*
52
- If defaultNS doesn't exist, try to fall back to the deprecated static folder
53
- https://github.com/isaachinman/next-i18next/issues/523
54
- */
55
- const staticDirPath = path.resolve(process.cwd(), STATIC_LOCALE_PATH, defaultFile);
56
- const staticDirExists = fs.existsSync(staticDirPath);
57
-
58
- if (staticDirExists) {
59
- consoleMessage('warn', 'next-i18next: Falling back to /static folder, deprecated in next@9.1.*', combinedConfig);
60
- serverLocalePath = STATIC_LOCALE_PATH;
61
- } else if (process.env.NODE_ENV !== 'production') {
62
- throw new Error(`Default namespace not found at ${defaultNSPath}`);
50
+ combinedConfig.preload = allLanguages;
51
+ const hasCustomBackend = userConfig.use && userConfig.use.find(b => b.type === 'backend');
52
+
53
+ if (!hasCustomBackend) {
54
+ const fs = eval("require('fs')");
55
+
56
+ const path = require('path');
57
+
58
+ let serverLocalePath = localePath;
59
+ /*
60
+ Validate defaultNS
61
+ https://github.com/isaachinman/next-i18next/issues/358
62
+ */
63
+
64
+ if (typeof combinedConfig.defaultNS === 'string') {
65
+ const defaultFile = `/${defaultLanguage}/${combinedConfig.defaultNS}.${localeExtension}`;
66
+ const defaultNSPath = path.join(localePath, defaultFile);
67
+ const defaultNSExists = fs.existsSync(defaultNSPath);
68
+
69
+ if (!defaultNSExists) {
70
+ /*
71
+ If defaultNS doesn't exist, try to fall back to the deprecated static folder
72
+ https://github.com/isaachinman/next-i18next/issues/523
73
+ */
74
+ const staticDirPath = path.resolve(process.cwd(), STATIC_LOCALE_PATH, defaultFile);
75
+ const staticDirExists = fs.existsSync(staticDirPath);
76
+
77
+ if (staticDirExists) {
78
+ consoleMessage('warn', 'next-i18next: Falling back to /static folder, deprecated in next@9.1.*', combinedConfig);
79
+ serverLocalePath = STATIC_LOCALE_PATH;
80
+ } else if (process.env.NODE_ENV !== 'production') {
81
+ throw new Error(`Default namespace not found at ${defaultNSPath}`);
82
+ }
63
83
  }
64
84
  }
65
- }
66
- /*
67
- Set server side backend
68
- */
69
-
85
+ /*
86
+ Set server side backend
87
+ */
70
88
 
71
- combinedConfig.backend = {
72
- loadPath: path.resolve(process.cwd(), `${serverLocalePath}/${localeStructure}.${localeExtension}`),
73
- addPath: path.resolve(process.cwd(), `${serverLocalePath}/${localeStructure}.missing.${localeExtension}`)
74
- };
75
- /*
76
- Set server side preload (languages and namespaces)
77
- */
78
89
 
79
- combinedConfig.preload = allLanguages;
90
+ combinedConfig.backend = {
91
+ loadPath: path.resolve(process.cwd(), `${serverLocalePath}/${localeStructure}.${localeExtension}`),
92
+ addPath: path.resolve(process.cwd(), `${serverLocalePath}/${localeStructure}.missing.${localeExtension}`)
93
+ };
94
+ /*
95
+ Set server side preload (namespaces)
96
+ */
80
97
 
81
- if (!combinedConfig.ns) {
82
- const getAllNamespaces = p => fs.readdirSync(p).map(file => file.replace(`.${localeExtension}`, ''));
98
+ if (!combinedConfig.ns) {
99
+ const getAllNamespaces = p => fs.readdirSync(p).map(file => file.replace(`.${localeExtension}`, ''));
83
100
 
84
- combinedConfig.ns = getAllNamespaces(path.resolve(process.cwd(), `${serverLocalePath}/${defaultLanguage}`));
101
+ combinedConfig.ns = getAllNamespaces(path.resolve(process.cwd(), `${serverLocalePath}/${defaultLanguage}`));
102
+ }
85
103
  }
86
104
  } else {
87
105
  let clientLocalePath = localePath;
@@ -8,4 +8,5 @@ export { redirectWithoutCache } from './redirect-without-cache';
8
8
  export { removeSubpath } from './remove-subpath';
9
9
  export { subpathFromLng } from './subpath-from-lng';
10
10
  export { subpathIsPresent } from './subpath-is-present';
11
- export { subpathIsRequired } from './subpath-is-required';
11
+ export { subpathIsRequired } from './subpath-is-required';
12
+ export { isCIMode } from './is-cimode';
@@ -0,0 +1,3 @@
1
+ // Detects if in CI mode due language used
2
+ // https://www.i18next.com/overview/api#changelanguage
3
+ export const isCIMode = language => language === 'cimode';
@@ -1,5 +1,5 @@
1
1
  import { format as formatUrl, parse as parseUrl } from 'url';
2
- import { removeSubpath, subpathIsPresent, subpathIsRequired, subpathFromLng } from './index';
2
+ import { removeSubpath, subpathIsPresent, subpathIsRequired, subpathFromLng, isCIMode } from './index';
3
3
 
4
4
  const parseAs = (originalAs, href) => {
5
5
  const asType = typeof originalAs;
@@ -48,7 +48,7 @@ export const lngPathCorrector = (config, currentRoute, currentLanguage) => {
48
48
  href: originalHref
49
49
  } = currentRoute;
50
50
 
51
- if (!allLanguages.includes(currentLanguage)) {
51
+ if (!isCIMode(currentLanguage) && !allLanguages.includes(currentLanguage)) {
52
52
  throw new Error('Invalid configuration: Current language is not included in all languages array');
53
53
  }
54
54
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "6.0.1",
3
+ "version": "7.0.1",
4
4
  "repository": "git@github.com:isaachinman/next-i18next.git",
5
5
  "author": "Isaac Hinman <isaac@isaachinman.com>",
6
6
  "funding": {
@@ -50,7 +50,7 @@
50
50
  "bundlesize": [
51
51
  {
52
52
  "path": "./examples/simple/.next/static/chunks/commons*.js",
53
- "maxSize": "65 kB"
53
+ "maxSize": "67 kB"
54
54
  },
55
55
  {
56
56
  "path": "./examples/simple/.next/static/chunks/main*.js",
@@ -109,7 +109,7 @@
109
109
  "i18next-http-middleware": "^3.0.2",
110
110
  "path-match": "^1.2.4",
111
111
  "prop-types": "^15.6.2",
112
- "react-i18next": "^11.7.0",
112
+ "react-i18next": "^11.7.3",
113
113
  "url": "^0.11.0"
114
114
  },
115
115
  "peerDependencies": {
package/types.d.ts CHANGED
@@ -32,7 +32,11 @@ export type InitConfig = {
32
32
  export type Config = {
33
33
  fallbackLng: boolean;
34
34
  allLanguages: string[];
35
+ // https://github.com/i18next/i18next/blob/master/CHANGELOG.md#1950
36
+ supportedLngs: string[];
37
+ // temporal backwards compatibility WHITELIST REMOVAL
35
38
  whitelist: string[];
39
+ // end temporal backwards compatibility WHITELIST REMOVAL
36
40
  preload: string[];
37
41
  } & InitConfig
38
42
 
@@ -45,7 +49,6 @@ export type Trans = (props: TransProps) => any
45
49
  export type Link = React.ComponentClass<LinkProps>
46
50
  export type Router = SingletonRouter
47
51
  export type UseTranslation = typeof useTranslation
48
- export type I18nContext = typeof I18nContext
49
52
  export type AppWithTranslation = <P extends object>(Component: React.ComponentType<P> | React.ElementType<P>) => any
50
53
  export type TFunction = I18NextTFunction
51
54
  export type I18n = i18n
@@ -53,6 +56,11 @@ export type WithTranslationHocType = typeof withTranslation
53
56
  export type WithTranslation = ReactI18nextWithTranslation
54
57
  export type InitPromise = Promise<TFunction>
55
58
 
59
+ export {
60
+ I18nContext,
61
+ withTranslation,
62
+ }
63
+
56
64
  declare class NextI18Next {
57
65
  constructor(config: InitConfig);
58
66
  Trans: Trans