2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
} from 'use-context-selector'
|
2023-05-15 08:51:32 +08:00
|
|
|
import type { Locale } from '@/i18n'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { getLanguage } from '@/i18n/language'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
type II18NContext = {
|
|
|
|
locale: Locale
|
2023-08-23 13:54:40 +08:00
|
|
|
i18n: Record<string, any>
|
|
|
|
setLocaleOnClient: (locale: Locale, reloadPage?: boolean) => void
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const I18NContext = createContext<II18NContext>({
|
2024-02-23 14:31:06 +08:00
|
|
|
locale: 'en-US',
|
2023-05-15 08:51:32 +08:00
|
|
|
i18n: {},
|
2023-08-23 13:54:40 +08:00
|
|
|
setLocaleOnClient: (lang: Locale, reloadPage?: boolean) => { },
|
2023-05-15 08:51:32 +08:00
|
|
|
})
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
export const useI18N = () => useContext(I18NContext)
|
|
|
|
export const useGetLanguage = () => {
|
|
|
|
const { locale } = useI18N()
|
|
|
|
|
|
|
|
return getLanguage(locale)
|
|
|
|
}
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
export default I18NContext
|