name: Weblate Synchronization on: # Run every 6 hours to sync with Weblate schedule: - cron: '0 */6 * * *' # Allow manual triggering workflow_dispatch: # Trigger on push to main branch (for source updates) push: branches: [ main ] paths: - 'src/uu/*/locales/en-US.ftl' - 'src/uucore/locales/en-US.ftl' - 'src/uu/*/src/*.rs' # Serialize with upstream-source-sync: both push to main, and a source push # landing mid-sync is what leaves Weblate behind and gets its push rejected. concurrency: group: l10n-sync cancel-in-progress: false jobs: sync-weblate: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v7 with: token: ${{ secrets.WEBLATE_TOKEN || github.token }} fetch-depth: 0 - name: Install Weblate CLI run: | pip install wlc - name: Configure Weblate CLI run: | mkdir -p ~/.config cat > ~/.config/weblate << EOF [weblate] url = https://hosted.weblate.org/api/ [keys] https://hosted.weblate.org/api/ = ${{ secrets.WEBLATE_API_KEY }} EOF - name: Install Mozilla Fluent Linter run: | pip install moz-fluent-linter - name: Pull translations from Weblate run: | # `wlc pull` makes Weblate rebase its component repositories onto this # repo's main branch. After the daily upstream-source-sync rewrites # en-US.ftl, Weblate's checkouts diverge and the rebase fails with # "Could not rebase local branch onto remote branch ", # aborting the step under `bash -e`. # # Recover by resetting Weblate's checkout to the remote (main) and # retrying. Translations live in Weblate's *database*, so `wlc reset` # only discards the unmergeable VCS state, not the translations — # Weblate re-commits them cleanly on the next cycle. We do NOT commit # or push Weblate's diverged branches first: that push is the very # non-fast-forward state we're trying to clear, and hosted.weblate.org # tends to drop the connection (RemoteDisconnected) mid-operation, so # we retry the reset+pull a few times. pull_translations() { local attempt for attempt in 1 2 3 4 5; do if wlc pull rust-coreutils; then return 0 fi echo "::warning::wlc pull failed (attempt ${attempt}); resetting Weblate checkout to remote and retrying" wlc reset rust-coreutils || true sleep $((attempt * 15)) done echo "::error::wlc pull still failing after ${attempt} attempts" return 1 } pull_translations - name: Fix Fluent syntax issues run: | # Ensure all .ftl files end with a newline find src/uu/*/locales src/uucore/locales -name "*.ftl" -type f | while read -r f; do if [ -s "$f" ] && [ "$(tail -c1 "$f" | wc -l)" -eq 0 ]; then echo "" >> "$f" echo "Fixed missing newline: $f" fi done - name: Validate Fluent syntax run: | has_errors=false for dir in $(find src/uu/*/locales src/uucore/locales -name "*.ftl" -type f -exec dirname {} \; | sort -u); do if ! moz-fluent-lint --config .github/fluent_linter_config.yml "$dir"; then echo "::warning::Fluent syntax issues in $dir" has_errors=true fi done if [ "$has_errors" = true ]; then echo "::warning::Some Fluent files have syntax issues that could not be auto-fixed" fi - name: Commit and push translations run: | git config --local user.email "action@github.com" git config --local user.name "GitHub Action" if [ -n "$(git status --porcelain)" ]; then # Use find -type f so we don't traverse symlinked locales dirs # (e.g. base64/locales -> ../base32/locales), which would cause # "pathspec is beyond a symbolic link" errors from git add. find src/uu/*/locales src/uucore/locales -name '*.ftl' -type f -print0 \ | xargs -0 git add -- git commit -m "Update translations from Weblate" git pull --rebase git push else echo "No translation updates to commit" fi - name: Push source changes to Weblate id: push-sources if: github.event_name == 'push' run: | # Lock translations to prevent conflicts wlc lock rust-coreutils # Push source changes wlc push rust-coreutils - name: Unlock Weblate project # Always runs: a project left locked by a failed push blocks every # translator until someone unlocks it by hand. if: always() && steps.push-sources.outcome != 'skipped' run: | wlc unlock rust-coreutils