Skip to content

Commit fae8a57

Browse files
committed
feat: add support for options.lng, options.fallbackLng, options.supportedLngs as a language source
1 parent ea6a877 commit fae8a57

File tree

5 files changed

+177
-31
lines changed

5 files changed

+177
-31
lines changed

__tests__/client-hmr.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,72 @@ describe('client-hmr', () => {
270270
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
271271
});
272272

273+
it('should support options.supportedLngs as a language source', async () => {
274+
i18nMock.options = {
275+
backend: {},
276+
ns: ['nested/name-space'],
277+
fallbackNS: ['nested/fallback-name-space'],
278+
supportedLngs: ['en-US'],
279+
};
280+
i18nMock.language = 'en-US';
281+
i18nMock.languages = [];
282+
283+
applyClientHMR(i18nMock);
284+
285+
await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);
286+
287+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
288+
['en-US'],
289+
['nested/fallback-name-space'],
290+
expect.any(Function)
291+
);
292+
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
293+
});
294+
295+
it('should support options.lng as a language source', async () => {
296+
i18nMock.options = {
297+
backend: {},
298+
ns: ['nested/name-space'],
299+
fallbackNS: ['nested/fallback-name-space'],
300+
lng: 'en-US',
301+
};
302+
i18nMock.language = 'en-US';
303+
i18nMock.languages = [];
304+
305+
applyClientHMR(i18nMock);
306+
307+
await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);
308+
309+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
310+
['en-US'],
311+
['nested/fallback-name-space'],
312+
expect.any(Function)
313+
);
314+
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
315+
});
316+
317+
it('should support options.fallbackLng as a language source', async () => {
318+
i18nMock.options = {
319+
backend: {},
320+
ns: ['nested/name-space'],
321+
fallbackNS: ['nested/fallback-name-space'],
322+
fallbackLng: 'en-US',
323+
};
324+
i18nMock.language = 'en-US';
325+
i18nMock.languages = [];
326+
327+
applyClientHMR(i18nMock);
328+
329+
await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);
330+
331+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
332+
['en-US'],
333+
['nested/fallback-name-space'],
334+
expect.any(Function)
335+
);
336+
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
337+
});
338+
273339
describe('multiple files', () => {
274340
it('should support change of multiple files', async () => {
275341
i18nMock.options = { backend: {}, ns: ['name-space', 'name-space2'] };

__tests__/server-hmr.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,49 @@ describe('server-hmr', () => {
182182
expect.any(Function)
183183
);
184184
});
185+
186+
it('should support options.supportedLngs as a language source', async () => {
187+
i18nMock.language = 'en-US';
188+
i18nMock.options = {
189+
backend: {},
190+
ns: ['nested/name-space'],
191+
supportedLngs: [i18nMock.language],
192+
};
193+
194+
await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);
195+
196+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
197+
['en-US'],
198+
['nested/name-space'],
199+
expect.any(Function)
200+
);
201+
});
202+
203+
it('should support options.lng as a language source', async () => {
204+
i18nMock.language = 'en-US';
205+
i18nMock.options = { backend: {}, ns: ['nested/name-space'], lng: i18nMock.language };
206+
207+
await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);
208+
209+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
210+
['en-US'],
211+
['nested/name-space'],
212+
expect.any(Function)
213+
);
214+
});
215+
216+
it('should support options.fallbackLng as a language source', async () => {
217+
i18nMock.language = 'en-US';
218+
i18nMock.options = { backend: {}, ns: ['nested/name-space'], fallbackLng: i18nMock.language };
219+
220+
await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);
221+
222+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
223+
['en-US'],
224+
['nested/name-space'],
225+
expect.any(Function)
226+
);
227+
});
185228
});
186229

187230
describe('without native HMR', () => {
@@ -306,6 +349,49 @@ describe('server-hmr', () => {
306349
expect.any(Function)
307350
);
308351
});
352+
353+
it('should support options.supportedLngs as a language source', async () => {
354+
i18nMock.language = 'en-US';
355+
i18nMock.options = {
356+
backend: {},
357+
ns: ['nested/name-space'],
358+
supportedLngs: [i18nMock.language],
359+
};
360+
361+
plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });
362+
363+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
364+
['en-US'],
365+
['nested/name-space'],
366+
expect.any(Function)
367+
);
368+
});
369+
370+
it('should support options.lng as a language source', async () => {
371+
i18nMock.language = 'en-US';
372+
i18nMock.options = { backend: {}, ns: ['nested/name-space'], lng: i18nMock.language };
373+
374+
plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });
375+
376+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
377+
['en-US'],
378+
['nested/name-space'],
379+
expect.any(Function)
380+
);
381+
});
382+
383+
it('should support options.fallbackLng as a language source', async () => {
384+
i18nMock.language = 'en-US';
385+
i18nMock.options = { backend: {}, ns: ['nested/name-space'], fallbackLng: i18nMock.language };
386+
387+
plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });
388+
389+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
390+
['en-US'],
391+
['nested/name-space'],
392+
expect.any(Function)
393+
);
394+
});
309395
});
310396

311397
describe('i18n as a getter', () => {

lib/client-hmr.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
1+
const { extractList, printList, uniqueList, createLoggerOnce } = require('./utils');
22

33
module.exports = function applyClientHMR(i18nOrGetter) {
44
if (module.hot) {
@@ -27,8 +27,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {
2727

2828
backendOptions.queryStringParams._ = new Date().getTime(); // cache killer
2929

30-
const langs = makeUniqueList(list.map((item) => item.lang));
31-
const namespaces = makeUniqueList(list.map((item) => item.ns));
30+
const langs = uniqueList(list.map((item) => item.lang));
31+
const namespaces = uniqueList(list.map((item) => item.ns));
3232

3333
await i18nInstance.reloadResources(langs, namespaces, (error) => {
3434
if (error) {
@@ -50,17 +50,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {
5050
module.hot.accept('./trigger.js', () => {
5151
const { changedFiles } = require('./trigger.js');
5252
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
53-
const availableNSs = makeUniqueList(
54-
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
55-
);
56-
const availableLanguages = makeUniqueList(
57-
[].concat(i18nInstance.languages, i18nInstance.options.lng)
58-
);
5953

60-
const list = extractList(changedFiles, {
61-
namespaces: availableNSs,
62-
languages: availableLanguages,
63-
});
54+
const list = extractList(changedFiles, i18nInstance);
6455

6556
if (!list.length) {
6657
return;

lib/server-hmr.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
1+
const { extractList, printList, uniqueList, createLoggerOnce } = require('./utils');
22

33
module.exports = function applyServerHMR(i18nOrGetter) {
44
const pluginName = `\x1b[35m\x1b[1m${'I18NextHMR'}\x1b[0m\x1b[39m`;
@@ -10,26 +10,17 @@ module.exports = function applyServerHMR(i18nOrGetter) {
1010

1111
function reloadServerTranslation({ changedFiles }) {
1212
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
13-
const availableNSs = makeUniqueList(
14-
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
15-
);
16-
const availableLanguages = makeUniqueList(
17-
[].concat(i18nInstance.languages, i18nInstance.options.lng)
18-
);
19-
20-
const list = extractList(changedFiles, {
21-
namespaces: availableNSs,
22-
languages: availableLanguages,
23-
});
13+
14+
const list = extractList(changedFiles, i18nInstance);
2415

2516
if (list.length === 0) {
2617
return;
2718
}
2819

2920
log(`Got an update with ${printList(list)}`);
3021

31-
const langs = makeUniqueList(list.map((item) => item.lang));
32-
const namespaces = makeUniqueList(list.map((item) => item.ns));
22+
const langs = uniqueList(list.map((item) => item.lang));
23+
const namespaces = uniqueList(list.map((item) => item.ns));
3324

3425
i18nInstance.reloadResources(langs, namespaces, (error) => {
3526
if (error) {

lib/utils.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,25 @@ function printList(list) {
2929
return list.map((item) => `${item.lang}/${item.ns}`).join(', ');
3030
}
3131

32-
function extractList(changedFiles, currentConfig) {
32+
function extractList(changedFiles, i18nInstance) {
33+
const namespaces = uniqueList(
34+
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
35+
);
36+
const languages = uniqueList(
37+
[].concat(
38+
i18nInstance.languages,
39+
i18nInstance.options.supportedLngs,
40+
i18nInstance.options.lng,
41+
i18nInstance.options.fallbackLng
42+
)
43+
);
44+
3345
return changedFiles
34-
.map((changedFile) => extractLangAndNS(changedFile, currentConfig))
46+
.map((changedFile) => extractLangAndNS(changedFile, { namespaces, languages }))
3547
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
3648
}
3749

38-
function makeUniqueList(list) {
50+
function uniqueList(list) {
3951
return [...new Set(list)];
4052
}
4153

@@ -55,6 +67,6 @@ function createLoggerOnce(logger) {
5567
module.exports = {
5668
printList: printList,
5769
extractList: extractList,
58-
makeUniqueList: makeUniqueList,
70+
uniqueList: uniqueList,
5971
createLoggerOnce: createLoggerOnce,
6072
};

0 commit comments

Comments
 (0)