Skip to content

Commit 2e76ca0

Browse files
LarsDenBakkerMikhail Bashkirov
authored andcommitted
feat(localize): allow custom locale when loading namespaces
1 parent e381d9f commit 2e76ca0

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

packages/localize/src/LocalizeManager.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,25 @@ export class LocalizeManager extends LionSingleton {
5959
this.__namespacePatternsMap.set(pattern, loader);
6060
}
6161

62-
loadNamespaces(namespaces) {
63-
return Promise.all(namespaces.map(namespace => this.loadNamespace(namespace)));
62+
loadNamespaces(namespaces, { locale } = {}) {
63+
return Promise.all(namespaces.map(namespace => this.loadNamespace(namespace, { locale })));
6464
}
6565

66-
loadNamespace(namespaceObj) {
66+
loadNamespace(namespaceObj, { locale = this.locale } = { locale: this.locale }) {
6767
const isDynamicImport = typeof namespaceObj === 'object';
6868

6969
const namespace = isDynamicImport ? Object.keys(namespaceObj)[0] : namespaceObj;
7070

71-
if (this._isNamespaceInCache(this.locale, namespace)) {
71+
if (this._isNamespaceInCache(locale, namespace)) {
7272
return Promise.resolve();
7373
}
7474

75-
const existingLoaderPromise = this._getCachedNamespaceLoaderPromise(this.locale, namespace);
75+
const existingLoaderPromise = this._getCachedNamespaceLoaderPromise(locale, namespace);
7676
if (existingLoaderPromise) {
7777
return existingLoaderPromise;
7878
}
7979

80-
return this._loadNamespaceData(this.locale, namespaceObj, isDynamicImport, namespace);
80+
return this._loadNamespaceData(locale, namespaceObj, isDynamicImport, namespace);
8181
}
8282

8383
msg(keys, vars, opts = {}) {

packages/localize/test/LocalizeManager.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ describe('LocalizeManager', () => {
116116
});
117117
});
118118

119+
it('can load a namespace for a different locale', async () => {
120+
setupFakeImport('./my-component/nl-NL.js', { default: { greeting: 'Hello!' } });
121+
122+
const manager = new LocalizeManager();
123+
manager.locale = 'en-US';
124+
125+
await manager.loadNamespace(
126+
{
127+
'my-component': locale => fakeImport(`./my-component/${locale}.js`),
128+
},
129+
{ locale: 'nl-NL' },
130+
);
131+
132+
expect(manager.__storage).to.deep.equal({
133+
'nl-NL': {
134+
'my-component': { greeting: 'Hello!' },
135+
},
136+
});
137+
});
138+
119139
it('loads multiple namespaces via loadNamespaces()', async () => {
120140
setupFakeImport('./my-defaults/en-GB.js', { default: { submit: 'Submit' } });
121141
setupFakeImport('./my-send-button/en-GB.js', { default: { submit: 'Send' } });
@@ -135,6 +155,29 @@ describe('LocalizeManager', () => {
135155
});
136156
});
137157

158+
it('can load multiple namespaces for a different locale', async () => {
159+
setupFakeImport('./my-defaults/nl-NL.js', { default: { submit: 'Submit' } });
160+
setupFakeImport('./my-send-button/nl-NL.js', { default: { submit: 'Send' } });
161+
162+
const manager = new LocalizeManager();
163+
manager.locale = 'en-US';
164+
165+
await manager.loadNamespaces(
166+
[
167+
{ 'my-defaults': locale => fakeImport(`./my-defaults/${locale}.js`) },
168+
{ 'my-send-button': locale => fakeImport(`./my-send-button/${locale}.js`) },
169+
],
170+
{ locale: 'nl-NL' },
171+
);
172+
173+
expect(manager.__storage).to.deep.equal({
174+
'nl-NL': {
175+
'my-defaults': { submit: 'Submit' },
176+
'my-send-button': { submit: 'Send' },
177+
},
178+
});
179+
});
180+
138181
it('fallbacks to language file if locale file is not found', async () => {
139182
setupFakeImport('./my-component/en.js', { default: { greeting: 'Hello!' } });
140183

0 commit comments

Comments
 (0)