From ce7e477ced7a41cd3da6f4a205462bf36701ef5c Mon Sep 17 00:00:00 2001 From: NikitaCartes Date: Sat, 14 Dec 2024 20:26:06 +0100 Subject: [PATCH] Fix fallback languages not loading (#631) --- common/webapp/src/i18n.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/common/webapp/src/i18n.js b/common/webapp/src/i18n.js index 759a114f..25b83962 100644 --- a/common/webapp/src/i18n.js +++ b/common/webapp/src/i18n.js @@ -14,20 +14,24 @@ export const i18nModule = createI18n({ export const i18n = i18nModule.global; export async function setLanguage(lang) { + loadLanguage(lang); + + i18n.locale.value = lang; + + document.querySelector('html').setAttribute('lang', lang); + + return nextTick(); +} + +async function loadLanguage(lang) { try { - if (!i18n.availableLocales.includes(lang)){ + if (!i18n.availableLocales.includes(lang)) { let messages = await fetchHocon(`./lang/${lang}.conf`); i18n.setLocaleMessage(lang, messages); } - - i18n.locale.value = lang; - - document.querySelector('html').setAttribute('lang', lang); } catch (e) { console.error(`Failed to load language '${lang}'!`, e); } - - return nextTick(); } export async function loadLanguageSettings() { @@ -37,20 +41,24 @@ export async function loadLanguageSettings() { if (settings.useBrowserLanguage) { const availableLanguages = settings.languages.map(lang => lang.locale); - for (let browserLanguage of navigator.languages) { - selectedLanguage = availableLanguages.find(lang => lang === browserLanguage); - if (selectedLanguage) break; + for (let browserLanguage of navigator.languages) { + selectedLanguage = availableLanguages.find(lang => lang === browserLanguage); + if (selectedLanguage) break; - let baseBrowserLanguage = browserLanguage.split('-')[0]; - selectedLanguage = availableLanguages.find(lang => lang.startsWith(baseBrowserLanguage)); - if (selectedLanguage) break; - } + let baseBrowserLanguage = browserLanguage.split('-')[0]; + selectedLanguage = availableLanguages.find(lang => lang.startsWith(baseBrowserLanguage)); + if (selectedLanguage) break; + } } if (!selectedLanguage) { selectedLanguage = settings.default; } + await loadLanguage('en'); + await loadLanguage(settings.default); + i18nModule.global.fallbackLocale = [settings.default, 'en']; + i18n.languages = settings.languages; await setLanguage(selectedLanguage); }