使用Claude和GitHub Actions自动翻译博客

我写的每篇文章都会自动翻译。Claude处理翻译,将结果提交到main,翻译版本与原文一起部署。

流程

当英文内容合并到main时,翻译工作流在GitHub Actions中运行:

on:
  push:
    branches: [main]
    paths:
      - 'content/**/*.en.md'
flowchart LR
    B[合并到main] --> C[生成翻译]
    C --> D[提交到main]
    D --> E[所有语言一起部署到生产环境]

提示词

工作流使用claude-code-action

For each .en.md file that was added or modified:
1. Read the English content
2. Create translations for these languages: es, nl, de, it, fr, ja, zh, ru, hi
3. Save each translation as filename.{lang}.md (e.g., hello-world.es.md)
4. Preserve the frontmatter structure exactly, but translate the title
5. Translate the body content naturally
6. Keep code blocks, URLs, file paths, and technical terms unchanged

一个提示词处理所有9种语言。“自然翻译"比"翻译这段文字"产生更流畅的输出。代码块和技术术语保持英文。

处理Front Matter

Hugo文章有YAML front matter。有些字段需要翻译(标题),有些不需要(日期、slug):

“精确保留frontmatter结构,但翻译标题”

英文:

---
title: "Hello World"
date: 2025-12-20
draft: false
---

印地语:

---
title: "नमस्ते दुनिया"
date: 2025-12-20
draft: false
---

提交

工作流设置机器人身份并限制Claude可以做的事情:

- name: Setup git identity
  run: |
    git config --global user.email "claude[bot]@users.noreply.github.com"
    git config --global user.name "claude[bot]"

- name: Translate with Claude
  uses: anthropics/claude-code-action@v1
  with:
    claude_args: "--allowedTools 'Write,Bash(git:*)'"
    prompt: |
      ...
      Use `git add`, `git commit`, and `git push` to add the translations directly to main.

--allowedTools将Claude限制为只能写入文件和运行git命令。

质量

对于个人博客来说足够好了。Claude的翻译流畅,能很好地捕捉含义。母语者会注意到偶尔有些生硬的措辞,但比我自己能做的要好。

其他用途

同样的模式适用于文档生成、变更日志摘要、提交信息编写和发布说明:

  1. 检测发生了什么变化
  2. 将内容连同指令发送给Claude
  3. 将结果提交回分支