metacubexd/src/i18n/index.tsx

28 lines
771 B
TypeScript
Raw Normal View History

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