From 9453de5312aa5706a827337fbd45b2b4deba1a07 Mon Sep 17 00:00:00 2001 From: Mattias Kesti Date: Tue, 3 Oct 2023 14:07:35 +0200 Subject: [PATCH 1/2] Save (and restore) language setting --- .../src/components/LocaleChanger.vue | 1 + bankid-frontend/src/main.ts | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/bankid-frontend/src/components/LocaleChanger.vue b/bankid-frontend/src/components/LocaleChanger.vue index 60abc7ef..295c39d9 100644 --- a/bankid-frontend/src/components/LocaleChanger.vue +++ b/bankid-frontend/src/components/LocaleChanger.vue @@ -5,6 +5,7 @@ const selectLanguage = (lang: string) => { locale.value = lang; + localStorage.setItem('locale', locale.value); }; diff --git a/bankid-frontend/src/main.ts b/bankid-frontend/src/main.ts index 21ee85a6..c2f7705b 100644 --- a/bankid-frontend/src/main.ts +++ b/bankid-frontend/src/main.ts @@ -64,9 +64,28 @@ async function initializeApp() { customContent = overrides.content; } + const ALL_LOCALES = Object.keys(messagesToBeLoaded); + type LocaleType = (typeof ALL_LOCALES)[number]; + + function isLocaleType(value: string): value is LocaleType { + return ALL_LOCALES.includes(value as LocaleType); + } + + const storedLocale = localStorage.getItem('locale') ?? ''; + const browserLocale = navigator.language.slice(0, 2); + + let locale: LocaleType; + if (isLocaleType(storedLocale)) { + locale = storedLocale; + } else if (isLocaleType(browserLocale)) { + locale = browserLocale; + } else { + locale = 'sv'; + } + const i18n = createI18n({ legacy: false, - locale: 'sv', + locale: locale, fallbackLocale: 'en', messages: messagesToBeLoaded, }); From 922f23372e7cc76a5ddb1e51beb8a27578e20c15 Mon Sep 17 00:00:00 2001 From: Mattias Kesti Date: Tue, 3 Oct 2023 14:07:59 +0200 Subject: [PATCH 2/2] Set lang attribute --- bankid-frontend/src/components/LocaleChanger.vue | 1 + bankid-frontend/src/main.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/bankid-frontend/src/components/LocaleChanger.vue b/bankid-frontend/src/components/LocaleChanger.vue index 295c39d9..e219d5c9 100644 --- a/bankid-frontend/src/components/LocaleChanger.vue +++ b/bankid-frontend/src/components/LocaleChanger.vue @@ -5,6 +5,7 @@ const selectLanguage = (lang: string) => { locale.value = lang; + document.querySelector('html')?.setAttribute('lang', locale.value); localStorage.setItem('locale', locale.value); }; diff --git a/bankid-frontend/src/main.ts b/bankid-frontend/src/main.ts index c2f7705b..623e6967 100644 --- a/bankid-frontend/src/main.ts +++ b/bankid-frontend/src/main.ts @@ -83,6 +83,8 @@ async function initializeApp() { locale = 'sv'; } + document.querySelector('html')?.setAttribute('lang', locale); + const i18n = createI18n({ legacy: false, locale: locale,