This commit is contained in:
hailin 2025-04-17 13:13:05 +08:00
parent d6a7c26849
commit 6fbfbf53b8
2 changed files with 12 additions and 4 deletions

View File

@ -23,6 +23,10 @@ export function LanguageSwitcher() {
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => { const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const newLocale = e.target.value const newLocale = e.target.value
// ✅ 同步设置 cookie供 middleware.ts 使用
document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000` // 保存 1 年
let segments = pathname === '/' ? [] : pathname.split('/').filter(Boolean) let segments = pathname === '/' ? [] : pathname.split('/').filter(Boolean)
const isLocaleInPath = i18nConfig.locales.includes(segments[0] as any) const isLocaleInPath = i18nConfig.locales.includes(segments[0] as any)

View File

@ -23,6 +23,9 @@ export default function HomeRedirector() {
return return
} }
// ✅ 同步写入 cookie
document.cookie = `preferred-language=${preferred}; path=/; max-age=31536000` // 1 年
const pathLocale = currentPath.split('/')[1] const pathLocale = currentPath.split('/')[1]
const hasLocaleInPath = isValidLocale(pathLocale) const hasLocaleInPath = isValidLocale(pathLocale)
@ -35,5 +38,6 @@ export default function HomeRedirector() {
} }
}, []) }, [])
return null return null
} }