跳转到内容

实战范例:管道组合技

yaku 从 stdin 读取、写入 stdout,天生适合 Unix 管道。

使用 markitdown 从 PDF 提取文字,再翻译:

Terminal window
markitdown report.pdf | yaku --to zh-CN --format md

使用 curl 获取页面并翻译内容:

Terminal window
curl -s https://example.com | yaku --to zh-CN

翻译 Markdown 内容:

Terminal window
curl -s https://raw.githubusercontent.com/user/repo/main/README.md | \
yaku --to zh-CN --format md

使用 GitHub CLI 获取并翻译 issue:

Terminal window
# 翻译 issue 内容
gh issue view 123 --json body -q .body | yaku --to zh-CN
# 翻译 issue 的所有评论
gh api repos/owner/repo/issues/123/comments \
--jq '.[].body' | yaku --to zh-CN
Terminal window
# 翻译 JSON API 响应
curl -s https://api.example.com/products | yaku --to zh-CN --format json
# 翻译后用 jq 格式化
curl -s https://api.example.com/data | \
yaku --to zh-CN --format json | jq '.'
Terminal window
# macOS:翻译剪贴板并覆盖
pbpaste | yaku --to zh-CN | pbcopy
# Linux (xclip)
xclip -selection clipboard -o | yaku --to zh-CN | xclip -selection clipboard
Terminal window
# 使用 -o 选项
echo "Hello" | yaku --to zh-CN -o greeting.txt
# 使用 shell 重定向
echo "Hello" | yaku --to zh-CN > greeting.txt
# 用循环翻译多个文件
for f in docs/en/*.md; do
yaku --to zh-CN -f "$f" -o "docs/zh-CN/$(basename "$f")"
done
Terminal window
man ls | col -b | yaku --to zh-CN | less

col -b 会移除 man page 输出中的格式控制码。

Terminal window
# 只翻译前 100 行
head -100 large-doc.md | yaku --to zh-CN --format md
# 翻译后计算字数
yaku --to zh-CN -f article.en.md | wc -w
# 从代码中提取字符串并翻译
grep -Eo '"[^"]*"' src/strings.go | yaku --to zh-CN
  • 用管道时记得加 --format 没有扩展名可检测时,yaku 默认使用纯文本。请明确指定 --format md--format json--format yaml
  • 翻译前先过滤。headtailgrep 缩减输入量,节省 token 和时间。
  • 输出文件时用 -o 代替 > -o 选项会在 stderr 打印确认信息,更清楚地表示文件已写入。