Skip to content

Commit

Permalink
feat: add support for options.lng, options.fallbackLng, options.suppo…
Browse files Browse the repository at this point in the history
…rtedLngs as a language source
  • Loading branch information
felixmosh committed Aug 11, 2022
1 parent ea6a877 commit fae8a57
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 31 deletions.
66 changes: 66 additions & 0 deletions __tests__/client-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,72 @@ describe('client-hmr', () => {
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
});

it('should support options.supportedLngs as a language source', async () => {
i18nMock.options = {
backend: {},
ns: ['nested/name-space'],
fallbackNS: ['nested/fallback-name-space'],
supportedLngs: ['en-US'],
};
i18nMock.language = 'en-US';
i18nMock.languages = [];

applyClientHMR(i18nMock);

await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/fallback-name-space'],
expect.any(Function)
);
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
});

it('should support options.lng as a language source', async () => {
i18nMock.options = {
backend: {},
ns: ['nested/name-space'],
fallbackNS: ['nested/fallback-name-space'],
lng: 'en-US',
};
i18nMock.language = 'en-US';
i18nMock.languages = [];

applyClientHMR(i18nMock);

await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/fallback-name-space'],
expect.any(Function)
);
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
});

it('should support options.fallbackLng as a language source', async () => {
i18nMock.options = {
backend: {},
ns: ['nested/name-space'],
fallbackNS: ['nested/fallback-name-space'],
fallbackLng: 'en-US',
};
i18nMock.language = 'en-US';
i18nMock.languages = [];

applyClientHMR(i18nMock);

await whenHotTriggeredWith(['nested/fallback-name-space/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/fallback-name-space'],
expect.any(Function)
);
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en-US');
});

describe('multiple files', () => {
it('should support change of multiple files', async () => {
i18nMock.options = { backend: {}, ns: ['name-space', 'name-space2'] };
Expand Down
86 changes: 86 additions & 0 deletions __tests__/server-hmr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ describe('server-hmr', () => {
expect.any(Function)
);
});

it('should support options.supportedLngs as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = {
backend: {},
ns: ['nested/name-space'],
supportedLngs: [i18nMock.language],
};

await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});

it('should support options.lng as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = { backend: {}, ns: ['nested/name-space'], lng: i18nMock.language };

await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});

it('should support options.fallbackLng as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = { backend: {}, ns: ['nested/name-space'], fallbackLng: i18nMock.language };

await whenNativeHMRTriggeredWith(['nested/name-space/locales/en-US']);

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});
});

describe('without native HMR', () => {
Expand Down Expand Up @@ -306,6 +349,49 @@ describe('server-hmr', () => {
expect.any(Function)
);
});

it('should support options.supportedLngs as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = {
backend: {},
ns: ['nested/name-space'],
supportedLngs: [i18nMock.language],
};

plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});

it('should support options.lng as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = { backend: {}, ns: ['nested/name-space'], lng: i18nMock.language };

plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});

it('should support options.fallbackLng as a language source', async () => {
i18nMock.language = 'en-US';
i18nMock.options = { backend: {}, ns: ['nested/name-space'], fallbackLng: i18nMock.language };

plugin.callbacks[0]({ changedFiles: ['nested/name-space/locales/en-US'] });

expect(i18nMock.reloadResources).toHaveBeenCalledWith(
['en-US'],
['nested/name-space'],
expect.any(Function)
);
});
});

describe('i18n as a getter', () => {
Expand Down
17 changes: 4 additions & 13 deletions lib/client-hmr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
const { extractList, printList, uniqueList, createLoggerOnce } = require('./utils');

module.exports = function applyClientHMR(i18nOrGetter) {
if (module.hot) {
Expand Down Expand Up @@ -27,8 +27,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {

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

const langs = makeUniqueList(list.map((item) => item.lang));
const namespaces = makeUniqueList(list.map((item) => item.ns));
const langs = uniqueList(list.map((item) => item.lang));
const namespaces = uniqueList(list.map((item) => item.ns));

await i18nInstance.reloadResources(langs, namespaces, (error) => {
if (error) {
Expand All @@ -50,17 +50,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {
module.hot.accept('./trigger.js', () => {
const { changedFiles } = require('./trigger.js');
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const availableNSs = makeUniqueList(
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
);
const availableLanguages = makeUniqueList(
[].concat(i18nInstance.languages, i18nInstance.options.lng)
);

const list = extractList(changedFiles, {
namespaces: availableNSs,
languages: availableLanguages,
});
const list = extractList(changedFiles, i18nInstance);

if (!list.length) {
return;
Expand Down
19 changes: 5 additions & 14 deletions lib/server-hmr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
const { extractList, printList, uniqueList, createLoggerOnce } = require('./utils');

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

function reloadServerTranslation({ changedFiles }) {
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const availableNSs = makeUniqueList(
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
);
const availableLanguages = makeUniqueList(
[].concat(i18nInstance.languages, i18nInstance.options.lng)
);

const list = extractList(changedFiles, {
namespaces: availableNSs,
languages: availableLanguages,
});

const list = extractList(changedFiles, i18nInstance);

if (list.length === 0) {
return;
}

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

const langs = makeUniqueList(list.map((item) => item.lang));
const namespaces = makeUniqueList(list.map((item) => item.ns));
const langs = uniqueList(list.map((item) => item.lang));
const namespaces = uniqueList(list.map((item) => item.ns));

i18nInstance.reloadResources(langs, namespaces, (error) => {
if (error) {
Expand Down
20 changes: 16 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ function printList(list) {
return list.map((item) => `${item.lang}/${item.ns}`).join(', ');
}

function extractList(changedFiles, currentConfig) {
function extractList(changedFiles, i18nInstance) {
const namespaces = uniqueList(
[].concat(i18nInstance.options.ns, i18nInstance.options.fallbackNS || []).filter(Boolean)
);
const languages = uniqueList(
[].concat(
i18nInstance.languages,
i18nInstance.options.supportedLngs,
i18nInstance.options.lng,
i18nInstance.options.fallbackLng
)
);

return changedFiles
.map((changedFile) => extractLangAndNS(changedFile, currentConfig))
.map((changedFile) => extractLangAndNS(changedFile, { namespaces, languages }))
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
}

function makeUniqueList(list) {
function uniqueList(list) {
return [...new Set(list)];
}

Expand All @@ -55,6 +67,6 @@ function createLoggerOnce(logger) {
module.exports = {
printList: printList,
extractList: extractList,
makeUniqueList: makeUniqueList,
uniqueList: uniqueList,
createLoggerOnce: createLoggerOnce,
};

0 comments on commit fae8a57

Please sign in to comment.