metacubexd/src/i18n/index.tsx
2023-09-22 17:14:35 +08:00

33 lines
744 B
TypeScript

import * as i18n from '@solid-primitives/i18n'
import { makePersisted } from '@solid-primitives/storage'
import { createMemo, createSignal } from 'solid-js'
import { LANG } from '~/constants'
import dict from './dict'
const [curLocale, setCurLocale] = makePersisted(
createSignal<LANG>(
Reflect.has(dict, navigator.language)
? (navigator.language as LANG)
: LANG.EN,
),
{
name: 'lang',
storage: localStorage,
},
)
const locale = (localeName?: LANG) => {
if (localeName) {
return setCurLocale(localeName)
}
return curLocale()
}
export const useI18n = () => {
const curDict = createMemo(() => i18n.flatten(dict[curLocale()]))!
const t = i18n.translator(() => curDict())
return { t, locale }
}