Skip to content

Commit eda9403

Browse files
committed
fix: also re-translate merged files when upstream source is updated later
1 parent 1b1bf62 commit eda9403

1 file changed

Lines changed: 51 additions & 16 deletions

File tree

scripts/sync-translations.sh

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# Print candidate cndocs files that likely need translation sync
5-
# Strategy:
6-
# 1) Compare upstream/main vs local production for docs/*.md
7-
# 2) Map changed docs/<name>.md -> cndocs/<name>.md when target exists
8-
# 3) Output unique, sorted cndocs paths (one per line)
4+
# Print candidate cndocs files that need translation sync.
5+
# Two strategies combined:
6+
# A) Compare upstream/main vs local production for docs/*.md (new upstream changes)
7+
# B) For previously-merged translations, check if upstream file was updated
8+
# more recently than the cndocs translation — if so, re-translate.
99

1010
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
1111
cd "$REPO_ROOT"
1212

13+
PROGRESS_FILE="$REPO_ROOT/scripts/translate-progress.json"
14+
1315
# Ensure upstream exists
1416
if ! git remote get-url upstream >/dev/null 2>&1; then
1517
echo "ERROR: missing upstream remote" >&2
@@ -18,18 +20,51 @@ fi
1820

1921
git fetch upstream --quiet
2022

21-
# Changed docs files between production and upstream/main
22-
changed_docs=$(git diff --name-only production..upstream/main -- 'docs/*.md' || true)
23+
candidates=()
2324

24-
if [ -z "${changed_docs}" ]; then
25-
exit 0
25+
# --- Strategy A: new upstream changes not yet in production ---
26+
changed_docs=$(git diff --name-only production..upstream/main -- 'docs/*.md' || true)
27+
if [ -n "${changed_docs}" ]; then
28+
while IFS= read -r f; do
29+
[ -z "$f" ] && continue
30+
name="${f#docs/}"
31+
target="cndocs/${name}"
32+
if [ -f "$target" ]; then
33+
candidates+=("$target")
34+
fi
35+
done <<< "$changed_docs"
2636
fi
2737

28-
while IFS= read -r f; do
29-
[ -z "$f" ] && continue
30-
name="${f#docs/}"
31-
target="cndocs/${name}"
32-
if [ -f "$target" ]; then
33-
echo "$target"
38+
# --- Strategy B: merged translations whose upstream source is now newer ---
39+
if [ -f "$PROGRESS_FILE" ]; then
40+
merged_files=$(python3 -c "
41+
import json, sys
42+
with open('$PROGRESS_FILE') as f:
43+
data = json.load(f)
44+
for item in data.get('merged', []):
45+
print(item)
46+
" 2>/dev/null || true)
47+
48+
if [ -n "$merged_files" ]; then
49+
while IFS= read -r target; do
50+
[ -z "$target" ] && continue
51+
# cndocs/foo.md -> docs/foo.md
52+
name="${target#cndocs/}"
53+
source="docs/${name}"
54+
55+
# Get upstream last-modified epoch for the source file
56+
upstream_epoch=$(git log -1 --format="%ct" upstream/main -- "$source" 2>/dev/null || echo 0)
57+
# Get production last-modified epoch for the translated file
58+
translation_epoch=$(git log -1 --format="%ct" production -- "$target" 2>/dev/null || echo 0)
59+
60+
if [ "$upstream_epoch" -gt "$translation_epoch" ] 2>/dev/null; then
61+
candidates+=("$target")
62+
fi
63+
done <<< "$merged_files"
3464
fi
35-
done <<< "$changed_docs" | sort -u
65+
fi
66+
67+
# Deduplicate and sort
68+
if [ ${#candidates[@]} -gt 0 ]; then
69+
printf '%s\n' "${candidates[@]}" | sort -u
70+
fi

0 commit comments

Comments
 (0)