Skip to content

Commit

Permalink
make things work with static resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Nov 18, 2024
1 parent 5401c3e commit 2e716c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
}
if (Object.prototype.hasOwnProperty.call(this, 'getLocalizeResources') || Object.prototype.hasOwnProperty.call(this, 'resources')) {
const possibleLanguages = this._generatePossibleLanguages(config);
if (config?.importFunc) {
const resourcesPromise = this.getLocalizeResources(possibleLanguages, config);
resourcesLoadedPromises.push(resourcesPromise);
}
const resourcesPromise = this.getLocalizeResources(possibleLanguages, config);
resourcesLoadedPromises.push(resourcesPromise);
if (config?.loadCommon) {
resourcesLoadedPromises.push(this.getLocalizeResources(possibleLanguages, {
importFunc: async(lang) => {
Expand All @@ -177,6 +175,8 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e

static async _getLocalizeResources(langs, { importFunc, osloCollection, useBrowserLangs }) {

if (importFunc === undefined) return;

// in dev, don't request unsupported langpacks
if (!importFunc.toString().includes('switch') && !useBrowserLangs) {
langs = langs.filter(lang => supportedLangpacks.includes(lang));
Expand Down Expand Up @@ -219,6 +219,13 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e

const allResources = {};
const resolvedLocales = new Set();

localizeResources.forEach((entry, index) => {
if (entry === undefined) {
localizeResources.splice(index, 1);
}
});

for (const { language, resources } of localizeResources) {
for (const [name, value] of Object.entries(resources)) {
allResources[name] = { language, value };
Expand Down
22 changes: 22 additions & 0 deletions test/localize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ describe('Localize', () => {
expect(localizer.localize.resources).to.deep.equal(localizer2.localize.resources);
});

it('should handle static resources with no importFunc', async() => {
class StaticLocalizer extends Localize {
static async getLocalizeResources(langs) {
for (let i = 0; i < langs.length; i++) {
if (resources[langs[i]]) {
return {
language: langs[i],
resources: resources[langs[i]]
};
}
}
}
constructor() {
super({});
}
}
const staticLocalizer = new StaticLocalizer();
await staticLocalizer.ready;
const localized = staticLocalizer.localize('basic', { employerName: 'D2L' });
expect(localized).to.equal('D2L is my employer');
});

describe('onResourcesChange', () => {

it('runs when the document locale changes', async() => {
Expand Down

0 comments on commit 2e716c4

Please sign in to comment.