This commit is contained in:
parent
3b03eca2b4
commit
4a3347c55c
|
|
@ -4,13 +4,6 @@ import { useEffect } from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import i18nConfig from '@/i18nConfig'
|
import i18nConfig from '@/i18nConfig'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const isValidLocale = (locale: string): locale is (typeof i18nConfig.locales)[number] => {
|
|
||||||
return (i18nConfig.locales as readonly string[]).includes(locale)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export default function HomeRedirector() {
|
export default function HomeRedirector() {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
|
@ -18,15 +11,25 @@ export default function HomeRedirector() {
|
||||||
const preferred = localStorage.getItem('preferred-language')
|
const preferred = localStorage.getItem('preferred-language')
|
||||||
const currentPath = window.location.pathname
|
const currentPath = window.location.pathname
|
||||||
|
|
||||||
|
console.log('[HomeRedirector] currentPath:', currentPath)
|
||||||
|
console.log('[HomeRedirector] preferred-language from storage:', preferred)
|
||||||
|
|
||||||
|
const isValidLocale = (lang: string) =>
|
||||||
|
(i18nConfig.locales as string[]).includes(lang)
|
||||||
|
|
||||||
|
// 若路径中已经有 locale 前缀,或 preferred 为空 / 非法,跳过
|
||||||
if (
|
if (
|
||||||
preferred &&
|
preferred &&
|
||||||
isValidLocale(preferred) &&
|
isValidLocale(preferred) &&
|
||||||
!currentPath.startsWith(`/${preferred}/`)
|
!currentPath.startsWith(`/${preferred}/`) &&
|
||||||
|
!i18nConfig.locales.some(locale => currentPath.startsWith(`/${locale}/`))
|
||||||
) {
|
) {
|
||||||
const newPath = `/${preferred}${currentPath}`
|
const newPath = `/${preferred}${currentPath}`
|
||||||
|
console.log('[HomeRedirector] Redirecting to:', newPath)
|
||||||
router.replace(newPath)
|
router.replace(newPath)
|
||||||
|
} else {
|
||||||
|
console.log('[HomeRedirector] No redirection triggered')
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue