From 6fdc329b154fd693e9e0cb239ef911dce181e8eb Mon Sep 17 00:00:00 2001 From: Sergii Leonenko Date: Thu, 22 Jul 2021 12:48:37 +0300 Subject: [PATCH] TT#132150 Fix language selection if default browser language is not en-US Steps to reproduce: * open broser settings and set any language which is not supported by CSC yet. You can chech that it was applied by executing "navigator.language" in the browser console. * clean session storage for the CSC * refresh the page. ** result before the fix: language selector are not displayed ** Expected result: language selector is present and language set to Englist (because it is defined as default language in CSC) Change-Id: I2d53e649e14e3d82e2a5c1be4f99168971cd258c --- src/boot/i18n.js | 8 +++++--- src/i18n/index.js | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/boot/i18n.js b/src/boot/i18n.js index cfd98a46..5a085bf7 100644 --- a/src/boot/i18n.js +++ b/src/boot/i18n.js @@ -2,7 +2,9 @@ import Vue from 'vue' import VueI18n from 'vue-i18n' import { - messages + messages, + getLangFromBrowserDefaults, + normalizeLocaleCode } from 'src/i18n' import { hasSession, @@ -25,9 +27,9 @@ export default ({ app, store }) => { app.i18n = i18n store.$i18n = i18n if (!hasSession('locale')) { - setSession('locale', navigator.language) + setSession('locale', getLangFromBrowserDefaults()) } - i18n.locale = getSession('locale') + '' + i18n.locale = normalizeLocaleCode(getSession('locale')) store.watch(() => i18n.locale, () => { store.dispatch('reloadLanguageRelatedData') diff --git a/src/i18n/index.js b/src/i18n/index.js index 767d09fd..70739f10 100644 --- a/src/i18n/index.js +++ b/src/i18n/index.js @@ -63,9 +63,10 @@ export function setLanguage (lang) { setSession('locale', lang) i18n.locale = lang + const quasarLangCode = lang.toLowerCase() import( /* webpackInclude: /(en-us|de|es|fr|it|ru)\.js$/ */ - 'quasar/lang/' + lang.toLowerCase() + 'quasar/lang/' + quasarLangCode ).then(lang => { Quasar.lang.set(lang.default) }) @@ -94,3 +95,15 @@ export function convertLangV1toV2 (lang) { export function getCurrentLangAsV1Format () { return convertLangV2toV1(i18n.locale) } + +export function normalizeLocaleCode (locale) { + const shortLangCode = String(locale || defaultLocale).substr(0, 2).toLowerCase() + const langCodeInV2Format = (shortLangCode === 'en') ? 'en-US' : shortLangCode + const langCode = Object.keys(messages).filter(l => l === langCodeInV2Format)[0] + return langCode || defaultLocale +} + +export function getLangFromBrowserDefaults () { + const browserLanguage = navigator?.language + return normalizeLocaleCode(browserLanguage) +}