Internationalization
Support multiple languages in your React Router 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:
npx @fumadocs/cli init --i18nTo add i18n to an existing Fumadocs setup, follow the steps below.
Setup
Define Languages
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 for other options, like the fallback language.
Add a Locale Segment
Add an optional :lang? segment to your pages:
import { route, type RouteConfig } from '@react-router/dev/routes';
export default [
route(':lang?', 'routes/home.tsx'),
route(':lang?/docs/*', 'routes/docs.tsx'),
route('api/search', 'routes/search.ts'),
] satisfies RouteConfig;Translate the UI
Define the UI translations next to your layout options, and take the locale in 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: {
// 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 translates the whole UI at once.
Pass the translations to <RootProvider />:
import { useParams } from 'react-router';
import { RootProvider } from 'fumadocs-ui/provider/react-router';
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();
return (
<html lang={lang} suppressHydrationWarning>
<head>{/* ... */}</head>
<body className="flex flex-col min-h-screen">
<RootProvider i18n={i18nProvider(translations, lang)}>{children}</RootProvider>
{/* ... */}
</body>
</html>
);
}Pass the Locale
Add your i18n config to loader():
import { i18n } from '@/lib/i18n';
export const source = loader({
i18n,
// ...
});Then pass the locale of the route to your layouts and pages:
export default function Home({ params }: Route.ComponentProps) {
return <HomeLayout {...baseOptions(params.lang)}>{/* ... */}</HomeLayout>;
}To prerender the docs pages, take their URLs from source.getPages() instead of globbing the content files, since index.cn.mdx is served at /cn/docs.
Translated Pages
Add the locale to the file name of a page or meta file to translate it:
A page without a translation falls back to the default language. To group files in a folder per language instead, see i18n routing.
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:
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:
import { DynamicLink } from 'fumadocs-core/dynamic-link';
export function getMDXComponents(components?: MDXComponents) {
return {
...defaultMdxComponents,
DynamicLink,
...components,
} satisfies MDXComponents;
}Search
The built-in search indexes every language with no extra config, and the default search dialog searches the current locale.
How is this guide?
Last updated on
