# Fumadocs (Framework Mode, React Router): 国際化
URL: /ja/docs/react-router/internationalization
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/internationalization.ja.mdx

React Routerのドキュメントを多言語に対応させます。

<Callout title="Fumadocs CLI">
`npx @fumadocs/cli init --i18n` を実行すると、i18n に対応した新しいドキュメントをセットアップできます。手動で設定する場合は、以下の手順に従ってください。
</Callout>

## 概要 [#overview]

React Router と Fumadocs の両方で i18n ルーティングを設定します。

Fumadocs が担当するのは、ドキュメントのコンテンツとサイドバーの i18n です。アプリのその他の部分は、独自の i18n 設定で国際化してください。

## セットアップ [#setup]

i18n の設定をファイルに定義します。このガイドでは、`@/lib/i18n` としてインポートします。

```ts title="app/lib/i18n.ts"
import { defineI18n } from 'fumadocs-core/i18n';

export const i18n = defineI18n({
  defaultLanguage: 'en',
  languages: ['cn', 'en'],
});
```

> i18n の設定で使用できるオプションについては、[こちら](/docs/headless/internationalization/config)を参照してください。

### ルーティング [#routing]

ページに `:lang` プレフィックスを追加します。

```ts title="app/routes.ts"
import { index, route, type RouteConfig } from '@react-router/dev/routes'; // [!code --]
import { route, type RouteConfig } from '@react-router/dev/routes'; // [!code ++]

export default [
  index('routes/home.tsx'), // [!code --]
  route('docs/*', 'routes/docs.tsx'), // [!code --]
  // [!code ++:2]
  route(':lang', 'routes/home.tsx'),
  route(':lang/docs/*', 'routes/docs.tsx'),
  route('api/search', 'routes/search.ts'),
] satisfies RouteConfig;
```

デフォルト言語をプレフィックスなし (例: `/docs`) で配信するには、パラメーターを省略可能にし、`hideLocale` を有効にします。

```ts tab="app/routes.ts"
export default [
  // [!code highlight:2]
  route(':lang?', 'routes/home.tsx'),
  route(':lang?/docs/*', 'routes/docs.tsx'),
  route('api/search', 'routes/search.ts'),
] satisfies RouteConfig;
```

```ts tab="app/lib/i18n.ts"
export const i18n = defineI18n({
  defaultLanguage: 'en',
  languages: ['cn', 'en'],
  hideLocale: 'default-locale', // [!code ++]
});
```

### 翻訳 [#translations]

翻訳は `lib/layout.shared.tsx` で定義します。公式の言語パックなど、詳しくは[翻訳](/docs/ui/translations)を参照してください。

また、`baseOptions()` に `locale` パラメーターを追加すると、ロケールごとにレイアウトオプションを切り替えられます。

```tsx title="app/lib/layout.shared.tsx"
// [!code ++:2]
import { i18n } from '@/lib/i18n';
import { uiTranslations } from 'fumadocs-ui/i18n';
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';

// [!code ++:11]
export const translations = i18n
  .translations()
  .extend(uiTranslations())
  .add({
    en: {
      displayName: 'English',
    },
    cn: {
      displayName: 'Chinese',
    },
  });

export function baseOptions(): BaseLayoutProps { // [!code --]
export function baseOptions(locale: string = i18n.defaultLanguage): BaseLayoutProps { // [!code ++]
  return {
    // `locale` に応じて異なる props を指定
  };
}
```

翻訳を `<RootProvider />` に渡します。

```tsx title="app/root.tsx"
import {
  isRouteErrorResponse,
  Links,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
  useParams, // [!code ++]
} from 'react-router';
import { RootProvider } from 'fumadocs-ui/provider/react-router';
// [!code ++:3]
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(); // [!code ++]

  return (
    // [!code highlight]
    <html lang={lang} suppressHydrationWarning>
      <head>{/* ... */}</head>
      <body className="flex flex-col min-h-screen">
        <RootProvider
          // [!code ++]
          i18n={i18nProvider(translations, lang)}
        >
          {children}
        </RootProvider>
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}
```

### ロケールを渡す [#pass-locale]

ページとレイアウトで、ロケールを Fumadocs に渡します。

```ts tab="app/lib/source.ts"
import { i18n } from '@/lib/i18n'; // [!code ++]

export const source = loader({
  source: docs.toFumadocsSource(),
  baseUrl: '/docs',
  i18n, // [!code ++]
});
```

```tsx tab="ホームページ" title="app/routes/home.tsx"
export default function Home() { // [!code --]
export default function Home({ params }: Route.ComponentProps) { // [!code ++]
  return (
    // [!code highlight]
    <HomeLayout {...baseOptions(params.lang)}>
      {/* ... */}
    </HomeLayout>
  );
}
```

```tsx tab="ドキュメントページ" title="app/routes/docs.tsx"
export async function loader({ params }: Route.LoaderArgs) {
  const slugs = params['*'].split('/').filter((v) => v.length > 0);
  const page = source.getPage(slugs); // [!code --]
  const page = source.getPage(slugs, params.lang); // [!code ++]
  if (!page) throw new Response('Not found', { status: 404 });

  return {
    path: page.path,
    pageTree: await source.serializePageTree(source.getPageTree()), // [!code --]
    pageTree: await source.serializePageTree(source.getPageTree(params.lang)), // [!code ++]
  };
}

export default function Page({ loaderData }: Route.ComponentProps) { // [!code --]
export default function Page({ loaderData, params }: Route.ComponentProps) { // [!code ++]
  const { path, pageTree } = useFumadocsLoader(loaderData);

  return (
    // [!code highlight]
    <DocsLayout {...baseOptions(params.lang)} tree={pageTree}>
      <Content path={path} />
    </DocsLayout>
  );
}
```

### 検索 [#search]

使用している検索ソリューションで i18n を設定してください。

* **組み込み検索:** [国際化](/docs/headless/search/orama#internationalization)を参照してください。
* **クラウドソリューション(Algolia など):** 多くの場合、多言語対応が公式にサポートされています。

## ドキュメントの作成 [#writing-documents]

特定のロケール向けのページを作成する方法については、[i18n ルーティング](/docs/react-router/page-conventions#i18n-routing)を参照してください。

### ナビゲーション [#navigation]

Fumadocs がナビゲーションを処理するのは、Fumadocs 自身のレイアウト(サイドバーなど)のみです。

それ以外の場所では、`useParams` フックで URL からロケールを取得してください。

```tsx
import { Link, useParams } from 'react-router';

const { lang } = useParams();

<Link to={`/${lang}/about`}>About Us</Link>;
```

[`fumadocs-core/dynamic-link`](/docs/headless/components/link#dynamic-hrefs) コンポーネントは動的な href に対応しているため、Markdown/MDX コンテンツ内でロケールのプレフィックスを付加する際に利用できます。

```mdx title="content.mdx"
import { DynamicLink } from 'fumadocs-core/dynamic-link';

<DynamicLink href="/[lang]/another-page">This is a link</DynamicLink>
```