Skip to content

Commit

Permalink
Load fallback (two-letter) locale
Browse files Browse the repository at this point in the history
When the full fledge locale (e.g. en_US) isn't available, fall back to
two letter locale name (i.e. 'en'). Also: catch the error(s) when either
or both aren't available.
  • Loading branch information
ehuelsmann committed Jul 25, 2023
1 parent 3fddc30 commit 3cd40f2
Showing 1 changed file with 18 additions and 5 deletions.
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

0 comments on commit 3cd40f2

Please sign in to comment.