This commit is contained in:
parent
a5a97137f6
commit
5b4116dfe7
|
|
@ -5,6 +5,9 @@ import { IconArrowRight } from "@tabler/icons-react"
|
||||||
import { useTheme } from "next-themes"
|
import { useTheme } from "next-themes"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
|
|
||||||
|
import HomeRedirector from "@/components/utility/home-redirector"
|
||||||
|
|
||||||
|
|
||||||
import { LanguageSwitcher } from '@/components/ui/language-switcher'
|
import { LanguageSwitcher } from '@/components/ui/language-switcher'
|
||||||
|
|
||||||
export default function HomePage() {
|
export default function HomePage() {
|
||||||
|
|
@ -13,6 +16,7 @@ export default function HomePage() {
|
||||||
return (
|
return (
|
||||||
<div className="flex size-full flex-col items-center justify-center relative">
|
<div className="flex size-full flex-col items-center justify-center relative">
|
||||||
|
|
||||||
|
<HomeRedirector />
|
||||||
<LanguageSwitcher />
|
<LanguageSwitcher />
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import i18nConfig from '@/i18nConfig'
|
import i18nConfig from '@/i18nConfig'
|
||||||
import { usePathname, useRouter } from 'next/navigation'
|
import { usePathname, useRouter } from 'next/navigation'
|
||||||
import { useTransition } from 'react'
|
import { useEffect, useTransition } from 'react'
|
||||||
import { Globe } from 'lucide-react'
|
import { Globe } from 'lucide-react'
|
||||||
|
|
||||||
export function LanguageSwitcher() {
|
export function LanguageSwitcher() {
|
||||||
|
|
@ -12,8 +12,25 @@ export function LanguageSwitcher() {
|
||||||
|
|
||||||
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// 第一次访问页面时,如果 URL 有语言,就记住
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
const saved = localStorage.getItem('preferred-language')
|
||||||
|
if (!saved || saved !== currentLocale) {
|
||||||
|
localStorage.setItem('preferred-language', currentLocale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [currentLocale])
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||||
const newLocale = e.target.value
|
const newLocale = e.target.value
|
||||||
|
|
||||||
|
// 保存用户选择
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
localStorage.setItem('preferred-language', newLocale)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 替换语言部分的路径
|
||||||
const segments = pathname.split('/')
|
const segments = pathname.split('/')
|
||||||
segments[1] = newLocale
|
segments[1] = newLocale
|
||||||
const newPath = segments.join('/')
|
const newPath = segments.join('/')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import { useRouter } from 'next/navigation'
|
||||||
|
import i18nConfig from '@/i18nConfig'
|
||||||
|
|
||||||
|
export default function HomeRedirector() {
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const preferred = localStorage.getItem('preferred-language')
|
||||||
|
const currentPath = window.location.pathname
|
||||||
|
|
||||||
|
if (
|
||||||
|
preferred &&
|
||||||
|
i18nConfig.locales.includes(preferred) &&
|
||||||
|
!currentPath.startsWith(`/${preferred}/`)
|
||||||
|
) {
|
||||||
|
const newPath = `/${preferred}${currentPath}`
|
||||||
|
router.replace(newPath)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue