diff --git a/components/ui/language-switcher.tsx b/components/ui/language-switcher.tsx index 9368c52..08a1692 100644 --- a/components/ui/language-switcher.tsx +++ b/components/ui/language-switcher.tsx @@ -17,19 +17,30 @@ export function LanguageSwitcher() { const saved = localStorage.getItem('preferred-language') const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any) - // ✅ 只有在 URL 中没有 locale 前缀时才写入 cookie 和 localStorage,避免错误覆盖 + // 只有在 URL 中没有 locale 前缀时才写入 cookie 和 localStorage,避免错误覆盖 if (!isLocaleInPath && saved !== currentLocale) { localStorage.setItem('preferred-language', currentLocale) document.cookie = `preferred-language=${currentLocale}; path=/; max-age=31536000` + console.log('[LanguageSwitcher] set preferred-language to:', currentLocale) + + // 确保 cookie 写入 + const cookieValue = document.cookie + console.log('[LanguageSwitcher] Cookie after setting:', cookieValue) } } }, [currentLocale]) const handleChange = (e: React.ChangeEvent) => { const newLocale = e.target.value + + // 写入 cookie document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000` + // 检查 cookie 是否正确写入 + const cookieValue = document.cookie + console.log('[LanguageSwitcher] Cookie set to:', cookieValue) + let segments = pathname === '/' ? [] : pathname.split('/').filter(Boolean) const isLocaleInPath = i18nConfig.locales.includes(segments[0] as any) @@ -41,7 +52,7 @@ export function LanguageSwitcher() { const newPath = '/' + segments.join('/') - // ✅ 延迟跳转,确保 cookie 写入完成 + // 延迟跳转,确保 cookie 写入完成 setTimeout(() => { startTransition(() => { if (pathname !== newPath) {