国际化
在你的文档中支持多种语言
定义配置
🌐 Define Config
Fumadocs 核心提供了必要的中间件和国际化支持选项。
🌐 Fumadocs core provides necessary middleware and options for i18n support.
你可以定义一个配置,在多个工具之间共享。
🌐 You can define a config to share between utilities.
import { defineI18n } from 'fumadocs-core/i18n';
export const i18n = defineI18n({
defaultLanguage: 'en',
languages: ['en', 'cn'],
});隐藏区域前缀
🌐 Hide Locale Prefix
要隐藏区域前缀(例如 /en/page -> /page),请使用 hideLocale 选项。
🌐 To hide the locale prefix (e.g. /en/page -> /page), use the hideLocale option.
import { defineI18n } from 'fumadocs-core/i18n';
export const i18n = defineI18n({
defaultLanguage: 'en',
languages: ['en', 'cn'],
hideLocale: 'default-locale',
});| 模式 | 描述 |
|---|---|
always | 总是隐藏前缀,从 Cookie 检测语言环境 |
default-locale | 仅隐藏默认语言环境 |
never | 从不隐藏前缀(默认) |
使用 always
在 always 模式下,语言环境被存储为 cookie(由中间件设置),这对于静态网站来说并不理想。
🌐 On always mode, locale is stored as a cookie (set by the middleware), which isn't optimal for static sites.
这可能会导致不希望出现的缓存问题,并且需要额外关注 SEO,以确保搜索引擎能够正确索引你的页面。
🌐 This may cause undesired cache problems, and need to pay extra attention on SEO to ensure search engines can index your pages correctly.
备选语言
🌐 Fallback Language
当页面缺少翻译时使用的备用语言,默认为你的 defaultLanguage。
🌐 The fallback language to use when translations are missing for a page, default to your defaultLanguage.
支持:
🌐 Supported:
- 在你的
languages数组中的一种语言。 - 当设置为
null时,将不会使用回退。
import { defineI18n } from 'fumadocs-core/i18n';
export const i18n = defineI18n({
languages: ['en', 'cn'],
defaultLanguage: 'en',
fallbackLanguage: 'cn',
});中间件
🌐 Middleware
将用户重定向到适当的地区,可以从 i18n.ts 自定义。
🌐 Redirects users to appropriate locale, it can be customised from i18n.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).*)'],
};当启用 hideLocale 时,它使用 NextResponse.rewrite 来隐藏语言前缀。
Last updated on
