document/LOCALIZED_NAVIGATION.md
路由跳转时语言前缀丢失,导致用户在切换页面后回到默认语言。
lib/i18n.ts: 设置 hideLocale: 'never',确保所有语言(包括默认语言)都显示语言前缀lib/localized-navigation.ts: 提供客户端路由工具,自动处理语言前缀app/[lang]/docs/layout.tsxgetLocalizedPath() 为每个 tab 的 URL 添加语言前缀lib/source.tsapp/[lang]/(home)/page.tsx - 首页重定向app/[lang]/(home)/[...not-found]/page.tsx - 404 重定向getLocalizedPath() 添加语言前缀components/docs/Redirect.tsxuseLocalizedRouter() hookcomponents/docs/not-found.tsxuseCurrentLang() 获取当前语言useLocalizedPath() 为重定向目标添加语言前缀components/docs/LocalizedLink.tsx - 自定义 Link 组件mdx-components.tsx - 配置 MDX 使用 LocalizedLink<a> 标签,自动为内部链接添加语言前缀app/layout.config.tsxi18n: {
locale,
languages: [
{ name: '简体中文', locale: 'zh-CN' },
{ name: 'English', locale: 'en' }
],
hideLocale: 'never'
}
'use client';
import { useLocalizedRouter } from '@/lib/localized-navigation';
function MyComponent() {
const router = useLocalizedRouter();
router.push('/docs/introduction'); // 自动添加语言前缀
}
import { getLocalizedPath } from '@/lib/i18n';
import { redirect } from 'next/navigation';
export default async function Page({ params }: { params: Promise<{ lang: string }> }) {
const { lang } = await params;
redirect(getLocalizedPath('/docs/intro', lang));
}
<!-- 内部链接会自动添加语言前缀 -->
[查看介绍](/docs/introduction)
<!-- 或使用 Redirect 组件 -->
<Redirect to="/docs/faq" />