blob: 1f95baaeaa8857864c1c1fad52512b38a482e9f3 [file] [log] [blame]
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +01001import {configureLocalization, updateWhenLocaleChanges} from '@lit/localize';
2import {LitElement} from 'lit';
3
4import MWI18nClient from '../common/mainWorldI18n/Client.js';
5import {allLocales, sourceLocale, targetLocales} from '../lit-locales/generated/locales.js';
6
7export class I18nLitElement extends LitElement {
8 #MWI18nClient;
9
10 constructor() {
11 super();
12 updateWhenLocaleChanges(this);
13 this.#MWI18nClient = new MWI18nClient();
14 this.#setUpLitL10n();
15 }
16
17 #setUpLitL10n() {
18 let pLocale;
19 if (chrome.i18n) {
20 pLocale = Promise.resolve(chrome.i18n.getUILanguage());
21 } else {
22 pLocale = this.#MWI18nClient.getUILanguage();
23 }
24 pLocale.then(browserLocale => {
25 let locale = browserLocale.substr(0, 2).toLowerCase();
26 if (locale == 'pt') locale = 'pt_BR';
27 const sanitizedLocale = allLocales.includes(locale) ? locale : 'en';
28 setLocale(sanitizedLocale);
29 });
30 }
31}
32
33export const {getLocale, setLocale} = configureLocalization({
34 sourceLocale,
35 targetLocales,
36 loadLocale: locale => {
37 if (!allLocales.includes(locale)) locale = 'en';
38 return import(
39 /* webpackMode: "eager" */
40 /* webpackExclude: /locales\.json$/ */
41 `../lit-locales/generated/${locale}.js`);
42 },
43});