This commit is contained in:
parent
d85f08fb7a
commit
9a9a60f380
|
|
@ -22,7 +22,7 @@ export function LanguageSwitcher() {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const saved = localStorage.getItem('preferred-language')
|
const saved = localStorage.getItem('preferred-language')
|
||||||
|
|
||||||
console.log('[LanguageSwitcher]=> Read from localstorage preferred-language=', saved)
|
// console.log('[LanguageSwitcher]=> Read from localstorage preferred-language=', saved)
|
||||||
|
|
||||||
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
||||||
|
|
||||||
|
|
@ -32,44 +32,74 @@ export function LanguageSwitcher() {
|
||||||
}
|
}
|
||||||
}, [currentLocale])
|
}, [currentLocale])
|
||||||
|
|
||||||
|
// const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||||
|
// const newLocale = e.target.value
|
||||||
|
// // 仅在用户选择语言后保存 cookie
|
||||||
|
// // console.log('[LanguageSwitcher] Write Cookie newLocale value:', newLocale)
|
||||||
|
// // document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000`
|
||||||
|
|
||||||
|
// // 同时更新 localStorage
|
||||||
|
// localStorage.setItem('preferred-language', newLocale)
|
||||||
|
|
||||||
|
// // 验证 cookie 是否成功写入
|
||||||
|
// // const cookieValue = document.cookie
|
||||||
|
|
||||||
|
// // 更新 i18n 的语言
|
||||||
|
// i18n.changeLanguage(newLocale) // 修改 TranslationProvider 中的 locale
|
||||||
|
|
||||||
|
// let segments = pathname === '/' ? [] : pathname.split('/').filter(Boolean)
|
||||||
|
// const isLocaleInPath = i18nConfig.locales.includes(segments[0] as any)
|
||||||
|
|
||||||
|
// if (isLocaleInPath) {
|
||||||
|
// segments[0] = newLocale
|
||||||
|
// } else {
|
||||||
|
// segments.unshift(newLocale)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const newPath = '/' + segments.join('/')
|
||||||
|
|
||||||
|
// // 延迟跳转,确保 cookie 写入完成
|
||||||
|
// setTimeout(() => {
|
||||||
|
// startTransition(() => {
|
||||||
|
// if (pathname !== newPath) {
|
||||||
|
// router.push(newPath)
|
||||||
|
// } else {
|
||||||
|
// router.refresh()
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// }, 0)
|
||||||
|
// }
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||||
const newLocale = e.target.value
|
const newLocale = e.target.value
|
||||||
// 仅在用户选择语言后保存 cookie
|
|
||||||
console.log('[LanguageSwitcher] Write Cookie newLocale value:', newLocale)
|
// 更新 localStorage
|
||||||
document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000`
|
|
||||||
|
|
||||||
// 同时更新 localStorage
|
|
||||||
localStorage.setItem('preferred-language', newLocale)
|
localStorage.setItem('preferred-language', newLocale)
|
||||||
|
|
||||||
// 验证 cookie 是否成功写入
|
|
||||||
const cookieValue = document.cookie
|
|
||||||
|
|
||||||
// 更新 i18n 的语言
|
// 更新 i18n 的语言
|
||||||
i18n.changeLanguage(newLocale) // 修改 TranslationProvider 中的 locale
|
i18n.changeLanguage(newLocale)
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
if (isLocaleInPath) {
|
if (isLocaleInPath) {
|
||||||
segments[0] = newLocale
|
segments[0] = newLocale
|
||||||
} else {
|
} else {
|
||||||
segments.unshift(newLocale)
|
segments.unshift(newLocale)
|
||||||
}
|
}
|
||||||
|
|
||||||
const newPath = '/' + segments.join('/')
|
const newPath = '/' + segments.join('/')
|
||||||
|
|
||||||
// 延迟跳转,确保 cookie 写入完成
|
startTransition(() => {
|
||||||
setTimeout(() => {
|
if (pathname !== newPath) {
|
||||||
startTransition(() => {
|
router.push(newPath)
|
||||||
if (pathname !== newPath) {
|
} else {
|
||||||
router.push(newPath)
|
router.refresh()
|
||||||
} else {
|
}
|
||||||
router.refresh()
|
})
|
||||||
}
|
|
||||||
})
|
|
||||||
}, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="absolute top-4 right-4 z-50 flex items-center gap-2">
|
<div className="absolute top-4 right-4 z-50 flex items-center gap-2">
|
||||||
<Globe size={18} className="text-muted-foreground" />
|
<Globe size={18} className="text-muted-foreground" />
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ export default function HomeRedirector() {
|
||||||
console.log('[HomeRedirector] localStorage preferred:', preferred)
|
console.log('[HomeRedirector] localStorage preferred:', preferred)
|
||||||
|
|
||||||
if (!preferred || !isValidLocale(preferred)) {
|
if (!preferred || !isValidLocale(preferred)) {
|
||||||
console.log('[HomeRedirector] No valid preferred language found.')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,7 +34,7 @@ export default function HomeRedirector() {
|
||||||
|
|
||||||
if (currentPath === "/" && !hasLocaleInPath) {
|
if (currentPath === "/" && !hasLocaleInPath) {
|
||||||
const newPath = `/${preferred}${currentPath}`
|
const newPath = `/${preferred}${currentPath}`
|
||||||
console.log('[HomeRedirector] Redirecting to:', newPath)
|
// console.log('[HomeRedirector] Redirecting to:', newPath)
|
||||||
router.replace(newPath)
|
router.replace(newPath)
|
||||||
} else {
|
} else {
|
||||||
console.log('[HomeRedirector] Skipping redirect.')
|
console.log('[HomeRedirector] Skipping redirect.')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue