Internationalization
Support multiple languages in your documentation
Overview
For Next.js apps, you'll have to configure i18n routing on both Next.js and Fumadocs.
Fumadocs is not a full-powered i18n library, it's up to you when internationalizing the nest of your app. You can also use other libraries with Fumadocs like next-intl.
New to Next.js?
You can learn more about i18n in Next.js.
Setup
Define the i18n configurations in a file, we will import it with @/ilb/i18n
in this guide.
import type { I18nConfig } from 'fumadocs-core/i18n';
export const i18n: I18nConfig = {
defaultLanguage: 'en',
languages: ['en', 'cn'],
};
See available options for i18n config.
Pass it to the source loader.
import { i18n } from '@/lib/i18n';
import { loader } from 'fumadocs-core/source';
export const source = loader({
i18n,
// other options
});
Middleware
Create a middleware that redirects users to appropriate locale.
import { createI18nMiddleware } from 'fumadocs-core/i18n';
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.
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.
import { RootProvider } from 'fumadocs-ui/provider';
import type { Translations } from 'fumadocs-ui/i18n';
// translations
const cn: Partial<Translations> = {
search: 'Translated Content',
};
// available languages that will be displayed on UI
// make sure `locale` is consistent with your i18n config
const locales = [
{
name: 'English',
locale: 'en',
},
{
name: 'Chinese',
locale: 'cn',
},
];
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={{
locale: lang,
locales,
translations: { cn }[lang],
}}
>
{children}
</RootProvider>
</body>
</html>
);
}
Pass Locale
Pass the locale to Fumadocs in your pages and layouts.
import { i18n } from '@/lib/i18n';
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';
// Make `baseOptions` a function:
export function baseOptions(locale: string): BaseLayoutProps {
return {
i18n,
// different props based on `locale`
};
}
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
}
Search
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
You can add Markdown/meta files for different languages by attending .{locale}
to your file name, like page.cn.md
and meta.cn.json
.
Make sure to create a file for the default locale first, the locale code is optional (e.g. both get-started.mdx
and get-started.en.mdx
are accepted).
Navigation
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.
import { DynamicLink } from 'fumadocs-core/dynamic-link';
<DynamicLink href="/[lang]/another-page">This is a link</DynamicLink>
How is this guide?
Last updated on