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

View File

@ -29,28 +29,28 @@ export function LanguageSwitcher() {
const newLocale = e.target.value const newLocale = e.target.value
console.log('[LanguageSwitcher] User selected newLocale:', newLocale) console.log('[LanguageSwitcher] User selected newLocale:', newLocale)
console.log('[LanguageSwitcher] Current pathname:', pathname) 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') console.log('[LanguageSwitcher] Saved newLocale to localStorage')
} }
const segments = pathname.split('/') let segments = pathname.split('/').filter(Boolean)
console.log('[LanguageSwitcher] Segments before change:', segments) console.log('[LanguageSwitcher] Segments before change:', segments)
const localeList = [...i18nConfig.locales] as string[] const localeList = [...i18nConfig.locales] as string[]
const isLocaleInPath = localeList.includes(segments[1]) const isLocaleInPath = localeList.includes(segments[0])
console.log('[LanguageSwitcher] isLocaleInPath:', isLocaleInPath) console.log('[LanguageSwitcher] isLocaleInPath:', isLocaleInPath)
if (isLocaleInPath) { if (isLocaleInPath) {
segments[1] = newLocale segments[0] = newLocale
} else { } else {
segments.unshift(newLocale) segments.unshift(newLocale)
} }
const newPath = segments.join('/') const newPath = '/' + segments.join('/')
console.log('[LanguageSwitcher] Final newPath:', newPath) console.log('[LanguageSwitcher] Final newPath:', newPath)
startTransition(() => { startTransition(() => {
router.push(newPath) router.push(newPath)
router.refresh() router.refresh()