import { createContextProvider } from '@solid-primitives/context' import * as i18n from '@solid-primitives/i18n' import { makePersisted } from '@solid-primitives/storage' import { createSignal } from 'solid-js' import { LANG } from '~/constants' import dict, { Dict } from './dict' export const [locale, setLocale] = makePersisted( createSignal( Reflect.has(dict, navigator.language) ? (navigator.language as LANG) : LANG.EN, ), { name: 'lang', storage: localStorage, }, ) export const [I18nProvider, useMaybeI18n] = createContextProvider< [i18n.Translator], { locale: LANG } >((props) => [i18n.translator(() => i18n.flatten(dict[props.locale]))]) export const useI18n = () => useMaybeI18n()!