#!/usr/bin/env bash
set -euo pipefail
MODE="--incremental"
UPSTREAM_DEFAULT="/path/to/anthropic-skills-repo/skills"
UPSTREAM="$UPSTREAM_DEFAULT"
while [[ $# -gt 0 ]]; do
case "$1" in
--full|--incremental)
MODE="$1"; shift ;;
--upstream)
UPSTREAM="${2:-}"; shift 2 ;;
*)
echo "Usage: $0 [--incremental|--full] [--upstream <upstream_skills_dir>]"
exit 1 ;;
esac
done
if [[ ! -d "$UPSTREAM" ]]; then
echo "Upstream dir not found: $UPSTREAM"
echo "Tip: git clone https://github.com/anthropics/skills.git /path/to/anthropic-skills-repo"
exit 1
fi
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="$ROOT/translations"
STATE="$ROOT/.translation-state.json"
TMP_PROMPT="/tmp/gemini_translate_prompt.txt"
mkdir -p "$OUT"
get_hash () {
local file="$1"
sha256sum "$file" | awk '{print $1}'
}
state_get_hash () {
local rel="$1"
python3 - "$STATE" "$rel" <<'PY'
import json,sys,os
state_path, rel = sys.argv[1], sys.argv[2]
if not os.path.exists(state_path):
print("")
raise SystemExit
try:
data = json.load(open(state_path, 'r', encoding='utf-8'))
except Exception:
print("")
raise SystemExit
print((data.get('files') or {}).get(rel, {}).get('src_sha256', ''))
PY
}
state_upsert_hash () {
local rel="$1"
local sha="$2"
python3 - "$STATE" "$rel" "$sha" "$UPSTREAM" <<'PY'
import json,sys,os,subprocess
state_path, rel, sha, upstream = sys.argv[1:]
if os.path.exists(state_path):
try:
data = json.load(open(state_path, 'r', encoding='utf-8'))
except Exception:
data = {}
else:
data = {}
files = data.setdefault('files', {})
entry = files.setdefault(rel, {})
entry['src_sha256'] = sha
# optional audit info: upstream repo HEAD
head = ''
repo = os.path.abspath(os.path.join(upstream, '..'))
if os.path.isdir(os.path.join(repo, '.git')):
try:
head = subprocess.check_output(
['git', '-C', repo, 'rev-parse', 'HEAD'],
text=True
).strip()
except Exception:
head = ''
if head:
data['upstream_repo_head'] = head
os.makedirs(os.path.dirname(state_path), exist_ok=True)
with open(state_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
PY
}
translate_one () {
local src_md="$1"
local rel="${src_md#$UPSTREAM/}"
local out_md="$OUT/$rel"
mkdir -p "$(dirname "$out_md")"
{
echo '你是技术文档翻译助手。请把下面英文 SKILL.md 完整翻译成简体中文,要求:'
echo '1) 保留原有 Markdown 结构、标题层级、列表、代码块、YAML frontmatter。'
echo '2) 专有名词和命令保留英文(例如 Claude API, MCP, Playwright, gemini 命令)。'
echo '3) 文风准确、自然,不要删减内容。'
echo '4) 仅输出翻译后的 Markdown,不要额外解释。'
echo ''
echo '--- 原文开始 ---'
cat "$src_md"
echo ''
echo '--- 原文结束 ---'
} > "$TMP_PROMPT"
gemini --model gemini-2.5-pro "$(cat "$TMP_PROMPT")" > "$out_md"
echo "translated: $rel"
}
count=0
skipped=0
while IFS= read -r -d '' f; do
rel="${f#$UPSTREAM/}"
src_sha="$(get_hash "$f")"
if [[ "$MODE" == "--incremental" ]]; then
old_sha="$(state_get_hash "$rel")"
if [[ -n "$old_sha" && "$old_sha" == "$src_sha" ]]; then
skipped=$((skipped+1))
continue
fi
fi
translate_one "$f"
state_upsert_hash "$rel" "$src_sha"
count=$((count+1))
done < <(find "$UPSTREAM" -type f -name 'SKILL.md' -print0 | sort -z)
echo "done, translated files: $count, skipped: $skipped, mode: $MODE"
echo "state file: $STATE"