国際化
Wakuのドキュメントを多言語に対応させます。
Fumadocs は、ドキュメントとその UI を多言語で提供します。アプリのその他の部分では、任意の i18n ライブラリを使用できます。
アプリで Fumadocs の i18n をセットアップするには、次のコマンドを実行してください。
npx @fumadocs/cli init --i18n既存の Fumadocs プロジェクトに i18n を追加するには、以下の手順に従ってください。
セットアップ
言語を定義する
import { defineI18n } from 'fumadocs-core/i18n';
export const i18n = defineI18n({
defaultLanguage: 'en',
languages: ['en', 'cn'],
});Waku はオプショナルなルートセグメントをサポートしていないため、すべての URL に /en/docs のようなロケールのプレフィックスが付きます。
フォールバック言語など、その他のオプションについては設定を参照してください。
UI を翻訳する
UI の翻訳はレイアウトオプションと同じ場所で定義し、baseOptions() でロケールを受け取るようにします。
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({
cn: {
// 言語スイッチに表示される名前
displayName: '中文',
'Search(search trigger)': '搜尋文檔',
},
});
export function baseOptions(locale: string): BaseLayoutProps {
return {
nav: {
title: 'My App',
url: `/${locale}`,
},
};
}言語パックを使えば、UI 全体を一括で翻訳できます。
翻訳を <RootProvider /> に渡します。
'use client';
import type { ReactNode } from 'react';
import { useRouter } from 'waku/router/client';
import { RootProvider } from 'fumadocs-ui/provider/waku';
import { i18nProvider } from 'fumadocs-ui/i18n';
import { i18n } from '@/lib/i18n';
import { translations } from '@/lib/layout.shared';
export function Provider({ children }: { children: ReactNode }) {
// パスの最初のセグメント
const lang = useRouter().path.split('/')[1] || i18n.defaultLanguage;
return <RootProvider i18n={i18nProvider(translations, lang)}>{children}</RootProvider>;
}ロケールを渡す
loader() に i18n の設定を追加します。
import { i18n } from '@/lib/i18n';
export const source = loader({
i18n,
// ...
});次に、ロケールをページに渡します。Waku のレイアウトはロケールを受け取らないため、<HomeLayout /> と <DocsLayout /> はページ側でレンダリングしてください。
export default function Home({ lang }: PageProps<'/[lang]'>) {
return <HomeLayout {...baseOptions(lang)}>{/* ... */}</HomeLayout>;
}
export async function getConfig() {
return {
render: 'static',
staticPaths: i18n.languages,
} as const;
}ページの翻訳
ページまたは meta ファイルを翻訳するには、ファイル名にロケールを付けます。
翻訳がないページは、デフォルト言語にフォールバックします。代わりに言語ごとのフォルダーにファイルをまとめる場合は、i18n ルーティングを参照してください。
リンク
Fumadocs のレイアウトは、リンクにロケールを自動的に付加します。
独自のコンポーネントでは、パスからロケールを取得します。
'use client';
import { Link, useRouter } from 'waku/router/client';
export function AboutLink() {
const lang = useRouter().path.split('/')[1];
return <Link to={`/${lang}/about`}>About</Link>;
}検索
組み込みの検索は、追加の設定なしですべての言語をインデックス化します。また、デフォルトの検索ダイアログでは現在のロケールが検索対象になります。
How is this guide?
最終更新日
