Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fallback languages not loading (#631) #632

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading