Fix fallback languages not loading (#631) (#632)

This commit is contained in:
Nikita 2024-12-15 13:21:18 +03:00 committed by GitHub
parent 7392447a35
commit af2dad1d2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,20 +14,24 @@ export const i18nModule = createI18n({
export const i18n = i18nModule.global; export const i18n = i18nModule.global;
export async function setLanguage(lang) { export async function setLanguage(lang) {
loadLanguage(lang);
i18n.locale.value = lang;
document.querySelector('html').setAttribute('lang', lang);
return nextTick();
}
async function loadLanguage(lang) {
try { try {
if (!i18n.availableLocales.includes(lang)){ if (!i18n.availableLocales.includes(lang)) {
let messages = await fetchHocon(`./lang/${lang}.conf`); let messages = await fetchHocon(`./lang/${lang}.conf`);
i18n.setLocaleMessage(lang, messages); i18n.setLocaleMessage(lang, messages);
} }
i18n.locale.value = lang;
document.querySelector('html').setAttribute('lang', lang);
} catch (e) { } catch (e) {
console.error(`Failed to load language '${lang}'!`, e); console.error(`Failed to load language '${lang}'!`, e);
} }
return nextTick();
} }
export async function loadLanguageSettings() { export async function loadLanguageSettings() {
@ -37,20 +41,24 @@ export async function loadLanguageSettings() {
if (settings.useBrowserLanguage) { if (settings.useBrowserLanguage) {
const availableLanguages = settings.languages.map(lang => lang.locale); const availableLanguages = settings.languages.map(lang => lang.locale);
for (let browserLanguage of navigator.languages) { for (let browserLanguage of navigator.languages) {
selectedLanguage = availableLanguages.find(lang => lang === browserLanguage); selectedLanguage = availableLanguages.find(lang => lang === browserLanguage);
if (selectedLanguage) break; if (selectedLanguage) break;
let baseBrowserLanguage = browserLanguage.split('-')[0]; let baseBrowserLanguage = browserLanguage.split('-')[0];
selectedLanguage = availableLanguages.find(lang => lang.startsWith(baseBrowserLanguage)); selectedLanguage = availableLanguages.find(lang => lang.startsWith(baseBrowserLanguage));
if (selectedLanguage) break; if (selectedLanguage) break;
} }
} }
if (!selectedLanguage) { if (!selectedLanguage) {
selectedLanguage = settings.default; selectedLanguage = settings.default;
} }
await loadLanguage('en');
await loadLanguage(settings.default);
i18nModule.global.fallbackLocale = [settings.default, 'en'];
i18n.languages = settings.languages; i18n.languages = settings.languages;
await setLanguage(selectedLanguage); await setLanguage(selectedLanguage);
} }