next-i18next 8.7.0 → 9.0.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
@@ -129,7 +129,7 @@ Note that `serverSideTranslations` must be imported from `next-i18next/serverSid
129
129
 
130
130
  Also, note that `serverSideTranslations` is not compatible with `getInitialProps`, as it _only_ can execute in a server environment, whereas `getInitialProps` is called on the client side when navigating between pages.
131
131
 
132
- The `serverSideTranslations` HOC is primarily responsible for passing translations and configuration options into pages, as props.
132
+ The `serverSideTranslations` HOC is primarily responsible for passing translations and configuration options into pages, as props – you need to add it to any page that has translations.
133
133
 
134
134
  ### useTranslation
135
135
 
@@ -236,6 +236,24 @@ This option will reload your translations whenever `serverSideTranslations` is c
236
236
 
237
237
  All other [i18next options](https://www.i18next.com/overview/configuration-options) can be passed in as well.
238
238
 
239
+ #### Loading Namespaces Dynamically Client Side
240
+
241
+ In some use cases, you might want to load a translation file dynamically without having to use `serverSideTranslations`. This can be especially useful for lazy-loaded components that you don't want slowing down pages.
242
+
243
+ This can easily be done by using [addResourceBundle](https://www.i18next.com/how-to/add-or-load-translations#add-after-init):
244
+
245
+ ```tsx
246
+ import { i18n } from 'next-i18next'
247
+
248
+ const Component = () => {
249
+ const { locale } = useRouter()
250
+
251
+ useEffect(() => {
252
+ i18n.addResourceBundle(locale, '<namespace name>')
253
+ }, [])
254
+ }
255
+ ```
256
+
239
257
  ## Migration to v8
240
258
 
241
259
  To migrate from previous versions to the version 8, check out the [v8-migration guide](https://github.com/isaachinman/next-i18next/tree/master/docs/v8-migration.md)
@@ -67,7 +67,7 @@ var _reactI18next = require("react-i18next");
67
67
 
68
68
  var _createConfig = require("./config/createConfig");
69
69
 
70
- var _createClient2 = _interopRequireDefault(require("./createClient"));
70
+ var _createClient = _interopRequireDefault(require("./createClient"));
71
71
 
72
72
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
73
73
 
@@ -86,18 +86,20 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
86
86
  var configOverride = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
87
87
 
88
88
  var AppWithTranslation = function AppWithTranslation(props) {
89
- var _props$pageProps;
89
+ var _ref = props.pageProps,
90
+ _nextI18Next = _ref._nextI18Next;
91
+ var locale = null; // Memoize the instance and only re-initialize when either:
92
+ // 1. The route changes (non-shallowly)
93
+ // 2. Router locale changes
90
94
 
91
- var i18n = null;
92
- var locale = null;
93
-
94
- if (props !== null && props !== void 0 && (_props$pageProps = props.pageProps) !== null && _props$pageProps !== void 0 && _props$pageProps._nextI18Next) {
95
+ var i18n = (0, _react.useMemo)(function () {
95
96
  var _userConfig;
96
97
 
97
- var userConfig = props.pageProps._nextI18Next.userConfig;
98
- var _props$pageProps$_nex = props.pageProps._nextI18Next,
99
- initialI18nStore = _props$pageProps$_nex.initialI18nStore,
100
- initialLocale = _props$pageProps$_nex.initialLocale;
98
+ if (!_nextI18Next) return null;
99
+ var userConfig = _nextI18Next.userConfig;
100
+ var initialI18nStore = _nextI18Next.initialI18nStore,
101
+ initialLocale = _nextI18Next.initialLocale;
102
+ locale = initialLocale;
101
103
 
102
104
  if (userConfig === null && configOverride === null) {
103
105
  throw new Error('appWithTranslation was called without a next-i18next config');
@@ -111,21 +113,15 @@ var appWithTranslation = function appWithTranslation(WrappedComponent) {
111
113
  throw new Error('appWithTranslation was called without config.i18n');
112
114
  }
113
115
 
114
- locale = initialLocale;
115
-
116
- var _createClient = (0, _createClient2["default"])(_objectSpread(_objectSpread({}, (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
117
- lng: initialLocale
116
+ var instance = (0, _createClient["default"])(_objectSpread(_objectSpread({}, (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
117
+ lng: locale
118
118
  }))), {}, {
119
- lng: initialLocale,
119
+ lng: locale,
120
120
  resources: initialI18nStore
121
- }));
122
-
123
- i18n = _createClient.i18n;
124
- (0, _react.useMemo)(function () {
125
- exports.globalI18n = globalI18n = i18n;
126
- }, [i18n]);
127
- }
128
-
121
+ })).i18n;
122
+ exports.globalI18n = globalI18n = instance;
123
+ return instance;
124
+ }, [_nextI18Next, locale]);
129
125
  return i18n !== null ? __jsx(_reactI18next.I18nextProvider, {
130
126
  i18n: i18n
131
127
  }, __jsx(WrappedComponent, (0, _extends2["default"])({
@@ -9,21 +9,25 @@ export { Trans, useTranslation, withTranslation } from 'react-i18next';
9
9
  export let globalI18n = null;
10
10
  export const appWithTranslation = (WrappedComponent, configOverride = null) => {
11
11
  const AppWithTranslation = props => {
12
- var _props$pageProps;
13
-
14
- let i18n = null;
15
- let locale = null;
16
-
17
- if (props !== null && props !== void 0 && (_props$pageProps = props.pageProps) !== null && _props$pageProps !== void 0 && _props$pageProps._nextI18Next) {
12
+ const {
13
+ _nextI18Next
14
+ } = props.pageProps;
15
+ let locale = null; // Memoize the instance and only re-initialize when either:
16
+ // 1. The route changes (non-shallowly)
17
+ // 2. Router locale changes
18
+
19
+ const i18n = useMemo(() => {
18
20
  var _userConfig;
19
21
 
22
+ if (!_nextI18Next) return null;
20
23
  let {
21
24
  userConfig
22
- } = props.pageProps._nextI18Next;
25
+ } = _nextI18Next;
23
26
  const {
24
27
  initialI18nStore,
25
28
  initialLocale
26
- } = props.pageProps._nextI18Next;
29
+ } = _nextI18Next;
30
+ locale = initialLocale;
27
31
 
28
32
  if (userConfig === null && configOverride === null) {
29
33
  throw new Error('appWithTranslation was called without a next-i18next config');
@@ -37,20 +41,15 @@ export const appWithTranslation = (WrappedComponent, configOverride = null) => {
37
41
  throw new Error('appWithTranslation was called without config.i18n');
38
42
  }
39
43
 
40
- locale = initialLocale;
41
- ({
42
- i18n
43
- } = createClient({ ...createConfig({ ...userConfig,
44
- lng: initialLocale
44
+ const instance = createClient({ ...createConfig({ ...userConfig,
45
+ lng: locale
45
46
  }),
46
- lng: initialLocale,
47
+ lng: locale,
47
48
  resources: initialI18nStore
48
- }));
49
- useMemo(() => {
50
- globalI18n = i18n;
51
- }, [i18n]);
52
- }
53
-
49
+ }).i18n;
50
+ globalI18n = instance;
51
+ return instance;
52
+ }, [_nextI18Next, locale]);
54
53
  return i18n !== null ? /*#__PURE__*/React.createElement(I18nextProvider, {
55
54
  i18n: i18n
56
55
  }, /*#__PURE__*/React.createElement(WrappedComponent, _extends({
@@ -17,18 +17,20 @@ export var appWithTranslation = function appWithTranslation(WrappedComponent) {
17
17
  var configOverride = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
18
18
 
19
19
  var AppWithTranslation = function AppWithTranslation(props) {
20
- var _props$pageProps;
20
+ var _ref = props.pageProps,
21
+ _nextI18Next = _ref._nextI18Next;
22
+ var locale = null; // Memoize the instance and only re-initialize when either:
23
+ // 1. The route changes (non-shallowly)
24
+ // 2. Router locale changes
21
25
 
22
- var i18n = null;
23
- var locale = null;
24
-
25
- if (props !== null && props !== void 0 && (_props$pageProps = props.pageProps) !== null && _props$pageProps !== void 0 && _props$pageProps._nextI18Next) {
26
+ var i18n = useMemo(function () {
26
27
  var _userConfig;
27
28
 
28
- var userConfig = props.pageProps._nextI18Next.userConfig;
29
- var _props$pageProps$_nex = props.pageProps._nextI18Next,
30
- initialI18nStore = _props$pageProps$_nex.initialI18nStore,
31
- initialLocale = _props$pageProps$_nex.initialLocale;
29
+ if (!_nextI18Next) return null;
30
+ var userConfig = _nextI18Next.userConfig;
31
+ var initialI18nStore = _nextI18Next.initialI18nStore,
32
+ initialLocale = _nextI18Next.initialLocale;
33
+ locale = initialLocale;
32
34
 
33
35
  if (userConfig === null && configOverride === null) {
34
36
  throw new Error('appWithTranslation was called without a next-i18next config');
@@ -42,21 +44,15 @@ export var appWithTranslation = function appWithTranslation(WrappedComponent) {
42
44
  throw new Error('appWithTranslation was called without config.i18n');
43
45
  }
44
46
 
45
- locale = initialLocale;
46
-
47
- var _createClient = createClient(_objectSpread(_objectSpread({}, createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
48
- lng: initialLocale
47
+ var instance = createClient(_objectSpread(_objectSpread({}, createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
48
+ lng: locale
49
49
  }))), {}, {
50
- lng: initialLocale,
50
+ lng: locale,
51
51
  resources: initialI18nStore
52
- }));
53
-
54
- i18n = _createClient.i18n;
55
- useMemo(function () {
56
- globalI18n = i18n;
57
- }, [i18n]);
58
- }
59
-
52
+ })).i18n;
53
+ globalI18n = instance;
54
+ return instance;
55
+ }, [_nextI18Next, locale]);
60
56
  return i18n !== null ? __jsx(I18nextProvider, {
61
57
  i18n: i18n
62
58
  }, __jsx(WrappedComponent, _extends({
@@ -8,4 +8,4 @@ declare type AppProps = NextJsAppProps & {
8
8
  pageProps: SSRConfig;
9
9
  };
10
10
  export declare let globalI18n: I18NextClient | null;
11
- export declare const appWithTranslation: (WrappedComponent: React.ComponentType<AppProps> | React.ElementType<AppProps>, configOverride?: UserConfig | null) => ((props: AppProps) => JSX.Element) & hoistNonReactStatics.NonReactStatics<React.ComponentClass<AppProps, any> | React.FunctionComponent<AppProps>, {}>;
11
+ export declare const appWithTranslation: <Props extends AppProps = AppProps>(WrappedComponent: React.ComponentType<Props>, configOverride?: UserConfig | null) => ((props: Props) => JSX.Element) & hoistNonReactStatics.NonReactStatics<React.ComponentType<Props>, {}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "8.7.0",
3
+ "version": "9.0.0",
4
4
  "repository": "git@github.com:isaachinman/next-i18next.git",
5
5
  "author": "Isaac Hinman <isaac@isaachinman.com>",
6
6
  "funding": {
@@ -12,7 +12,7 @@
12
12
  "types": "dist/types/types.d.ts",
13
13
  "license": "MIT",
14
14
  "engines": {
15
- "node": ">=10"
15
+ "node": ">=12"
16
16
  },
17
17
  "description": "The easiest way to translate your NextJs apps.",
18
18
  "keywords": [
@@ -73,7 +73,8 @@
73
73
  "@babel/preset-react": "^7.10.4",
74
74
  "@babel/preset-typescript": "^7.10.4",
75
75
  "@testing-library/react": "^11.2.5",
76
- "@types/jest": "^26.0.20",
76
+ "@types/i18next-fs-backend": "^1.1.2",
77
+ "@types/jest": "^27.0.1",
77
78
  "@types/node": "^16.7.1",
78
79
  "@types/react": "^17.0.3",
79
80
  "@types/react-dom": "^17.0.3",
@@ -104,10 +105,9 @@
104
105
  "dependencies": {
105
106
  "@babel/runtime": "^7.13.17",
106
107
  "@types/hoist-non-react-statics": "^3.3.1",
107
- "@types/i18next-fs-backend": "^1.0.0",
108
108
  "core-js": "^3",
109
109
  "hoist-non-react-statics": "^3.2.0",
110
- "i18next": "^20.1.0",
110
+ "i18next": "^21.4.1",
111
111
  "i18next-fs-backend": "^1.0.7",
112
112
  "react-i18next": "^11.8.13"
113
113
  },