This commit is contained in:
parent
9b763db533
commit
c108dc2998
|
|
@ -13,7 +13,7 @@ export function LanguageSwitcher() {
|
||||||
|
|
||||||
const { i18n } = useTranslation() // 获取 i18n 实例
|
const { i18n } = useTranslation() // 获取 i18n 实例
|
||||||
|
|
||||||
console.log('[LanguageSwitcher]=>Current Pathname=', pathname)
|
console.log('[LanguageSwitcher]=>Current url Pathname=', pathname)
|
||||||
|
|
||||||
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
||||||
|
|
||||||
|
|
@ -21,18 +21,16 @@ export function LanguageSwitcher() {
|
||||||
// 只在加载时检查,避免多次保存 cookie
|
// 只在加载时检查,避免多次保存 cookie
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const saved = localStorage.getItem('preferred-language')
|
const saved = localStorage.getItem('preferred-language')
|
||||||
console.log('[LanguageSwitcher]=> Read cookie from localstorage preferred-language=', saved)
|
|
||||||
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
|
||||||
console.log('[LanguageSwitcher]=>i18nConfig.locales currentLocale=', currentLocale)
|
|
||||||
|
|
||||||
// 只有在 URL 中没有 locale 前缀时才进行处理
|
console.log('[LanguageSwitcher]=> Read from localstorage preferred-language=', saved)
|
||||||
if (!isLocaleInPath && saved !== currentLocale) {
|
|
||||||
// 不做 cookie 写入,除非用户选择语言
|
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
||||||
console.log('[LanguageSwitcher] No need to update cookie, currentLocale:', currentLocale)
|
|
||||||
}
|
console.log('[LanguageSwitcher]=>i18nConfig.locales default=', currentLocale)
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
console.log('language switcher noi running in server.')
|
console.log('[LanguageSwitcher] language switcher cannt running in server.')
|
||||||
}
|
}
|
||||||
}, [currentLocale])
|
}, [currentLocale])
|
||||||
|
|
||||||
|
|
@ -43,13 +41,12 @@ export function LanguageSwitcher() {
|
||||||
document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000`
|
document.cookie = `preferred-language=${newLocale}; path=/; max-age=31536000`
|
||||||
|
|
||||||
// 同时更新 localStorage
|
// 同时更新 localStorage
|
||||||
localStorage.setItem('preferred-language', newLocale)
|
|
||||||
console.log('[LanguageSwitcher] Write to localStorage newLocale value:', newLocale)
|
console.log('[LanguageSwitcher] Write to localStorage newLocale value:', newLocale)
|
||||||
|
localStorage.setItem('preferred-language', newLocale)
|
||||||
|
|
||||||
// 验证 cookie 是否成功写入
|
// 验证 cookie 是否成功写入
|
||||||
|
console.log('[LanguageSwitcher] Read back to check if cookie value saved?', cookieValue)
|
||||||
const cookieValue = document.cookie
|
const cookieValue = document.cookie
|
||||||
console.log('[LanguageSwitcher] Check writed Cookie value:', cookieValue)
|
|
||||||
|
|
||||||
|
|
||||||
// 更新 i18n 的语言
|
// 更新 i18n 的语言
|
||||||
i18n.changeLanguage(newLocale) // 修改 TranslationProvider 中的 locale
|
i18n.changeLanguage(newLocale) // 修改 TranslationProvider 中的 locale
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,10 @@ export default function HomeRedirector() {
|
||||||
|
|
||||||
if (!hasLocaleInPath) {
|
if (!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] Already has valid locale in path, skipping redirect.')
|
console.log('[HomeRedirector] Skipping redirect.')
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue