Fumadocs

Next.js

Support i18n routing on your Next.js + Fumadocs app

New to Next.js?

Setup

Define the i18n configurations in a file, we will import it with @/lib/i18n in this guide.

lib/i18n.ts
import { defineI18n } from 'fumadocs-core/i18n';

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

See available options for i18n config.

Middleware

Create a middleware that redirects users to appropriate locale.

middleware.ts
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { i18n } from '@/lib/i18n';

export default createI18nMiddleware(i18n);

export const config = {
  // Matcher ignoring `/_next/` and `/api/`
  // You may need to adjust it to ignore static assets in `/public` folder
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};

Using your own middleware?

The default middleware is optional, you can also use your own middleware or the one provided by i18n libraries.

Make sure its behaviour aligns with the hidePrefix option you set in your i18n config.

Routing

Create a /app/[lang] folder, and move your pages/layouts into it, except route handlers.

api/search/route.ts
layout.tsx
(home)/page.tsx
...

Common Mistake

Did you accidentally find your styles lost? Make sure the import path to global.css is still correct!

Provide UI translations and other config to <RootProvider />, the English translations are used when translations is not specified.

app/[lang]/layout.tsx
import { RootProvider } from 'fumadocs-ui/provider';
import { defineI18nUI } from 'fumadocs-ui/i18n';
import { i18n } from '@/lib/i18n';

const { provider } = defineI18nUI(i18n, {
  translations: {
    en: {
      displayName: 'English',
    },
    cn: {
      displayName: 'Chinese',
      search: '搜尋文檔',
    },
  },
});

export default async function RootLayout({
  params,
  children,
}: {
  params: Promise<{ lang: string }>;
  children: React.ReactNode;
}) {
  const lang = (await params).lang;

  return (
    <html lang={lang}>
      <body>
        <RootProvider
          i18n={provider(lang)}
        >
          {children}
        </RootProvider>
      </body>
    </html>
  );
}

Pass Locale

Add locale parameter to baseOptions() and add i18n into it:

lib/layout.shared.tsx
import { i18n } from '@/lib/i18n';
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';

export function baseOptions(locale: string): BaseLayoutProps {
  return {
    i18n,
    // different props based on `locale`
  };
}

Pass the locale to Fumadocs in your pages and layouts.

import { i18n } from '@/lib/i18n';
import { loader } from 'fumadocs-core/source';

export const source = loader({
  i18n,
  // other options
});

Using another name for lang dynamic segment?

If you're using another name like app/[locale], you also need to update generateStaticParams() in docs page:

export function generateStaticParams() {
  return source.generateParams();
  return source.generateParams('slug', 'locale'); // new param name
}

Configure i18n on your search solution.

  • Built-in Search (Orama): See Internationalization.
  • Cloud Solutions (e.g. Algolia): They usually have official support for multilingual.

Writing Documents

See i18n routing to learn how to create pages for specific locales.

Fumadocs only handles navigation for its own layouts (e.g. sidebar). For other places, you can use the useParams hook to get the locale from url, and attend it to href.

import Link from 'next/link';
import { useParams } from 'next/navigation';

const { lang } = useParams();

return <Link href={`/${lang}/another-page`}>This is a link</Link>;

In addition, the fumadocs-core/dynamic-link component supports dynamic hrefs, you can use it to attend the locale prefix. It is useful for Markdown/MDX content.

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

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

How is this guide?

Last updated on