i18n - 使用 git
一种可能的翻译策略是使用 Git(或其他 VCS)对翻译文件进行版本控制。
利弊
这一方法有如下优势:
- 易于上手:只需要向 Git 提交
i18n文件夹即可 - 开发方便:Git、GitHub 及合并请求均为主流的开发者工具
- 免费(至少不需要额外费用,如果你已经使用 Git 的话)
- 工作量小:不需注册第三方工具
- 高反馈:贡献者乐于看到自己漂亮的贡献历史
但使用 Git 也存在一些劣势:
- 对非开发者不友好:他们并未掌握 Git 及合并请求
- 对专业译者不友好:他们习惯使用 SaaS 翻译软件与高级功能
- 难以维护:你必须保持已翻译和未翻译的文件同步
有一些大规模的技术项目(如 React、Vue.js、MDN、TypeScript、Nuxt.js 等等) 使用 Git 翻译。
Refer to the Docusaurus i18n RFC for our notes and links studying these systems.
初始化
This is a walk-through of using Git to translate a newly initialized English Docusaurus website into French, and assume you already followed the i18n tutorial.
准备 Docusaurus 站点
初始化新的 Docusaurus 站点:
npx create-docusaurus@latest website classic
添加简体中文版网站的配置:
export default {
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
},
themeConfig: {
navbar: {
items: [
// ...
{
type: 'localeDropdown',
position: 'left',
},
// ...
],
},
},
// ...
};
翻译首页:
import React from 'react';
import Translate from '@docusaurus/Translate';
import Layout from '@theme/Layout';
export default function Home() {
return (
<Layout>
<h1 style={{margin: 20}}>
<Translate description="The homepage main heading">
Welcome to my Docusaurus translated site!
</Translate>
</h1>
</Layout>
);
}
Initialize the i18n folder
Use the write-translations CLI command to initialize the JSON translation files for the French locale:
- npm
- Yarn
- pnpm
- Bun
npm run write-translations -- --locale zh-Hans
1 translations written at i18n/zh-Hans/code.json
11 translations written at i18n/zh-Hans/docusaurus-theme-classic/footer.json
4 translations written at i18n/zh-Hans/docusaurus-theme-classic/navbar.json
3 translations written at i18n/zh-Hans/docusaurus-plugin-content-docs/current.json
yarn write-translations --locale zh-Hans
1 translations written at i18n/zh-Hans/code.json
11 translations written at i18n/zh-Hans/docusaurus-theme-classic/footer.json
4 translations written at i18n/zh-Hans/docusaurus-theme-classic/navbar.json
3 translations written at i18n/zh-Hans/docusaurus-plugin-content-docs/current.json
pnpm run write-translations --locale zh-Hans
1 translations written at i18n/zh-Hans/code.json
11 translations written at i18n/zh-Hans/docusaurus-theme-classic/footer.json
4 translations written at i18n/zh-Hans/docusaurus-theme-classic/navbar.json
3 translations written at i18n/zh-Hans/docusaurus-plugin-content-docs/current.json
bun run write-translations --locale zh-Hans
1 translations written at i18n/zh-Hans/code.json
11 translations written at i18n/zh-Hans/docusaurus-theme-classic/footer.json
4 translations written at i18n/zh-Hans/docusaurus-theme-classic/navbar.json
3 translations written at i18n/zh-Hans/docusaurus-plugin-content-docs/current.json
Use the --messagePrefix '(fr) ' option to make the untranslated strings stand out.
Hello will appear as (fr) Hello and makes it clear a translation is missing.
把未翻译的 Markdown 文件复制到简体中文文件夹:
mkdir -p i18n/fr/docusaurus-plugin-content-docs/current
cp -r docs/. i18n/fr/docusaurus-plugin-content-docs/current
mkdir -p i18n/fr/docusaurus-plugin-content-blog
cp -r blog/. i18n/fr/docusaurus-plugin-content-blog
mkdir -p i18n/fr/docusaurus-plugin-content-pages
cp -r src/pages/. i18n/fr/docusaurus-plugin-content-pages
把所有文件添加到 Git。
翻译文件
Translate the Markdown and JSON files in i18n/fr and commit the translation.
你现在可以用简体中文启动站点,并查看翻译:
- npm
- Yarn
- pnpm
- Bun
npm run start -- --locale zh-Hans
yarn run start --locale zh-Hans
pnpm run start --locale zh-Hans
bun run start --locale zh-Hans
你也可以在本地或 CI 上构建站点:
- npm
- Yarn
- pnpm
- Bun
npm run build
# 或者
npm run build -- --locale zh-Hans
yarn build
# 或者
yarn build --locale zh-Hans
pnpm run build
# 或者
pnpm run build --locale zh-Hans
bun run build
# 或者
bun run build --locale zh-Hans
以此类推
为你要支持的每一个语言重复相同的流程。
维护
Keeping translated files consistent with the originals can be challenging, in particular for Markdown documents.
翻译 Markdown 文档
When an untranslated Markdown document is edited, it is your responsibility to maintain the respective translated files, and we unfortunately don't have a good way to help you do so.
To keep your translated sites consistent, when the website/docs/doc1.md doc is edited, you need backport these edits to i18n/fr/docusaurus-plugin-content-docs/current/doc1.md.
翻译 JSON
To help you maintain the JSON translation files, it is possible to run again the write-translations CLI command:
- npm
- Yarn
- pnpm
- Bun
npm run write-translations -- --locale zh-Hans
yarn write-translations --locale zh-Hans
pnpm run write-translations --locale zh-Hans
bun run write-translations --locale zh-Hans
新译文会被追加,而旧译文不会被覆盖。
Reset your translations with the --override option.
本地化编辑链接
When the user is browsing a page at /fr/doc1, the edit button will link by default to the unlocalized doc at website/docs/doc1.md.
Your translations are on Git, and you can use the editLocalizedFiles: true option of the docs and blog plugins.
The edit button will link to the localized doc at i18n/fr/docusaurus-plugin-content-docs/current/doc1.md.