feat: type i18n improvements
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dominik Pschenitschni 2022-10-17 13:14:07 +02:00
parent 30adad5ae6
commit dea1789a00
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
1 changed files with 6 additions and 6 deletions

View File

@ -15,7 +15,7 @@ export const SUPPORTED_LOCALES = {
'pt-PT': 'Português', 'pt-PT': 'Português',
'zh-CN': 'Chinese', 'zh-CN': 'Chinese',
'no-NO': 'Norsk Bokmål', 'no-NO': 'Norsk Bokmål',
} as Record<string, string> } as const
export type SupportedLocale = keyof typeof SUPPORTED_LOCALES export type SupportedLocale = keyof typeof SUPPORTED_LOCALES
@ -23,12 +23,12 @@ export const DEFAULT_LANGUAGE: SupportedLocale= 'en'
export type ISOLanguage = string export type ISOLanguage = string
// we load all messsages async // we load all messages async
export const i18n = createI18n({ export const i18n = createI18n({
fallbackLocale: DEFAULT_LANGUAGE, fallbackLocale: DEFAULT_LANGUAGE,
legacy: false, legacy: false,
messages: { messages: {
en: langEN, [DEFAULT_LANGUAGE]: langEN,
} as Record<SupportedLocale, any>, } as Record<SupportedLocale, any>,
}) })
@ -54,16 +54,16 @@ export async function setLanguage(lang: SupportedLocale = getCurrentLanguage()):
} }
export function getCurrentLanguage(): SupportedLocale { export function getCurrentLanguage(): SupportedLocale {
const savedLanguage = localStorage.getItem('language') const savedLanguage = localStorage.getItem('language') as SupportedLocale | null
if (savedLanguage !== null) { if (savedLanguage !== null) {
return savedLanguage return savedLanguage
} }
const browserLanguage = navigator.language 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 + '-') return langKey === browserLanguage || langKey.startsWith(browserLanguage + '-')
}) }) as SupportedLocale | undefined
return language || DEFAULT_LANGUAGE return language || DEFAULT_LANGUAGE
} }