Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Nov 14, 2024
1 parent 04af341 commit 99da305
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
7 changes: 4 additions & 3 deletions lib/PluralRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class PluralRules extends Intl.PluralRules {
return super.supportedLocalesOf(l);
}).flat();
}
#localeData;
#locale;
#type;

constructor(locales, options = {}) {
super(locales, options);
Expand All @@ -68,6 +65,10 @@ class PluralRules extends Intl.PluralRules {
}
}

#localeData;
#locale;
#type;

}

Object.defineProperty(Intl, 'PluralRules', {
Expand Down
90 changes: 46 additions & 44 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,14 @@ const noAllowedTagsRegex = getDisallowedTagsRegex([]);

export const getLocalizeClass = (superclass = class {}) => class LocalizeClass extends superclass {

static #localizeMarkup;
static documentLocaleSettings = getDocumentLocaleSettings();

static setLocalizeMarkup(localizeMarkup) {
this.#localizeMarkup ??= localizeMarkup;
}

#connected = false;
#localeChangeCallback;
#resolveResourcesLoaded;
#resourcesPromise;
pristine = true;

async #localeChangeHandler() {
if (!this._hasResources()) return;

const resourcesPromise = this.constructor._getAllLocalizeResources(this.config);
this.#resourcesPromise = resourcesPromise;
const localizeResources = (await resourcesPromise).flat(Infinity);
// If the locale changed while resources were being fetched, abort
if (this.#resourcesPromise !== resourcesPromise) return;

const allResources = {};
const resolvedLocales = new Set();
for (const { language, resources } of localizeResources) {
for (const [name, value] of Object.entries(resources)) {
allResources[name] = { language, value };
resolvedLocales.add(language);
}
}
this.localize.resources = allResources;
this.localize.resolvedLocale = [...resolvedLocales][0];
if (resolvedLocales.size > 1) {
console.warn(`Resolved multiple locales in '${this.constructor.name || this.tagName || ''}': ${[...resolvedLocales].join(', ')}`);
}

if (this.pristine) {
this.pristine = false;
this.#resolveResourcesLoaded();
}

this.#onResourcesChange();
}

#onResourcesChange() {
if (this.#connected) {
this.dispatchEvent?.(new CustomEvent('d2l-localize-resources-change'));
this.config?.onResourcesChange?.();
this.onLocalizeResourcesChange?.();
}
}

connect() {
this.#localeChangeCallback = () => this.#localeChangeHandler();
LocalizeClass.documentLocaleSettings.addChangeListener(this.#localeChangeCallback);
Expand Down Expand Up @@ -139,6 +95,13 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
return formattedMessage;
}

static #localizeMarkup;

#connected = false;
#localeChangeCallback;
#resolveResourcesLoaded;
#resourcesPromise;

__resourcesLoadedPromise = new Promise(r => this.#resolveResourcesLoaded = r);

static _generatePossibleLanguages(config) {
Expand Down Expand Up @@ -203,6 +166,45 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
return this.constructor.localizeConfig ? Boolean(this.constructor.localizeConfig.importFunc) : this.constructor.getLocalizeResources !== undefined;
}

async #localeChangeHandler() {
if (!this._hasResources()) return;

const resourcesPromise = this.constructor._getAllLocalizeResources(this.config);
this.#resourcesPromise = resourcesPromise;
const localizeResources = (await resourcesPromise).flat(Infinity);
// If the locale changed while resources were being fetched, abort
if (this.#resourcesPromise !== resourcesPromise) return;

const allResources = {};
const resolvedLocales = new Set();
for (const { language, resources } of localizeResources) {
for (const [name, value] of Object.entries(resources)) {
allResources[name] = { language, value };
resolvedLocales.add(language);
}
}
this.localize.resources = allResources;
this.localize.resolvedLocale = [...resolvedLocales][0];
if (resolvedLocales.size > 1) {
console.warn(`Resolved multiple locales in '${this.constructor.name || this.tagName || ''}': ${[...resolvedLocales].join(', ')}`);
}

if (this.pristine) {
this.pristine = false;
this.#resolveResourcesLoaded();
}

this.#onResourcesChange();
}

#onResourcesChange() {
if (this.#connected) {
this.dispatchEvent?.(new CustomEvent('d2l-localize-resources-change'));
this.config?.onResourcesChange?.();
this.onLocalizeResourcesChange?.();
}
}

};

export const Localize = class extends getLocalizeClass() {
Expand Down

0 comments on commit 99da305

Please sign in to comment.