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'
|
2023-09-03 03:26:29 +08:00
|
|
|
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-23 00:33:48 +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
|
|
|
|
2023-09-23 00:33:48 +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()]))!
|
2023-09-23 00:33:48 +08:00
|
|
|
const t = createMemo(() => i18n.translator(() => curDict()))
|
2023-09-01 23:25:37 +08:00
|
|
|
|
2023-09-23 00:33:48 +08:00
|
|
|
return { t: t(), locale }
|
2023-09-01 23:25:37 +08:00
|
|
|
}
|