knowyt/client/src/composables/useI18n.ts
2022-09-09 08:31:33 +02:00

38 lines
744 B
TypeScript

import type { Lang } from '@/composables/engine.d'
export type i18nMap = {
[key: string]: {
en: string,
de: string,
},
}
const browserLang = navigator.language ? navigator.language.substring(0, 2) : 'en'
const defaultLang: Lang = browserLang === 'de' ? 'de' : 'en'
let lang: Lang = defaultLang
export interface useI18n {
setLang(lang: Lang): void
getLang(): Lang
$t(key: string): string
}
export default (map: i18nMap): useI18n => {
return {
setLang: (_lang: Lang): void => {
lang = _lang
},
getLang: (): Lang => {
return lang
},
$t: (key: string): string => {
const t = map[key]
if (!t) {
return key
}
return t[lang] || t[defaultLang] || key
}
}
}