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

Support multiple languages in your React Router docs.

<Callout title="Fumadocs CLI">
`npx @fumadocs/cli init --i18n` sets up a new docs with i18n, or configure it manually below.
</Callout>

## Overview [#overview]

Configure i18n routing on both React Router and Fumadocs.

Fumadocs handles i18n for the docs content and sidebar. Internationalize the rest of your app with your own i18n setup.

## Setup [#setup]

Define the i18n config in a file. This guide imports it as `@/lib/i18n`.

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

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

> See [available options](/docs/headless/internationalization/config) for i18n config.

### Routing [#routing]

Add a `:lang` prefix to your pages:

```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;
```

To serve the default language without a prefix (e.g. `/docs`), make the parameter optional and enable `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 [#translations]

Define your translations in `lib/layout.shared.tsx`. See [Translations](/docs/ui/translations) for more details, including the official language packs.

You can also add a `locale` parameter to `baseOptions()` for localized layout options:

```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 {
    // different props based on `locale`
  };
}
```

Pass translations to `<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 [#pass-locale]

Pass the locale to Fumadocs in your pages and layouts.

```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="Home Page" 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="Docs Page" 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 [#search]

Configure i18n on your search solution.

- **Built-in Search:** See [Internationalization](/docs/headless/search/orama#internationalization).
- **Cloud Solutions (e.g. Algolia):** They usually have official support for multilingual.

## Writing Documents [#writing-documents]

See [i18n routing](/docs/react-router/page-conventions#i18n-routing) to learn how to create pages for specific locales.

### Navigation [#navigation]

Fumadocs only handles navigation for its own layouts (e.g. sidebar).

For other places, use the `useParams` hook to get the locale from the URL.

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

const { lang } = useParams();

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

The [`fumadocs-core/dynamic-link`](/docs/headless/components/link#dynamic-hrefs) component supports dynamic hrefs, so you can use it to prepend the locale prefix in Markdown/MDX content.

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

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