2023-09-23 01:38:36 +08:00
|
|
|
import { createContextProvider } from '@solid-primitives/context'
|
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-23 01:38:36 +08:00
|
|
|
import { createSignal } from 'solid-js'
|
2023-09-03 03:26:29 +08:00
|
|
|
import { LANG } from '~/constants'
|
2023-09-23 01:38:36 +08:00
|
|
|
import dict, { Dict } from './dict'
|
2023-09-01 23:25:37 +08:00
|
|
|
|
2023-09-23 01:38:36 +08:00
|
|
|
export const [locale, setLocale] = 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 01:38:36 +08:00
|
|
|
export const [I18nProvider, useMaybeI18n] = createContextProvider<
|
|
|
|
[i18n.Translator<Dict>],
|
|
|
|
{ locale: LANG }
|
2023-09-23 02:13:17 +08:00
|
|
|
>((props) => [
|
|
|
|
i18n.translator(() => i18n.flatten(dict[props.locale]), i18n.resolveTemplate),
|
|
|
|
])
|
2023-09-01 23:57:58 +08:00
|
|
|
|
2023-09-23 01:38:36 +08:00
|
|
|
export const useI18n = () => useMaybeI18n()!
|