Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating package-lock.json #206

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading