Skip to content

Commit

Permalink
Fix fallback languages not loading (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Dec 14, 2024
1 parent 7392447 commit ce7e477
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions common/webapp/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}

0 comments on commit ce7e477

Please sign in to comment.