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

Load fallback (two-letter) locale #7541

Merged
merged 2 commits into from Jul 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions UI/src/i18n.js
Expand Up @@ -15,7 +15,7 @@ function _mapLocale(locale) {
}

// eslint-disable-next-line import/no-unresolved
import messages from '@/locales/en.json'
import messages from '@/locales/en.json';
import { nextTick } from "vue";

const i18n = createI18n({
Expand All @@ -30,12 +30,25 @@ const i18n = createI18n({
});

export async function setI18nLanguage(lang) {
const locale = _mapLocale(lang.value);
let locale = _mapLocale(lang.value);

// If the language hasn't been loaded yet
if (!i18n.global.availableLocales.includes(locale)) {
const _messages = await import(/* webpackChunkName: "lang-[request]" */ `@/locales/${locale}.json`);
i18n.global.setLocaleMessage(locale, _messages.default);
if (!i18n.global.availableLocales.includes(locale)) {
try {
const _messages = await import(/* webpackChunkName: "lang-[request]" */ `@/locales/${locale}.json`);
i18n.global.setLocaleMessage(locale, _messages.default);
}
catch (e) {
const strippedLocale = locale.replace(/_[a-z]+/i, '');
try {
const _messages = await import(/* webpackChunkName: "lang-[request]" */ `@/locales/${strippedLocale}.json`);
i18n.global.setLocaleMessage(strippedLocale, _messages.default);
locale = strippedLocale;
}
catch (f) {
locale = "en";
}
}
}
if ( !i18n.global.locale.value === locale ){
document.querySelector("html").setAttribute("lang", locale);
Expand Down