@lynx-js/web-core
Version:
This is an internal experimental package, do not use
44 lines • 1.76 kB
JavaScript
/*
* Copyright 2025 The Lynx Authors. All rights reserved.
* Licensed under the Apache License Version 2.0 that can be found in the
* LICENSE file in the root directory of this source tree.
*/
import { i18nResourceMissedEventName } from '../../constants.js';
export const getCacheI18nResourcesKey = (options) => {
return `${options.locale}_${options.channel}_${options.fallback_url}`;
};
export class I18nManager {
#background;
#rootDom;
#i18nResources;
constructor(background, rootDom, i18nResources = []) {
this.#background = background;
this.#rootDom = rootDom;
this.#i18nResources = i18nResources;
}
updateData(data, options) {
this.#i18nResources = this.#i18nResources.concat(data);
const matchedInitI18nResources = data.find(i => getCacheI18nResourcesKey(i.options)
=== getCacheI18nResourcesKey(options));
this.#background.dispatchI18nResource(matchedInitI18nResources?.resource);
}
_I18nResourceTranslation = (options) => {
const matchedInitI18nResources = this.#i18nResources?.find((i) => getCacheI18nResourcesKey(i.options)
=== getCacheI18nResourcesKey(options));
this.#background.dispatchI18nResource(matchedInitI18nResources?.resource);
if (matchedInitI18nResources) {
return matchedInitI18nResources.resource;
}
this.#triggerI18nResourceFallback(options);
return undefined;
};
#triggerI18nResourceFallback(options) {
const event = new CustomEvent(i18nResourceMissedEventName, {
detail: options,
bubbles: true,
composed: true,
});
this.#rootDom.dispatchEvent(event);
}
}
//# sourceMappingURL=I18n.js.map