Skip to content

Commit

Permalink
Merge pull request #169 from swedenconnect/feature/IS-168-Save-langua…
Browse files Browse the repository at this point in the history
…ge-setting

Feature/is 168 save language setting
  • Loading branch information
martin-lindstrom authored Oct 3, 2023
2 parents 0e8905c + 922f233 commit 0a2e88f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bankid-frontend/src/components/LocaleChanger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
const selectLanguage = (lang: string) => {
locale.value = lang;
document.querySelector('html')?.setAttribute('lang', locale.value);
localStorage.setItem('locale', locale.value);
};
</script>

Expand Down
23 changes: 22 additions & 1 deletion bankid-frontend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,30 @@ 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';
}

document.querySelector('html')?.setAttribute('lang', locale);

const i18n = createI18n({
legacy: false,
locale: 'sv',
locale: locale,
fallbackLocale: 'en',
messages: messagesToBeLoaded,
});
Expand Down

0 comments on commit 0a2e88f

Please sign in to comment.