metacubexd/src/i18n/index.tsx

33 lines
744 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
2023-09-22 17:14:35 +08:00
const [curLocale, setCurLocale] = makePersisted(
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
2023-09-22 17:14:35 +08:00
const locale = (localeName?: LANG) => {
if (localeName) {
return setCurLocale(localeName)
}
2023-09-01 23:57:58 +08:00
2023-09-22 17:14:35 +08:00
return 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 = i18n.translator(() => curDict())
2023-09-01 23:25:37 +08:00
2023-09-22 17:14:35 +08:00
return { t, locale }
2023-09-01 23:25:37 +08:00
}