This commit is contained in:
parent
02f5463162
commit
e977c98618
|
|
@ -12,15 +12,18 @@ export function LanguageSwitcher() {
|
||||||
const [isPending, startTransition] = useTransition()
|
const [isPending, startTransition] = useTransition()
|
||||||
|
|
||||||
const { i18n } = useTranslation() // 获取 i18n 实例
|
const { i18n } = useTranslation() // 获取 i18n 实例
|
||||||
|
|
||||||
|
console.log('[LanguageSwitcher]=>Current Pathname=', pathname)
|
||||||
|
|
||||||
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// 只在加载时检查,避免多次保存 cookie
|
// 只在加载时检查,避免多次保存 cookie
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const saved = localStorage.getItem('preferred-language')
|
const saved = localStorage.getItem('preferred-language')
|
||||||
console.log('Read cookie from localstorage preferred-language=', saved)
|
console.log('[LanguageSwitcher]=> Read cookie from localstorage preferred-language=', saved)
|
||||||
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
const isLocaleInPath = i18nConfig.locales.includes(currentLocale as any)
|
||||||
console.log('i18nConfig.locales currentLocale=', currentLocale)
|
console.log('[LanguageSwitcher]=>i18nConfig.locales currentLocale=', currentLocale)
|
||||||
|
|
||||||
// 只有在 URL 中没有 locale 前缀时才进行处理
|
// 只有在 URL 中没有 locale 前缀时才进行处理
|
||||||
if (!isLocaleInPath && saved !== currentLocale) {
|
if (!isLocaleInPath && saved !== currentLocale) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue