diff --git a/src/app/[locale]/layout.tsx b/src/app/[locale]/layout.tsx index 07b5a25..83673ca 100644 --- a/src/app/[locale]/layout.tsx +++ b/src/app/[locale]/layout.tsx @@ -1,6 +1,7 @@ import { notFound } from "next/navigation" import { setRequestLocale } from 'next-intl/server' import Link from "next/link" +import { LocaleSwitcher } from "@/components/locale/LocaleSwitcher" const locales = ['zh', 'en'] @@ -72,9 +73,7 @@ export default async function LocaleLayout({ {/* Right side */}
- + = { + zh: '中文', + en: 'EN' +} + +export function LocaleSwitcher({ currentLocale }: { currentLocale: string }) { + const pathname = usePathname() + + // 移除当前语言前缀,获取基础路径 + const getBasePathname = () => { + const segments = pathname.split('/').filter(Boolean) + if (segments[0] && locales.includes(segments[0] as any)) { + return '/' + segments.slice(1).join('/') + } + return pathname + } + + const basePathname = getBasePathname() + + const switchLocale = (newLocale: string) => { + // 切换到另一种语言 + return `/${newLocale}${basePathname}` + } + + const otherLocale = currentLocale === 'zh' ? 'en' : 'zh' + + return ( + + {localeNames[currentLocale]} / {localeNames[otherLocale]} + + ) +}