跳转到主要内容

框架集成

使用流行框架设置 Champollion 的分步指南。

Hugo (TOML / YAML / Markdown)

项目结构

Hugo 使用 i18n/ 进行字符串翻译,使用 content/ 进行页面内容:

my-hugo-site/
├── i18n/
│ ├── en.toml ← source of truth
│ ├── fr.toml
│ └── ja.toml
├── content/
│ ├── posts/
│ │ ├── hello.md ← source (English)
│ │ ├── hello.fr.md
│ │ └── hello.ja.md
│ └── about.md
└── .env.local

设置

npm install --save-dev champollion
champollion.config.json
{
"version": 3,
"inputLocale": "en",
"localesDir": "./i18n",
"contentDir": "./content",
"format": "auto",
"languages": ["fr", "de", "ja", "es", "ko", "zh"]
}
champollion sync # sync i18n string files + content files
champollion sync --dry # preview changes without writing

内容翻译详情

Front matter:支持 YAML (---) 和 TOML (+++) 分隔符。默认翻译 titledescriptionsummarysubtitlecaptionlinkTitle。所有其他字段(日期、草稿、标签、权重、slug 等)都会保留。使用配置中的 translatableFields 进行自定义。

块保护:代码块、Hugo shortcodes、内联代码和原始 HTML 会自动使用 Unicode 哨兵占位符进行屏蔽。它们会原封不动地通过。

文件名约定:遵循 Hugo 的按文件名翻译模式:

  • my-post.mdmy-post.fr.md
  • my-post.en.mdmy-post.fr.md(去除源后缀)

跳过现有文件:现有的已翻译文件永远不会被覆盖。删除目标文件以强制重新翻译。

复数形式

TOML 和 YAML 语言环境支持 CLDR 复数形式:

[items]
one = "{{ .Count }} item"
other = "{{ .Count }} items"

在内部表示为 items.oneitems.other 用于差异比较,然后在写入时重新序列化为正确的分段格式。