This commit is contained in:
hailin 2025-04-17 11:28:19 +08:00
parent 4696d3f364
commit caf341f9cd
1 changed files with 10 additions and 1 deletions

View File

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