i18n - 教程
本教程将带您了解 Docusaurus i18n 系统的基础知识。
我们将为一个新初始化的英文 Docusaurus 网站添加法语翻译。
使用 npx create-docusaurus@latest website classic
初始化一个新网站(如此示例)。
配置您的网站
修改 docusaurus.config.js
以添加对法语的 i18n 支持。
网站配置
使用网站 i18n 配置来声明 i18n 语言环境。
export default {
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr', 'fa'],
localeConfigs: {
en: {
htmlLang: 'en-GB',
},
// You can omit a locale (e.g. fr) if you don't need to override the defaults
fa: {
direction: 'rtl',
},
},
},
};
语言环境名称用于翻译文件的位置,以及您已翻译语言环境的基本 URL。构建所有语言环境时,只有默认语言环境的基本 URL 中会省略其名称。
Docusaurus 使用语言环境名称提供合理的默认值:例如 <html lang="...">
属性、语言环境标签、日历格式等。您可以使用 localeConfigs
来自定义这些默认值。
主题配置
添加一个类型为 localeDropdown
的导航栏项,以便用户可以选择他们想要的语言环境。
export default {
themeConfig: {
navbar: {
items: [
{
type: 'localeDropdown',
position: 'left',
},
],
},
},
};
您可以通过一个查询参数,当用户使用下拉菜单更改语言环境时,该参数会附加到 URL(例如 queryString: '?persistLocale=true'
)。
这对于在您的服务器上实现自动语言环境检测很有用。例如,您可以使用此参数将用户首选的语言环境存储在 cookie 中。
启动您的网站
在开发模式下启动您的本地化网站,使用您选择的语言环境
- npm
- Yarn
- pnpm
- Bun
npm run start -- --locale fr
yarn run start --locale fr
pnpm run start --locale fr
bun run start --locale fr
您的网站可在 https://:3000/
访问。
我们尚未提供任何翻译,因此网站大部分内容未被翻译。
Docusaurus 为通用主题标签提供默认翻译,例如用于分页的“Next”和“Previous”。
请帮助我们完成这些默认翻译。
每个语言环境都是一个独立的单页应用程序:无法同时启动所有语言环境的 Docusaurus 网站。
翻译您的网站
法语语言环境的所有翻译数据都存储在 website/i18n/fr
中。每个插件在相应的文件夹下获取其翻译内容,而 code.json
文件定义了 React 代码中使用的所有文本标签。
复制文件后,使用 npm run start -- --locale fr
重新启动您的网站。编辑现有文件时,热重载效果会更好。
翻译您的 React 代码
对于您自己编写的任何 React 代码:React 页面、React 组件等,您将使用翻译 API。
在您的 React 代码中找到所有将对用户可见的文本标签,并使用翻译 API 对其进行标记。有两种类型的 API:
<Translate>
组件将字符串包装为 JSX 元素;translate()
回调接收一条消息并返回一个字符串。
根据语义更好地适应上下文的 API。例如,<Translate>
可以用作 React 子组件,而对于需要字符串的 props,则可以使用回调。
- 之前
- 之后
import React from 'react';
import Layout from '@theme/Layout';
import Link from '@docusaurus/Link';
export default function Home() {
return (
<Layout>
<h1>Welcome to my website</h1>
<main>
You can also visit my
<Link to="https://docusaurus.org.cn/blog">blog</Link>
<img
src="/img/home.png"
alt="Home icon"
/>
</main>
</Layout>
);
}
import React from 'react';
import Layout from '@theme/Layout';
import Link from '@docusaurus/Link';
import Translate, {translate} from '@docusaurus/Translate';
export default function Home() {
return (
<Layout>
<h1>
<Translate>Welcome to my website</Translate>
</h1>
<main>
<Translate
id="homepage.visitMyBlog"
description="The homepage message to ask the user to visit my blog"
values={{
blogLink: (
<Link to="https://docusaurus.org.cn/blog">
<Translate
id="homepage.visitMyBlog.linkLabel"
description="The label for the link to my blog">
blog
</Translate>
</Link>
),
}}>
{'You can also visit my {blogLink}'}
</Translate>
<img
src="/img/home.png"
alt={
translate({
message: 'Home icon',
description: 'The homepage icon alt message',
})
}
/>
</main>
</Layout>
);
}
Docusaurus 特意提供了一个非常小巧轻量级的翻译运行时,并且只支持基本的占位符插值,使用 ICU 消息格式的一个子集。
大多数文档网站通常是静态的,不需要高级 i18n 功能(复数、性别等)。对于更高级的用例,请使用像 react-intl 这样的库。
docusaurus write-translations
命令将静态分析您网站中使用的所有 React 代码文件,提取对这些 API 的调用,并将它们聚合到 code.json
文件中。翻译文件将存储为从 ID 到翻译消息对象(包括翻译后的标签和标签描述)的映射。在您对翻译 API(<Translate>
或 translate()
)的调用中,您需要指定默认的未翻译消息或 ID,以便 Docusaurus 能够正确地将每个翻译条目与 API 调用关联起来。
docusaurus write-translations
命令只对您的代码进行静态分析。它不会实际运行您的网站。因此,动态消息无法被提取,因为消息是表达式,而不是字符串。
const items = [
{id: 1, title: 'Hello'},
{id: 2, title: 'World'},
];
function ItemsList() {
return (
<ul>
{/* DON'T DO THIS: doesn't work with the write-translations command */}
{items.map((item) => (
<li key={item.id}>
<Translate>{item.title}</Translate>
</li>
))}
<ul>
);
}
这在运行时仍然表现正常。然而,将来我们可能会提供一种“无运行时”机制,允许通过 Babel 转换将翻译直接内联到 React 代码中,而不是在运行时调用 API。因此,为了面向未来,您应该始终优先使用可静态分析的消息。例如,我们可以将上述代码重构为:
const items = [
{id: 1, title: <Translate>Hello</Translate>},
{id: 2, title: <Translate>World</Translate>},
];
function ItemsList() {
return (
<ul>
{/* The titles are now already translated when rendering! */}
{items.map((item) => (
<li key={item.id}>{item.title}</li>
))}
<ul>
);
}
您可以将对翻译 API 的调用纯粹视为标记,它们告诉 Docusaurus“这里有一个文本标签需要替换为翻译后的消息”。
复数形式
当您运行 write-translations
时,您会注意到某些标签已复数化。
{
// ...
"theme.blog.post.plurals": "One post|{count} posts"
// ...
}
每种语言都将有一系列可能的复数类别。Docusaurus 将按照 ["zero", "one", "two", "few", "many", "other"]
的顺序排列它们。例如,因为英语 (en
) 有两种复数形式(“one”和“other”),所以翻译消息有两个标签,由竖线 (|
) 分隔。对于波兰语 (pl
),它有三种复数形式(“one”、“few”和“many”),您需要按该顺序提供三个标签,并用竖线连接。
您也可以将自己代码中的消息复数化。
import {translate} from '@docusaurus/Translate';
import {usePluralForm} from '@docusaurus/theme-common';
function ItemsList({items}) {
// `usePluralForm` will provide the plural selector for the current locale
const {selectMessage} = usePluralForm();
// Select the appropriate pluralized label based on `items.length`
const message = selectMessage(
items.length,
translate(
{message: 'One item|{count} items'},
{count: items.length},
),
);
return (
<>
<h2>{message}</h2>
<ul>{items.map((item) => <li key={item.id}>{item.title}</li>)}<ul>
</>
);
}
Docusaurus 使用 Intl.PluralRules
来解析和选择复数形式。为使 selectMessage
工作,提供正确数量和顺序的复数形式非常重要。
翻译插件数据
JSON 翻译文件用于您代码中散布的所有内容:
- React 代码,包括您上面标记的已翻译标签
- 主题配置中的导航栏和页脚标签
sidebars.js
中的文档侧边栏类别标签- 插件选项中的博客侧边栏标题
- ...
运行 write-translations 命令
- npm
- Yarn
- pnpm
- Bun
npm run write-translations -- --locale fr
yarn write-translations --locale fr
pnpm run write-translations --locale fr
bun run write-translations --locale fr
它将提取并初始化您需要翻译的 JSON 翻译文件。根目录下的 code.json
文件包含从源代码中提取的所有翻译 API 调用,这些调用可以由您编写,也可以由主题提供,其中一些可能已经默认翻译。
{
// No ID for the <Translate> component: the default message is used as ID
"Welcome to my website": {
"message": "Welcome to my website"
},
"home.visitMyBlog": {
"message": "You can also visit my {blog}",
"description": "The homepage message to ask the user to visit my blog"
},
"homepage.visitMyBlog.linkLabel": {
"message": "Blog",
"description": "The label for the link to my blog"
},
"Home icon": {
"message": "Home icon",
"description": "The homepage icon alt message"
}
}
插件和主题也将写入自己的 JSON 翻译文件,例如:
{
"title": {
"message": "My Site",
"description": "The title in the navbar"
},
"item.label.Docs": {
"message": "Docs",
"description": "Navbar item with label Docs"
},
"item.label.Blog": {
"message": "Blog",
"description": "Navbar item with label Blog"
},
"item.label.GitHub": {
"message": "GitHub",
"description": "Navbar item with label GitHub"
}
}
翻译 i18n/fr
目录下 JSON 文件中的 message
属性,您的网站布局和主页现在应该已被翻译。
翻译 Markdown 文件
官方 Docusaurus 内容插件广泛使用 Markdown/MDX 文件,并允许您翻译它们。
翻译文档
将您的文档 Markdown 文件从 docs/
复制到 i18n/docusaurus-plugin-content-docs/current
,并进行翻译。
mkdir -p i18n/docusaurus-plugin-content-docs/current
cp -r docs/** i18n/docusaurus-plugin-content-docs/current
请注意,docusaurus-plugin-content-docs
插件总是按版本划分其内容。./docs
文件夹中的数据将在 current
子文件夹和 current.json
文件中翻译。有关“current”含义的更多信息,请参阅文档版本控制指南。
翻译博客
将您的博客 Markdown 文件复制到 i18n/docusaurus-plugin-content-blog
,并进行翻译。
mkdir -p i18n/docusaurus-plugin-content-blog
cp -r blog/** i18n/docusaurus-plugin-content-blog
翻译页面
将您的页面 Markdown 文件复制到 i18n/docusaurus-plugin-content-pages
,并进行翻译。
mkdir -p i18n/docusaurus-plugin-content-pages
cp -r src/pages/**.md i18n/docusaurus-plugin-content-pages
cp -r src/pages/**.mdx i18n/docusaurus-plugin-content-pages
我们只复制 .md
和 .mdx
文件,因为 React 页面已经通过 JSON 翻译文件进行翻译。
默认情况下,Markdown 标题 ### Hello World
将生成一个 ID hello-world
。其他文档可以使用 [link](#hello-world)
链接它。然而,翻译后,标题变为 ### Bonjour le Monde
,ID 为 bonjour-le-monde
。
生成的 ID 并非总是适用于本地化网站,因为它要求您本地化所有锚点链接。
- [link](#hello-world).
+ [link](#bonjour-le-monde)
对于本地化网站,建议使用显式标题 ID。
部署您的网站
您可以选择将您的网站部署在单个域名下,或使用多个(子)域名。
单域名部署
运行以下命令:
- npm
- Yarn
- pnpm
- Bun
npm run build
yarn build
pnpm run build
bun run build
Docusaurus 将为每个语言环境构建一个单页应用程序:
website/build
:用于默认的英语website/build/fr
:用于法语
您现在可以将 build
文件夹部署到您选择的静态托管解决方案。
Docusaurus 网站采用此策略:
静态托管服务提供商通常根据惯例将 /unknown/url
重定向到 /404.html
,总是显示一个英文 404 页面。
通过配置您的主机将 /*
重定向到 /404.html
来本地化您的 404 页面。
这并非总是可能,取决于您的主机:GitHub Pages 无法做到这一点,但 Netlify 可以。
多域名部署
您还可以为单个语言环境构建您的网站:
- npm
- Yarn
- pnpm
- Bun
npm run build -- --locale fr
yarn build --locale fr
pnpm run build --locale fr
bun run build --locale fr
Docusaurus 将不会添加 /
URL 前缀。
在您的静态托管服务提供商上:
- 为每个语言环境创建一个部署
- 使用
--locale
选项配置相应的构建命令 - 为每个部署配置您选择的(子)域名
此策略在 GitHub Pages 上不可行,因为它只允许单个部署。
混合
有些语言环境可以使用子路径,而另一些则可以使用子域名。
也可以将每个语言环境作为单独的子域名部署,然后在 CDN 层面将这些子域名组合成一个统一的域名:
- 将您的网站部署为
fr.docusaurus.io
- 配置 CDN 以从
docusaurus.io/fr
提供服务
管理翻译
Docusaurus 不关心您如何管理翻译:它只需要在构建期间文件系统中提供所有翻译文件(JSON、Markdown 或其他数据文件)。然而,作为网站创建者,您需要考虑如何管理翻译,以便您的翻译贡献者能够良好协作。
我们将分享两种常见的翻译协作策略:使用 Git 和 使用 Crowdin。