40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import i18nConfig from '@/i18nConfig'
|
|
|
|
const isValidLocale = (locale: string): boolean => {
|
|
const localeList = [...i18nConfig.locales] as string[]
|
|
return localeList.includes(locale)
|
|
}
|
|
|
|
export default function HomeRedirector() {
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
|
|
useEffect(() => {
|
|
if (pathname !== '/') return
|
|
|
|
const preferred = localStorage.getItem('preferred-language')
|
|
const currentPath = window.location.pathname
|
|
|
|
console.log('[HomeRedirector] preferred:', preferred)
|
|
console.log('[HomeRedirector] currentPath:', currentPath)
|
|
|
|
if (
|
|
preferred &&
|
|
isValidLocale(preferred) &&
|
|
!currentPath.startsWith(`/${preferred}/`)
|
|
) {
|
|
const newPath = `/${preferred}${currentPath}`
|
|
console.log('[HomeRedirector] Redirecting to:', newPath)
|
|
router.replace(newPath)
|
|
} else {
|
|
console.log('[HomeRedirector] No redirect needed.')
|
|
}
|
|
}, [])
|
|
|
|
return null
|
|
}
|