This commit is contained in:
parent
f533724db5
commit
6ed20feda8
|
|
@ -3,6 +3,7 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import i18nConfig from '@/i18nConfig'
|
||||
import { useTranslation } from 'react-i18next' // 引入 useTranslation
|
||||
|
||||
const isValidLocale = (locale: string): locale is (typeof i18nConfig.locales)[number] => {
|
||||
return (i18nConfig.locales as readonly string[]).includes(locale)
|
||||
|
|
@ -10,6 +11,7 @@ const isValidLocale = (locale: string): locale is (typeof i18nConfig.locales)[nu
|
|||
|
||||
export default function HomeRedirector() {
|
||||
const router = useRouter()
|
||||
const { i18n } = useTranslation() // 获取 i18n 实例
|
||||
|
||||
useEffect(() => {
|
||||
const preferred = localStorage.getItem('preferred-language')
|
||||
|
|
@ -25,6 +27,9 @@ export default function HomeRedirector() {
|
|||
|
||||
// ✅ 同步写入 cookie
|
||||
document.cookie = `preferred-language=${preferred}; path=/; max-age=31536000` // 1 年
|
||||
|
||||
// 修改 i18n 中的语言
|
||||
i18n.changeLanguage(preferred) // 更新 i18n 的 locale
|
||||
|
||||
const pathLocale = currentPath.split('/')[1]
|
||||
const hasLocaleInPath = isValidLocale(pathLocale)
|
||||
|
|
|
|||
Loading…
Reference in New Issue