Skip to content

Commit

Permalink
only load common resources if specified in config
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Nov 8, 2024
1 parent 5c5d537 commit 3b16ccb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
20 changes: 14 additions & 6 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
if (!characterMap.has(char)) {
throw new Error(`localizeCharacter() does not support character: "${char}"`);
}
return this.localize(`intl-common:characters:${characterMap.get(char)}`);
const value = this.localize(`intl-common:characters:${characterMap.get(char)}`);
if (value.length === 0) {
throw new Error('localizeCharacter() cannot be used unless loadCommon in localizeConfig is enabled');
}
return value;
}

localizeHTML(name, replacements = {}) {
Expand Down Expand Up @@ -187,11 +191,15 @@ 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);
const resourcesPromise = this.getLocalizeResources(possibleLanguages, config);
resourcesLoadedPromises.push(resourcesPromise);
resourcesLoadedPromises.push(this.getLocalizeResources(possibleLanguages, {
importFunc: async lang => (await import(`../lang/${lang}.js`)).default
}));
if (config?.importFunc) {
const resourcesPromise = this.getLocalizeResources(possibleLanguages, config);
resourcesLoadedPromises.push(resourcesPromise);
}
if (config?.loadCommon) {
resourcesLoadedPromises.push(this.getLocalizeResources(possibleLanguages, {
importFunc: async lang => (await import(`../lang/${lang}.js`)).default
}));
}
}
return Promise.all(resourcesLoadedPromises);
}
Expand Down
38 changes: 28 additions & 10 deletions test/localize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,36 @@ describe('Localize', () => {

});

describe('localizeCharacter', () => {

it('should localize "&"', async() => {
await localizer.ready;
const localized = localizer.localizeCharacter('&');
expect(localized).to.equal('ampersand');
describe('common', () => {

let localizerCommon;
beforeEach(async() => {
localizerCommon = new Localize({
loadCommon: true
});
await localizerCommon.ready;
});

it('should throw an error for unknown characters', async() => {
await localizer.ready;
expect(() => localizer.localizeCharacter('$'))
.to.throw('localizeCharacter() does not support character: "$"');
afterEach(() => localizerCommon.disconnect());

describe('localizeCharacter', () => {

it('should localize "&"', async() => {
const localized = localizerCommon.localizeCharacter('&');
expect(localized).to.equal('ampersand');
});

it('should throw an error for unknown characters', async() => {
expect(() => localizerCommon.localizeCharacter('$'))
.to.throw('localizeCharacter() does not support character: "$"');
});

it('should throw an error if common resources are not loaded', async() => {
await localizer.ready;
expect(() => localizerCommon.localizeCharacter(':'))
.to.throw('localizeCharacter() cannot be used unless loadCommon in localizeConfig is enabled');
});

});

});
Expand Down

0 comments on commit 3b16ccb

Please sign in to comment.