Skip to content

Commit 3a20106

Browse files
committed
fix: add defaultNS as an optional ns value
1 parent ee584c4 commit 3a20106

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

__tests__/client-hmr.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,26 @@ describe('client-hmr', () => {
266266
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
267267
});
268268

269+
it('should support defaultNS as optional ns', async () => {
270+
i18nMock.options = {
271+
backend: {},
272+
ns: [],
273+
defaultNS: ['common'],
274+
};
275+
i18nMock.language = 'en-US';
276+
277+
applyClientHMR(i18nMock);
278+
279+
await whenHotTriggeredWith(['common/en-US']);
280+
281+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
282+
['en-US'],
283+
['common'],
284+
expect.any(Function)
285+
);
286+
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
287+
});
288+
269289
it('should support complex localePath {{ns}}/locales/{{lng}}.json', async () => {
270290
i18nMock.options = { backend: {}, ns: ['nested/name-space'] };
271291
i18nMock.language = 'en-US';

__tests__/server-hmr.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ describe('server-hmr', () => {
139139
);
140140
});
141141

142+
it('should support defaultNS as source of ns', async () => {
143+
const update = { lang: 'en', ns: 'nested/fallback-name-space' };
144+
i18nMock.options.ns = [];
145+
i18nMock.options.defaultNS = update.ns;
146+
whenNativeHMRTriggeredWith([`${update.lang}/${update.ns}`]);
147+
148+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
149+
[update.lang],
150+
[update.ns],
151+
expect.any(Function)
152+
);
153+
});
154+
142155
it('should notify on successful change', async () => {
143156
jest.spyOn(global.console, 'log');
144157

@@ -293,6 +306,19 @@ describe('server-hmr', () => {
293306
);
294307
});
295308

309+
it('should support defaultNS as source of ns', async () => {
310+
const update = { lang: 'en', ns: 'nested/fallback-name-space' };
311+
i18nMock.options.ns = [];
312+
i18nMock.options.defaultNS = update.ns;
313+
plugin.callbacks[0]({ changedFiles: [`${update.lang}/${update.ns}`] });
314+
315+
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
316+
[update.lang],
317+
[update.ns],
318+
expect.any(Function)
319+
);
320+
});
321+
296322
it('should ignore changes of none loaded namespace', async () => {
297323
jest.spyOn(global.console, 'log');
298324
i18nMock.options = { backend: {}, ns: ['name-space'] };

lib/utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ function printList(list) {
3131

3232
function extractList(changedFiles, i18nInstance) {
3333
const namespaces = uniqueList(
34-
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
34+
[]
35+
.concat(
36+
i18nInstance.options.ns,
37+
i18nInstance.options.fallbackNS,
38+
i18nInstance.options.defaultNS
39+
)
40+
.filter(Boolean)
3541
);
3642
const languages = uniqueList(
3743
[].concat(

0 commit comments

Comments
 (0)