# Fumadocs (Framework Mode, Astro): Internationalization
URL: /docs/astro/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 Astro docs.

Fumadocs serves your docs and its UI in multiple languages. The rest of your app can use any i18n library.

To set up Fumadocs with i18n on an app, run:

```package-install
npx @fumadocs/cli init --i18n
```

To add i18n to an existing Fumadocs setup, follow the steps below.

## Setup [#setup]

### Define Languages [step] [#define-languages]

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

export const i18n = defineI18n({
  defaultLanguage: 'en',
  languages: ['en', 'cn'],
  // serve the default language without a prefix, like `/docs` instead of `/en/docs`
  hideLocale: 'default-locale',
});
```

See [Configuration](/docs/headless/internationalization/config) for other options, like the fallback language.

### Add a Locale Segment [step] [#add-a-locale-segment]

Move your pages into a `[...lang]` segment. The rest parameter is empty for the default language:

<Files>
<Folder name="src/pages" defaultOpen>
<File name="api/search.ts" />

<Folder name="[...lang]" defaultOpen>
<File name="index.astro" />

<File name="docs/[...slug].astro" />
</Folder>
</Folder>
</Files>

### Translate the UI [step] [#translations]

Define the UI translations next to your layout options, and take the locale in `baseOptions()`:

```tsx title="lib/layout.shared.tsx"
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: {
      // the name in the language switch
      displayName: '中文',
      'Search(search trigger)': '搜尋文檔',
    },
  });

export function baseOptions(locale: string = i18n.defaultLanguage): BaseLayoutProps {
  return {
    nav: {
      title: 'My App',
      url: locale === i18n.defaultLanguage ? '/' : `/${locale}`,
    },
  };
}
```

A [language pack](/docs/ui/translations#language-packs) translates the whole UI at once.

Pass the translations to `<RootProvider />`:

The MDX content is a React island of its own, so it needs a provider too. The `wrapper` of your MDX components receives the props of `<Content />`:

```tsx tab="src/components/provider.tsx"
import { navigate } from 'astro:transitions/client';
import type { ReactNode } from 'react';
import { RootProvider } from 'fumadocs-ui/provider/astro';
import { i18nProvider } from 'fumadocs-ui/i18n';
import { translations } from '@/lib/layout.shared';
import SearchDialog from '@/components/search';

export interface ProviderProps {
  lang: string; // [!code ++]
  pathname: string;
  params: Record<string, string | undefined>;
  children?: ReactNode;
}

export function Provider({ lang, pathname, params, children }: ProviderProps) {
  return (
    <RootProvider
      pathname={pathname}
      params={params}
      navigate={navigate}
      i18n={i18nProvider(translations, lang)} // [!code ++]
      search={{ SearchDialog }}
    >
      {children}
    </RootProvider>
  );
}

// [!code ++:14]
export function ContentProvider({ lang, pathname, params, children }: ProviderProps) {
  return (
    <RootProvider
      pathname={pathname}
      params={params}
      navigate={navigate}
      i18n={i18nProvider(translations, lang)}
      theme={{ enabled: false }}
      search={{ enabled: false }}
    >
      {children}
    </RootProvider>
  );
}
```

```tsx tab="src/components/mdx.tsx"
import { ContentProvider, type ProviderProps } from '@/components/provider';

export default getMDXComponents({
  wrapper: DocsBody, // [!code --]
  // [!code ++:5]
  wrapper: ({ children, ...props }: ProviderProps) => (
    <ContentProvider {...props}>
      <DocsBody>{children}</DocsBody>
    </ContentProvider>
  ),
});
```

### Pass the Locale [step] [#pass-the-locale]

Add your i18n config to `loader()`:

```ts title="lib/source.ts"
import { i18n } from '@/lib/i18n';

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

Then pass the locale of the route to your layouts and pages:

```astro tab="Docs Page" title="src/pages/[...lang]/docs/[...slug].astro"
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { Docs } from '@/components/docs';
import { i18n } from '@/lib/i18n';
import { source } from '@/lib/source';

export const prerender = true;

export const getStaticPaths = (() => {
  const paths = [];
  for (const { language, pages } of source.getLanguages()) {
    for (const page of pages) {
      paths.push({
        params: {
          // the default language has no prefix
          lang: language === i18n.defaultLanguage ? undefined : language,
          slug: decodeURI(page.slugs.join('/')) || undefined,
        },
        props: { lang: language, page },
      });
    }
  }
  return paths;
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;

const { lang, page } = Astro.props;
const { body: Content, toc } = await page.data.load();
const pageTree = await source.serializePageTree(source.getPageTree(lang));
const provider = { lang, pathname: Astro.url.pathname, params: Astro.params };
---

<Layout lang={lang} title={page.data.title} description={page.data.description}>
  <Docs
    client:load
    transition:persist="docs"
    {...provider}
    tree={pageTree}
    title={page.data.title}
    description={page.data.description}
    toc={toc}
  >
    <Content {...provider} />
  </Docs>
</Layout>
```

```astro tab="Home Page" title="src/pages/[...lang]/index.astro"
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { i18n } from '@/lib/i18n';

export const prerender = true;

export const getStaticPaths = (() => {
  return i18n.languages.map((lang) => ({
    params: { lang: lang === i18n.defaultLanguage ? undefined : lang },
    props: { lang },
  }));
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;

const { lang } = Astro.props;
---

<Layout lang={lang} title="My App">
  <!-- ... -->
</Layout>
```

```tsx tab="src/components/docs.tsx"
export function Docs({ tree, title, description, toc, children, ...props }: DocsProps) {
  const data = useFumadocsLoader({ tree });

  return (
    <Provider {...props}>
      <DocsLayout {...baseOptions(props.lang)} tree={data.tree}>
        {/* ... */}
      </DocsLayout>
    </Provider>
  );
}
```

```astro tab="src/layouts/Layout.astro"
---
interface Props {
  lang: string; // [!code ++]
  title: string;
  description?: string;
}

const { lang, title, description } = Astro.props;
---

<html lang={lang} transition:animate="none">
  <!-- ... -->
</html>
```

## Translated Pages [#translated-pages]

Add the locale to the file name of a page or meta file to translate it:

<Files>
<Folder name="content/docs" defaultOpen>
<File name="meta.json" />

<File name="meta.cn.json" />

<File name="get-started.mdx" />

<File name="get-started.cn.mdx" />
</Folder>
</Files>

A page without a translation falls back to the default language. To group files in a folder per language instead, see [i18n routing](/docs/astro/page-conventions#i18n-routing).

## Links [#links]

Fumadocs layouts add the locale to their links.

In your own components and MDX content, `DynamicLink` fills `[lang]` with the locale of the current route:

```tsx
import { DynamicLink } from 'fumadocs-core/dynamic-link';

// `/cn/docs` on a Chinese page
<DynamicLink href="/[lang]/docs">Docs</DynamicLink>;
```

To use it in MDX, add it to your MDX components:

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

export function getMDXComponents(components?: MDXComponents) {
  return {
    ...defaultMdxComponents,
    DynamicLink, // [!code ++]
    ...components,
  } satisfies MDXComponents;
}
```

In `.astro` files, read the locale from `Astro.params`:

```astro
---
// `undefined` for the default language
const { lang } = Astro.params;
---

<a href={lang ? `/${lang}/about` : '/about'}>About</a>
```

## Search [#search]

The [built-in search](/docs/headless/search/orama#internationalization) indexes every language with no extra config, and the default search dialog searches the current locale.