Add lit components localization
Localization is performed via the @lit/localize package, which has been
integrated into the repo. A I18nLitElement class which extends
LitElement has been included, which automatically sets up the
localization.
This CL also introduces localization to the flattenThreads feature.
Fixed: twpowertools:157
Change-Id: If01540b9c8d70876a648aa002a1e0a5bede8f383
diff --git a/src/common/litI18nUtils.js b/src/common/litI18nUtils.js
new file mode 100644
index 0000000..1f95baa
--- /dev/null
+++ b/src/common/litI18nUtils.js
@@ -0,0 +1,43 @@
+import {configureLocalization, updateWhenLocaleChanges} from '@lit/localize';
+import {LitElement} from 'lit';
+
+import MWI18nClient from '../common/mainWorldI18n/Client.js';
+import {allLocales, sourceLocale, targetLocales} from '../lit-locales/generated/locales.js';
+
+export class I18nLitElement extends LitElement {
+ #MWI18nClient;
+
+ constructor() {
+ super();
+ updateWhenLocaleChanges(this);
+ this.#MWI18nClient = new MWI18nClient();
+ this.#setUpLitL10n();
+ }
+
+ #setUpLitL10n() {
+ let pLocale;
+ if (chrome.i18n) {
+ pLocale = Promise.resolve(chrome.i18n.getUILanguage());
+ } else {
+ pLocale = this.#MWI18nClient.getUILanguage();
+ }
+ pLocale.then(browserLocale => {
+ let locale = browserLocale.substr(0, 2).toLowerCase();
+ if (locale == 'pt') locale = 'pt_BR';
+ const sanitizedLocale = allLocales.includes(locale) ? locale : 'en';
+ setLocale(sanitizedLocale);
+ });
+ }
+}
+
+export const {getLocale, setLocale} = configureLocalization({
+ sourceLocale,
+ targetLocales,
+ loadLocale: locale => {
+ if (!allLocales.includes(locale)) locale = 'en';
+ return import(
+ /* webpackMode: "eager" */
+ /* webpackExclude: /locales\.json$/ */
+ `../lit-locales/generated/${locale}.js`);
+ },
+});