This commit is contained in:
parent
d6a7c26849
commit
6fbfbf53b8
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue