Debian Codegraph @

l10n/.github/workflows/weblate-sync.yml

raw
1 name: Weblate Synchronization
2
3 on:
4 # Run every 6 hours to sync with Weblate
5 schedule:
6 - cron: '0 */6 * * *'
7
8 # Allow manual triggering
9 workflow_dispatch:
10
11 # Trigger on push to main branch (for source updates)
12 push:
13 branches: [ main ]
14 paths:
15 - 'src/uu/*/locales/en-US.ftl'
16 - 'src/uucore/locales/en-US.ftl'
17 - 'src/uu/*/src/*.rs'
18
19 # Serialize with upstream-source-sync: both push to main, and a source push
20 # landing mid-sync is what leaves Weblate behind and gets its push rejected.
21 concurrency:
22 group: l10n-sync
23 cancel-in-progress: false
24
25 jobs:
26 sync-weblate:
27 runs-on: ubuntu-latest
28
29 steps:
30 - name: Checkout repository
31 uses: actions/checkout@v7
32 with:
33 token: ${{ secrets.WEBLATE_TOKEN || github.token }}
34 fetch-depth: 0
35
36 - name: Install Weblate CLI
37 run: |
38 pip install wlc
39
40 - name: Configure Weblate CLI
41 run: |
42 mkdir -p ~/.config
43 cat > ~/.config/weblate << EOF
44 [weblate]
45 url = https://hosted.weblate.org/api/
46
47 [keys]
48 https://hosted.weblate.org/api/ = ${{ secrets.WEBLATE_API_KEY }}
49 EOF
50
51 - name: Install Mozilla Fluent Linter
52 run: |
53 pip install moz-fluent-linter
54
55 - name: Pull translations from Weblate
56 run: |
57 # `wlc pull` makes Weblate rebase its component repositories onto this
58 # repo's main branch. After the daily upstream-source-sync rewrites
59 # en-US.ftl, Weblate's checkouts diverge and the rebase fails with
60 # "Could not rebase local branch onto remote branch <component>",
61 # aborting the step under `bash -e`.
62 #
63 # Recover by resetting Weblate's checkout to the remote (main) and
64 # retrying. Translations live in Weblate's *database*, so `wlc reset`
65 # only discards the unmergeable VCS state, not the translations —
66 # Weblate re-commits them cleanly on the next cycle. We do NOT commit
67 # or push Weblate's diverged branches first: that push is the very
68 # non-fast-forward state we're trying to clear, and hosted.weblate.org
69 # tends to drop the connection (RemoteDisconnected) mid-operation, so
70 # we retry the reset+pull a few times.
71 pull_translations() {
72 local attempt
73 for attempt in 1 2 3 4 5; do
74 if wlc pull rust-coreutils; then
75 return 0
76 fi
77 echo "::warning::wlc pull failed (attempt ${attempt}); resetting Weblate checkout to remote and retrying"
78 wlc reset rust-coreutils || true
79 sleep $((attempt * 15))
80 done
81 echo "::error::wlc pull still failing after ${attempt} attempts"
82 return 1
83 }
84 pull_translations
85
86 - name: Fix Fluent syntax issues
87 run: |
88 # Ensure all .ftl files end with a newline
89 find src/uu/*/locales src/uucore/locales -name "*.ftl" -type f | while read -r f; do
90 if [ -s "$f" ] && [ "$(tail -c1 "$f" | wc -l)" -eq 0 ]; then
91 echo "" >> "$f"
92 echo "Fixed missing newline: $f"
93 fi
94 done
95
96 - name: Validate Fluent syntax
97 run: |
98 has_errors=false
99 for dir in $(find src/uu/*/locales src/uucore/locales -name "*.ftl" -type f -exec dirname {} \; | sort -u); do
100 if ! moz-fluent-lint --config .github/fluent_linter_config.yml "$dir"; then
101 echo "::warning::Fluent syntax issues in $dir"
102 has_errors=true
103 fi
104 done
105 if [ "$has_errors" = true ]; then
106 echo "::warning::Some Fluent files have syntax issues that could not be auto-fixed"
107 fi
108
109 - name: Commit and push translations
110 run: |
111 git config --local user.email "[email protected]"
112 git config --local user.name "GitHub Action"
113
114 if [ -n "$(git status --porcelain)" ]; then
115 # Use find -type f so we don't traverse symlinked locales dirs
116 # (e.g. base64/locales -> ../base32/locales), which would cause
117 # "pathspec is beyond a symbolic link" errors from git add.
118 find src/uu/*/locales src/uucore/locales -name '*.ftl' -type f -print0 \
119 | xargs -0 git add --
120 git commit -m "Update translations from Weblate"
121 git pull --rebase
122 git push
123 else
124 echo "No translation updates to commit"
125 fi
126
127 - name: Push source changes to Weblate
128 id: push-sources
129 if: github.event_name == 'push'
130 run: |
131 # Lock translations to prevent conflicts
132 wlc lock rust-coreutils
133
134 # Push source changes
135 wlc push rust-coreutils
136
137 - name: Unlock Weblate project
138 # Always runs: a project left locked by a failed push blocks every
139 # translator until someone unlocks it by hand.
140 if: always() && steps.push-sources.outcome != 'skipped'
141 run: |
142 wlc unlock rust-coreutils