next-i18next 11.2.2 → 11.3.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
@@ -159,7 +159,32 @@ To do that, you can pass an array of required namespaces for each page into `ser
159
159
 
160
160
  Note: `useTranslation` provides namespaces to the component that you use it in. However, `serverSideTranslations` provides the total available namespaces to the entire React tree and belongs on the page level. Both are required.
161
161
 
162
- ### 5. Advanced configuration
162
+ ### 5. Declaring locale dependencies
163
+
164
+ By default, `next-i18next` will send _only the active locale_ down to the client on each request. This helps reduce the size of the
165
+ initial payload sent to the client. However in some cases one may need the translations for other languages at runtime too. For example
166
+ when using [getFixedT](https://www.i18next.com/overview/api#getfixedt) of `useTranslation` hook.
167
+
168
+ To change the behavior and load extra locales just pass in an array of locales as the last argument to `serverSideTranslations`.
169
+
170
+ ```diff
171
+ import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
172
+
173
+ export async function getStaticProps({ locale }) {
174
+ return {
175
+ props: {
176
+ - ...(await serverSideTranslations(locale, ['common', 'footer'])),
177
+ + ...(await serverSideTranslations(locale, ['common', 'footer'], null, ['en', 'no'])),
178
+ },
179
+ };
180
+ }
181
+ ```
182
+
183
+ As a result the translations for both `no` and `en` locales will always be loaded regardless of the current language.
184
+
185
+ > Note: The extra argument should be added to all pages that use `getFixedT` function.
186
+
187
+ ### 6. Advanced configuration
163
188
 
164
189
  #### Passing other config options
165
190
 
@@ -101,18 +101,18 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
101
101
 
102
102
  var DEFAULT_CONFIG_PATH = './next-i18next.config.js';
103
103
 
104
- var getFallbackLocales = function getFallbackLocales(fallbackLng) {
105
- if (typeof fallbackLng === 'string') {
106
- return [fallbackLng];
104
+ var flatLocales = function flatLocales(locales) {
105
+ if (typeof locales === 'string') {
106
+ return [locales];
107
107
  }
108
108
 
109
- if (Array.isArray(fallbackLng)) {
110
- return fallbackLng;
109
+ if (Array.isArray(locales)) {
110
+ return locales;
111
111
  }
112
112
 
113
- if ((0, _typeof2["default"])(fallbackLng) === 'object' && fallbackLng !== null) {
114
- return Object.values(fallbackLng).reduce(function (all, locales) {
115
- return [].concat((0, _toConsumableArray2["default"])(all), (0, _toConsumableArray2["default"])(locales));
113
+ if ((0, _typeof2["default"])(locales) === 'object' && locales !== null) {
114
+ return Object.values(locales).reduce(function (all, items) {
115
+ return [].concat((0, _toConsumableArray2["default"])(all), (0, _toConsumableArray2["default"])(items));
116
116
  }, []);
117
117
  }
118
118
 
@@ -143,6 +143,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
143
143
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(initialLocale) {
144
144
  var namespacesRequired,
145
145
  configOverride,
146
+ extraLocales,
146
147
  userConfig,
147
148
  config,
148
149
  localeExtension,
@@ -163,78 +164,79 @@ var serverSideTranslations = /*#__PURE__*/function () {
163
164
  case 0:
164
165
  namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
165
166
  configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
167
+ extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
166
168
 
167
169
  if (!(typeof initialLocale !== 'string')) {
168
- _context.next = 4;
170
+ _context.next = 5;
169
171
  break;
170
172
  }
171
173
 
172
174
  throw new Error('Initial locale argument was not passed into serverSideTranslations');
173
175
 
174
- case 4:
176
+ case 5:
175
177
  userConfig = configOverride;
176
178
 
177
179
  if (!(!userConfig && _fs["default"].existsSync(_path["default"].resolve(DEFAULT_CONFIG_PATH)))) {
178
- _context.next = 9;
180
+ _context.next = 10;
179
181
  break;
180
182
  }
181
183
 
182
- _context.next = 8;
184
+ _context.next = 9;
183
185
  return Promise.resolve("".concat(_path["default"].resolve(DEFAULT_CONFIG_PATH))).then(function (s) {
184
186
  return _interopRequireWildcard(require(s));
185
187
  });
186
188
 
187
- case 8:
189
+ case 9:
188
190
  userConfig = _context.sent;
189
191
 
190
- case 9:
192
+ case 10:
191
193
  if (!(userConfig === null)) {
192
- _context.next = 11;
194
+ _context.next = 12;
193
195
  break;
194
196
  }
195
197
 
196
198
  throw new Error('next-i18next was unable to find a user config');
197
199
 
198
- case 11:
200
+ case 12:
199
201
  config = (0, _createConfig.createConfig)(_objectSpread(_objectSpread({}, userConfig), {}, {
200
202
  lng: initialLocale
201
203
  }));
202
204
  localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
203
205
 
204
206
  if (!reloadOnPrerender) {
205
- _context.next = 16;
207
+ _context.next = 17;
206
208
  break;
207
209
  }
208
210
 
209
- _context.next = 16;
211
+ _context.next = 17;
210
212
  return _appWithTranslation.globalI18n === null || _appWithTranslation.globalI18n === void 0 ? void 0 : _appWithTranslation.globalI18n.reloadResources();
211
213
 
212
- case 16:
214
+ case 17:
213
215
  _createClient = (0, _createClient2["default"])(_objectSpread(_objectSpread({}, config), {}, {
214
216
  lng: initialLocale
215
217
  })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
216
- _context.next = 19;
218
+ _context.next = 20;
217
219
  return initPromise;
218
220
 
219
- case 19:
221
+ case 20:
220
222
  initialI18nStore = (0, _defineProperty2["default"])({}, initialLocale, {});
221
- getFallbackLocales(fallbackLng).forEach(function (lng) {
223
+ flatLocales(fallbackLng).concat(flatLocales(extraLocales)).forEach(function (lng) {
222
224
  initialI18nStore[lng] = {};
223
225
  });
224
226
 
225
227
  if (Array.isArray(namespacesRequired)) {
226
- _context.next = 27;
228
+ _context.next = 28;
227
229
  break;
228
230
  }
229
231
 
230
232
  if (!(typeof localePath === 'function')) {
231
- _context.next = 24;
233
+ _context.next = 25;
232
234
  break;
233
235
  }
234
236
 
235
237
  throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
236
238
 
237
- case 24:
239
+ case 25:
238
240
  getLocaleNamespaces = function getLocaleNamespaces(path) {
239
241
  return _fs["default"].readdirSync(path).map(function (file) {
240
242
  return file.replace(".".concat(localeExtension), '');
@@ -246,7 +248,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
246
248
  });
247
249
  namespacesRequired = flatNamespaces(namespacesByLocale);
248
250
 
249
- case 27:
251
+ case 28:
250
252
  namespacesRequired.forEach(function (ns) {
251
253
  for (var locale in initialI18nStore) {
252
254
  initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
@@ -261,7 +263,7 @@ var serverSideTranslations = /*#__PURE__*/function () {
261
263
  }
262
264
  });
263
265
 
264
- case 29:
266
+ case 30:
265
267
  case "end":
266
268
  return _context.stop();
267
269
  }
@@ -5,17 +5,17 @@ import createClient from './createClient';
5
5
  import { globalI18n } from './appWithTranslation';
6
6
  const DEFAULT_CONFIG_PATH = './next-i18next.config.js';
7
7
 
8
- const getFallbackLocales = fallbackLng => {
9
- if (typeof fallbackLng === 'string') {
10
- return [fallbackLng];
8
+ const flatLocales = locales => {
9
+ if (typeof locales === 'string') {
10
+ return [locales];
11
11
  }
12
12
 
13
- if (Array.isArray(fallbackLng)) {
14
- return fallbackLng;
13
+ if (Array.isArray(locales)) {
14
+ return locales;
15
15
  }
16
16
 
17
- if (typeof fallbackLng === 'object' && fallbackLng !== null) {
18
- return Object.values(fallbackLng).reduce((all, locales) => [...all, ...locales], []);
17
+ if (typeof locales === 'object' && locales !== null) {
18
+ return Object.values(locales).reduce((all, items) => [...all, ...items], []);
19
19
  }
20
20
 
21
21
  return [];
@@ -31,7 +31,7 @@ const flatNamespaces = namespacesByLocale => {
31
31
  return Array.from(new Set(allNamespaces));
32
32
  };
33
33
 
34
- export const serverSideTranslations = async (initialLocale, namespacesRequired = undefined, configOverride = null) => {
34
+ export const serverSideTranslations = async (initialLocale, namespacesRequired = undefined, configOverride = null, extraLocales = false) => {
35
35
  if (typeof initialLocale !== 'string') {
36
36
  throw new Error('Initial locale argument was not passed into serverSideTranslations');
37
37
  }
@@ -70,7 +70,7 @@ export const serverSideTranslations = async (initialLocale, namespacesRequired =
70
70
  const initialI18nStore = {
71
71
  [initialLocale]: {}
72
72
  };
73
- getFallbackLocales(fallbackLng).forEach(lng => {
73
+ flatLocales(fallbackLng).concat(flatLocales(extraLocales)).forEach(lng => {
74
74
  initialI18nStore[lng] = {};
75
75
  });
76
76
 
@@ -21,18 +21,18 @@ import createClient from './createClient';
21
21
  import { globalI18n } from './appWithTranslation';
22
22
  var DEFAULT_CONFIG_PATH = './next-i18next.config.js';
23
23
 
24
- var getFallbackLocales = function getFallbackLocales(fallbackLng) {
25
- if (typeof fallbackLng === 'string') {
26
- return [fallbackLng];
24
+ var flatLocales = function flatLocales(locales) {
25
+ if (typeof locales === 'string') {
26
+ return [locales];
27
27
  }
28
28
 
29
- if (Array.isArray(fallbackLng)) {
30
- return fallbackLng;
29
+ if (Array.isArray(locales)) {
30
+ return locales;
31
31
  }
32
32
 
33
- if (_typeof(fallbackLng) === 'object' && fallbackLng !== null) {
34
- return Object.values(fallbackLng).reduce(function (all, locales) {
35
- return [].concat(_toConsumableArray(all), _toConsumableArray(locales));
33
+ if (_typeof(locales) === 'object' && locales !== null) {
34
+ return Object.values(locales).reduce(function (all, items) {
35
+ return [].concat(_toConsumableArray(all), _toConsumableArray(items));
36
36
  }, []);
37
37
  }
38
38
 
@@ -63,6 +63,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
63
63
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(initialLocale) {
64
64
  var namespacesRequired,
65
65
  configOverride,
66
+ extraLocales,
66
67
  userConfig,
67
68
  config,
68
69
  localeExtension,
@@ -83,76 +84,77 @@ export var serverSideTranslations = /*#__PURE__*/function () {
83
84
  case 0:
84
85
  namespacesRequired = _args.length > 1 && _args[1] !== undefined ? _args[1] : undefined;
85
86
  configOverride = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
87
+ extraLocales = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
86
88
 
87
89
  if (!(typeof initialLocale !== 'string')) {
88
- _context.next = 4;
90
+ _context.next = 5;
89
91
  break;
90
92
  }
91
93
 
92
94
  throw new Error('Initial locale argument was not passed into serverSideTranslations');
93
95
 
94
- case 4:
96
+ case 5:
95
97
  userConfig = configOverride;
96
98
 
97
99
  if (!(!userConfig && fs.existsSync(path.resolve(DEFAULT_CONFIG_PATH)))) {
98
- _context.next = 9;
100
+ _context.next = 10;
99
101
  break;
100
102
  }
101
103
 
102
- _context.next = 8;
104
+ _context.next = 9;
103
105
  return import(path.resolve(DEFAULT_CONFIG_PATH));
104
106
 
105
- case 8:
107
+ case 9:
106
108
  userConfig = _context.sent;
107
109
 
108
- case 9:
110
+ case 10:
109
111
  if (!(userConfig === null)) {
110
- _context.next = 11;
112
+ _context.next = 12;
111
113
  break;
112
114
  }
113
115
 
114
116
  throw new Error('next-i18next was unable to find a user config');
115
117
 
116
- case 11:
118
+ case 12:
117
119
  config = createConfig(_objectSpread(_objectSpread({}, userConfig), {}, {
118
120
  lng: initialLocale
119
121
  }));
120
122
  localeExtension = config.localeExtension, localePath = config.localePath, fallbackLng = config.fallbackLng, reloadOnPrerender = config.reloadOnPrerender;
121
123
 
122
124
  if (!reloadOnPrerender) {
123
- _context.next = 16;
125
+ _context.next = 17;
124
126
  break;
125
127
  }
126
128
 
127
- _context.next = 16;
129
+ _context.next = 17;
128
130
  return globalI18n === null || globalI18n === void 0 ? void 0 : globalI18n.reloadResources();
129
131
 
130
- case 16:
132
+ case 17:
131
133
  _createClient = createClient(_objectSpread(_objectSpread({}, config), {}, {
132
134
  lng: initialLocale
133
135
  })), i18n = _createClient.i18n, initPromise = _createClient.initPromise;
134
- _context.next = 19;
136
+ _context.next = 20;
135
137
  return initPromise;
136
138
 
137
- case 19:
139
+ case 20:
138
140
  initialI18nStore = _defineProperty({}, initialLocale, {});
139
- getFallbackLocales(fallbackLng).forEach(function (lng) {
141
+ flatLocales(fallbackLng).concat(flatLocales(extraLocales)).forEach(function (lng) {
140
142
  initialI18nStore[lng] = {};
141
143
  });
142
144
 
143
145
  if (Array.isArray(namespacesRequired)) {
144
- _context.next = 27;
146
+ _context.next = 28;
145
147
  break;
146
148
  }
147
149
 
148
150
  if (!(typeof localePath === 'function')) {
149
- _context.next = 24;
151
+ _context.next = 25;
150
152
  break;
151
153
  }
152
154
 
153
155
  throw new Error('Must provide namespacesRequired to serverSideTranslations when using a function as localePath');
154
156
 
155
- case 24:
157
+ case 25:
156
158
  getLocaleNamespaces = function getLocaleNamespaces(path) {
157
159
  return fs.readdirSync(path).map(function (file) {
158
160
  return file.replace(".".concat(localeExtension), '');
@@ -164,7 +166,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
164
166
  });
165
167
  namespacesRequired = flatNamespaces(namespacesByLocale);
166
168
 
167
- case 27:
169
+ case 28:
168
170
  namespacesRequired.forEach(function (ns) {
169
171
  for (var locale in initialI18nStore) {
170
172
  initialI18nStore[locale][ns] = (i18n.services.resourceStore.data[locale] || {})[ns] || {};
@@ -179,7 +181,7 @@ export var serverSideTranslations = /*#__PURE__*/function () {
179
181
  }
180
182
  });
181
183
 
182
- case 29:
184
+ case 30:
183
185
  case "end":
184
186
  return _context.stop();
185
187
  }
@@ -1,2 +1,2 @@
1
1
  import { UserConfig, SSRConfig } from './types';
2
- export declare const serverSideTranslations: (initialLocale: string, namespacesRequired?: string[] | undefined, configOverride?: UserConfig | null) => Promise<SSRConfig>;
2
+ export declare const serverSideTranslations: (initialLocale: string, namespacesRequired?: string[] | undefined, configOverride?: UserConfig | null, extraLocales?: string[] | false) => Promise<SSRConfig>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-i18next",
3
- "version": "11.2.2",
3
+ "version": "11.3.0",
4
4
  "repository": "git@github.com:i18next/next-i18next.git",
5
5
  "author": "i18next",
6
6
  "funding": [