From dea1789a00981fb496f0c1f4c19a6f0749e4de70 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Mon, 17 Oct 2022 13:14:07 +0200 Subject: [PATCH] feat: type i18n improvements --- src/i18n/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 6987a5bbd..77ebf216a 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -15,7 +15,7 @@ export const SUPPORTED_LOCALES = { 'pt-PT': 'Português', 'zh-CN': 'Chinese', 'no-NO': 'Norsk Bokmål', -} as Record +} as const export type SupportedLocale = keyof typeof SUPPORTED_LOCALES @@ -23,12 +23,12 @@ export const DEFAULT_LANGUAGE: SupportedLocale= 'en' export type ISOLanguage = string -// we load all messsages async +// we load all messages async export const i18n = createI18n({ fallbackLocale: DEFAULT_LANGUAGE, legacy: false, messages: { - en: langEN, + [DEFAULT_LANGUAGE]: langEN, } as Record, }) @@ -54,16 +54,16 @@ export async function setLanguage(lang: SupportedLocale = getCurrentLanguage()): } export function getCurrentLanguage(): SupportedLocale { - const savedLanguage = localStorage.getItem('language') + const savedLanguage = localStorage.getItem('language') as SupportedLocale | null if (savedLanguage !== null) { return savedLanguage } const browserLanguage = navigator.language - const language: SupportedLocale | undefined = Object.keys(SUPPORTED_LOCALES).find(langKey => { + const language = Object.keys(SUPPORTED_LOCALES).find(langKey => { return langKey === browserLanguage || langKey.startsWith(browserLanguage + '-') - }) + }) as SupportedLocale | undefined return language || DEFAULT_LANGUAGE }