metacubexd/src/i18n/index.tsx

28 lines
742 B
TypeScript
Raw Normal View History

2023-09-22 17:14:35 +08:00
import * as i18n from '@solid-primitives/i18n'
2023-09-01 23:57:58 +08:00
import { makePersisted } from '@solid-primitives/storage'
2023-09-22 17:14:35 +08:00
import { createMemo, createSignal } from 'solid-js'
import { LANG } from '~/constants'
2023-09-02 13:50:24 +08:00
import dict from './dict'
2023-09-01 23:25:37 +08:00
export const [curLocale, setCurLocale] = makePersisted(
2023-09-22 17:14:35 +08:00
createSignal<LANG>(
Reflect.has(dict, navigator.language)
? (navigator.language as LANG)
: LANG.EN,
),
{
name: 'lang',
storage: localStorage,
},
)
2023-09-01 23:57:58 +08:00
const locale = (localeName?: LANG) =>
localeName ? setCurLocale(localeName) : curLocale()
2023-09-01 23:57:58 +08:00
2023-09-22 17:14:35 +08:00
export const useI18n = () => {
const curDict = createMemo(() => i18n.flatten(dict[curLocale()]))!
const t = createMemo(() => i18n.translator(() => curDict()))
2023-09-01 23:25:37 +08:00
return { t: t(), locale }
2023-09-01 23:25:37 +08:00
}