This commit is contained in:
parent
4696d3f364
commit
caf341f9cd
|
|
@ -15,24 +15,32 @@ export function LanguageSwitcher() {
|
|||
useEffect(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const saved = localStorage.getItem('preferred-language')
|
||||
console.log('[LanguageSwitcher] currentLocale:', currentLocale)
|
||||
console.log('[LanguageSwitcher] saved preferred-language:', saved)
|
||||
|
||||
if (!saved || saved !== currentLocale) {
|
||||
localStorage.setItem('preferred-language', currentLocale)
|
||||
console.log('[LanguageSwitcher] preferred-language updated to:', currentLocale)
|
||||
}
|
||||
}
|
||||
}, [currentLocale])
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
const newLocale = e.target.value
|
||||
console.log('[LanguageSwitcher] User selected newLocale:', newLocale)
|
||||
console.log('[LanguageSwitcher] Current pathname:', pathname)
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('preferred-language', newLocale)
|
||||
console.log('[LanguageSwitcher] Saved newLocale to localStorage')
|
||||
}
|
||||
|
||||
const segments = pathname.split('/')
|
||||
console.log('[LanguageSwitcher] Segments before change:', segments)
|
||||
|
||||
// ✅ 修复:类型断言 readonly -> string[]
|
||||
const localeList = [...i18nConfig.locales] as string[]
|
||||
const isLocaleInPath = localeList.includes(segments[1])
|
||||
console.log('[LanguageSwitcher] isLocaleInPath:', isLocaleInPath)
|
||||
|
||||
if (isLocaleInPath) {
|
||||
segments[1] = newLocale
|
||||
|
|
@ -41,6 +49,7 @@ export function LanguageSwitcher() {
|
|||
}
|
||||
|
||||
const newPath = segments.join('/')
|
||||
console.log('[LanguageSwitcher] Final newPath:', newPath)
|
||||
|
||||
startTransition(() => {
|
||||
router.push(newPath)
|
||||
|
|
|
|||
Loading…
Reference in New Issue