refactor: get rid of getlang function and set locale during initialization

fix: show english flag when locale does not match
This commit is contained in:
Ryazanov Alexander Mihailovich 2024-05-23 13:30:16 +03:00 committed by Alexander Ryazanov
parent b068ca162a
commit f0c6d83b8e
2 changed files with 9 additions and 10 deletions

View File

@ -42,6 +42,9 @@ const switchLanguage = function (lang) {
const languageFlag = computed(() => { const languageFlag = computed(() => {
// `this` points to the component instance // `this` points to the component instance
let lang = appGlobal.$i18n.locale.toLowerCase(); let lang = appGlobal.$i18n.locale.toLowerCase();
if (!appGlobal.$i18n.availableLocales.includes(lang)) {
lang = appGlobal.$i18n.fallbackLocale;
}
if (lang === "en") { if (lang === "en") {
lang = "us"; lang = "us";
} }

View File

@ -4,20 +4,16 @@ import ru from './translations/ru.json';
import en from './translations/en.json'; import en from './translations/en.json';
import {createI18n} from "vue-i18n"; import {createI18n} from "vue-i18n";
function getStoredLanguage() {
let initialLang = localStorage.getItem('wgLang');
if (!initialLang) {
initialLang = "en"
}
return initialLang
}
// Create i18n instance with options // Create i18n instance with options
const i18n = createI18n({ const i18n = createI18n({
legacy: false, legacy: false,
globalInjection: true, globalInjection: true,
allowComposition: true, allowComposition: true,
locale: getStoredLanguage(), // set locale locale: (
localStorage.getItem('wgLang')
|| (window && window.navigator && (window.navigator.userLanguage || window.navigator.language).split('-')[0])
|| 'en'
), // set locale
fallbackLocale: "en", // set fallback locale fallbackLocale: "en", // set fallback locale
messages: { messages: {
"de": de, "de": de,
@ -26,4 +22,4 @@ const i18n = createI18n({
} }
}); });
export default i18n export default i18n