國際化
讓您的 React Router 文件支援多種語言。
Fumadocs CLI
執行 npx @fumadocs/cli init --i18n 即可建立支援 i18n 的新文件網站,您也可以依照下方說明手動設定。
概覽
請在 React Router 與 Fumadocs 兩端設定 i18n 路由。
Fumadocs 會負責文件內容與側邊欄的 i18n。應用程式的其他部分,請透過您自己的 i18n 設定來進行國際化。
設定
請在一個檔案中定義 i18n 設定。本指南會以 @/lib/i18n 匯入該設定。
import { defineI18n } from 'fumadocs-core/i18n';
export const i18n = defineI18n({
defaultLanguage: 'en',
languages: ['cn', 'en'],
});i18n 設定的可用選項請參閱這裡。
路由
為您的頁面加上 :lang 前綴:
import { index, route, type RouteConfig } from '@react-router/dev/routes';
import { route, type RouteConfig } from '@react-router/dev/routes';
export default [
index('routes/home.tsx'),
route('docs/*', 'routes/docs.tsx'),
route(':lang', 'routes/home.tsx'),
route(':lang/docs/*', 'routes/docs.tsx'),
route('api/search', 'routes/search.ts'),
] satisfies RouteConfig;若要讓預設語言不帶前綴 (例如 /docs) ,請將參數設為選用,並啟用 hideLocale:
export default [
route(':lang?', 'routes/home.tsx'),
route(':lang?/docs/*', 'routes/docs.tsx'),
route('api/search', 'routes/search.ts'),
] satisfies RouteConfig;翻譯
請在 lib/layout.shared.tsx 中定義您的翻譯。詳細資訊 (包括官方語言包) 請參閱翻譯。
您也可以為 baseOptions() 加上 locale 參數,以提供依語系調整的版面配置選項:
import { i18n } from '@/lib/i18n';
import { uiTranslations } from 'fumadocs-ui/i18n';
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';
export const translations = i18n
.translations()
.extend(uiTranslations())
.add({
en: {
displayName: 'English',
},
cn: {
displayName: 'Chinese',
},
});
export function baseOptions(): BaseLayoutProps {
export function baseOptions(locale: string = i18n.defaultLanguage): BaseLayoutProps {
return {
// 根據 `locale` 提供不同的 props
};
}將翻譯傳入 <RootProvider />:
import {
isRouteErrorResponse,
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useParams,
} from 'react-router';
import { RootProvider } from 'fumadocs-ui/provider/react-router';
import { i18nProvider } from 'fumadocs-ui/i18n';
import { i18n } from '@/lib/i18n';
import { translations } from '@/lib/layout.shared';
export function Layout({ children }: { children: React.ReactNode }) {
const { lang = i18n.defaultLanguage } = useParams();
return (
<html lang={lang} suppressHydrationWarning>
<head>{/* ... */}</head>
<body className="flex flex-col min-h-screen">
<RootProvider
i18n={i18nProvider(translations, lang)}
>
{children}
</RootProvider>
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}傳入語系
請在頁面與版面配置中將語系傳給 Fumadocs。
import { i18n } from '@/lib/i18n';
export const source = loader({
source: docs.toFumadocsSource(),
baseUrl: '/docs',
i18n,
});搜尋
請在您使用的搜尋方案中設定 i18n。
- 內建搜尋: 請參閱國際化。
- 雲端方案 (例如 Algolia) : 這類方案通常有官方的多語系支援。
撰寫文件
請參閱 i18n 路由,了解如何為特定語系建立頁面。
導覽
Fumadocs 只會處理其自身版面配置 (例如側邊欄) 中的導覽。
至於其他地方,請使用 useParams hook 從 URL 取得語系。
import { Link, useParams } from 'react-router';
const { lang } = useParams();
<Link to={`/${lang}/about`}>About Us</Link>;fumadocs-core/dynamic-link 元件支援動態 href,因此您可以在 Markdown/MDX 內容中用它加上語系前綴。
import { DynamicLink } from 'fumadocs-core/dynamic-link';
<DynamicLink href="/[lang]/another-page">This is a link</DynamicLink>How is this guide?
最後更新於
