From 3b16ccb96d314b6248bd2f73e4f1999580828423 Mon Sep 17 00:00:00 2001 From: Dave Lockhart Date: Fri, 8 Nov 2024 16:36:57 -0500 Subject: [PATCH] only load common resources if specified in config --- lib/localize.js | 20 ++++++++++++++------ test/localize.test.js | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/localize.js b/lib/localize.js index 2a35262..7ffeb87 100644 --- a/lib/localize.js +++ b/lib/localize.js @@ -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 = {}) { @@ -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); } diff --git a/test/localize.test.js b/test/localize.test.js index feaf939..f588c2d 100644 --- a/test/localize.test.js +++ b/test/localize.test.js @@ -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'); + }); + }); });