Adrià Vilanova MartÃnez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 1 | import {configureLocalization, updateWhenLocaleChanges} from '@lit/localize'; |
| 2 | import {LitElement} from 'lit'; |
| 3 | |
| 4 | import MWI18nClient from '../common/mainWorldI18n/Client.js'; |
| 5 | import {allLocales, sourceLocale, targetLocales} from '../lit-locales/generated/locales.js'; |
| 6 | |
| 7 | export 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 | |
| 33 | export 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 | }); |