|
| 1 | +name: Translate Docs |
| 2 | + |
| 3 | +# Refreshes one language's machine-translated docs (i18n/<lang>/) and opens or |
| 4 | +# updates a draft PR with the result for that language's reviewers. See |
| 5 | +# docs/translations.md and i18n/README.md. |
| 6 | +# |
| 7 | +# Manual dispatch only. To refresh on a cadence, add a `schedule:` trigger |
| 8 | +# (e.g. `- cron: "0 5 1,15 * *"`) with a matrix over i18n/languages.yml once |
| 9 | +# someone owns the API budget. |
| 10 | +# |
| 11 | +# Required configuration: |
| 12 | +# - secrets.ANTHROPIC_API_KEY |
| 13 | +# - Settings → Actions → General → "Allow GitHub Actions to create and |
| 14 | +# approve pull requests" |
| 15 | +# |
| 16 | +# The PR is opened with GITHUB_TOKEN, so it does not trigger CI by itself; |
| 17 | +# close and reopen it (or push to its branch) to run the checks, or pass |
| 18 | +# create-pull-request a bot/App token via `token:`. Every refresh puts the PR |
| 19 | +# back in draft, since its content changed under any earlier review. |
| 20 | + |
| 21 | +on: |
| 22 | + workflow_dispatch: |
| 23 | + inputs: |
| 24 | + lang: |
| 25 | + description: Language code from i18n/languages.yml (e.g. pt-BR) |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + pages: |
| 29 | + description: Re-translate exactly these pages in full (space-separated paths under docs/, e.g. "index.md servers/tools.md"); not capped by limit |
| 30 | + required: false |
| 31 | + type: string |
| 32 | + grep: |
| 33 | + description: Revise only current pages (and sections) whose English matches this regex, e.g. after a glossary change |
| 34 | + required: false |
| 35 | + type: string |
| 36 | + limit: |
| 37 | + description: Maximum number of pages to translate in this run |
| 38 | + required: false |
| 39 | + default: 15 |
| 40 | + type: number |
| 41 | + |
| 42 | +permissions: {} |
| 43 | + |
| 44 | +concurrency: |
| 45 | + # One run per language at a time; a second dispatch queues rather than |
| 46 | + # cancelling a run that is already spending API budget. |
| 47 | + group: translate-${{ inputs.lang }} |
| 48 | + cancel-in-progress: false |
| 49 | + |
| 50 | +jobs: |
| 51 | + translate: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + permissions: |
| 54 | + contents: write # push the translate/<lang> branch |
| 55 | + pull-requests: write # open or update the draft PR |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 58 | + with: |
| 59 | + persist-credentials: false |
| 60 | + |
| 61 | + - name: Install uv |
| 62 | + uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0 |
| 63 | + with: |
| 64 | + enable-cache: true |
| 65 | + version: 0.9.5 |
| 66 | + |
| 67 | + - name: Install dependencies |
| 68 | + run: uv sync --frozen --group translate |
| 69 | + |
| 70 | + # Exit code 1 means some pages failed while the rest were written: those |
| 71 | + # still ship in the PR (the body says so) and the run is failed at the |
| 72 | + # end. Any other non-zero code (2: configuration or credentials, 3: the |
| 73 | + # tool crashed) stops here, before anything is built or proposed. |
| 74 | + - name: Translate |
| 75 | + id: translate |
| 76 | + env: |
| 77 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 78 | + LANG_CODE: ${{ inputs.lang }} |
| 79 | + PAGES: ${{ inputs.pages }} |
| 80 | + GREP: ${{ inputs.grep }} |
| 81 | + LIMIT: ${{ inputs.limit }} |
| 82 | + # Per-page progress lines reach the log as they happen, not at exit. |
| 83 | + PYTHONUNBUFFERED: "1" |
| 84 | + run: | |
| 85 | + args=(--lang "$LANG_CODE" --limit "$LIMIT") |
| 86 | + if [[ -n "$PAGES" ]]; then |
| 87 | + read -r -a pages <<< "$PAGES" |
| 88 | + args+=(--pages "${pages[@]}") |
| 89 | + fi |
| 90 | + if [[ -n "$GREP" ]]; then |
| 91 | + args+=(--grep "$GREP") |
| 92 | + fi |
| 93 | + set +e |
| 94 | + uv run --frozen --no-sync python scripts/docs/translations.py translate "${args[@]}" 2>&1 \ |
| 95 | + | tee "$RUNNER_TEMP/translate.log" |
| 96 | + exit_code=${PIPESTATUS[0]} |
| 97 | + set -e |
| 98 | + echo "exit_code=$exit_code" >> "$GITHUB_OUTPUT" |
| 99 | + case $exit_code in |
| 100 | + 0 | 1) ;; |
| 101 | + *) exit "$exit_code" ;; |
| 102 | + esac |
| 103 | +
|
| 104 | + # build.sh builds English first, then this language with --strict so a |
| 105 | + # dead link or anchor introduced by this refresh fails here, in the run |
| 106 | + # that produced it, rather than as a warning in everyone else's builds. |
| 107 | + # The translations are already paid for, so a failed build still opens |
| 108 | + # the PR (flagged in its body) and fails the run at the end. |
| 109 | + - name: Build the language site strictly |
| 110 | + id: build |
| 111 | + continue-on-error: true |
| 112 | + env: |
| 113 | + DOCS_LANGUAGES: ${{ inputs.lang }} |
| 114 | + DOCS_STRICT_LANGUAGES: "1" |
| 115 | + run: bash scripts/docs/build.sh |
| 116 | + |
| 117 | + - name: Compose the pull request |
| 118 | + id: compose |
| 119 | + env: |
| 120 | + LANG_CODE: ${{ inputs.lang }} |
| 121 | + TRANSLATE_EXIT_CODE: ${{ steps.translate.outputs.exit_code }} |
| 122 | + BUILD_OUTCOME: ${{ steps.build.outcome }} |
| 123 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 124 | + run: | |
| 125 | + reviewers="$(PYTHONPATH=scripts/docs uv run --frozen --no-sync python -c ' |
| 126 | + import os, build_config |
| 127 | + code = os.environ["LANG_CODE"] |
| 128 | + language = next(lang for lang in build_config.load_registry().languages if lang.code == code) |
| 129 | + print(",".join(language.reviewers)) |
| 130 | + ')" |
| 131 | + echo "reviewers=$reviewers" >> "$GITHUB_OUTPUT" |
| 132 | + { |
| 133 | + echo "Machine translation refresh for \`$LANG_CODE\`, generated by [this workflow run]($RUN_URL)." |
| 134 | + echo |
| 135 | + echo "Corrections go in \`i18n/$LANG_CODE/instructions.md\` or \`i18n/$LANG_CODE/glossary.json\`, not in this diff: the pages are regenerated from those inputs (see \`i18n/README.md\`)." |
| 136 | + echo |
| 137 | + if [[ "$BUILD_OUTCOME" == "failure" ]]; then |
| 138 | + echo "> [!CAUTION]" |
| 139 | + echo "> The strict build of this language site failed (a dead link or anchor, most likely); see the run log before merging." |
| 140 | + echo |
| 141 | + fi |
| 142 | + if [[ "$TRANSLATE_EXIT_CODE" != "0" ]]; then |
| 143 | + echo "> [!WARNING]" |
| 144 | + echo "> Some pages failed to translate and keep their previous version:" |
| 145 | + echo |
| 146 | + echo '```text' |
| 147 | + grep '^error:' "$RUNNER_TEMP/translate.log" || echo "(see the run log)" |
| 148 | + echo '```' |
| 149 | + echo |
| 150 | + fi |
| 151 | + echo "### Status" |
| 152 | + echo |
| 153 | + echo '```text' |
| 154 | + uv run --frozen --no-sync python scripts/docs/translations.py status --lang "$LANG_CODE" |
| 155 | + echo '```' |
| 156 | + echo |
| 157 | + echo "### Usage" |
| 158 | + echo |
| 159 | + echo '```text' |
| 160 | + grep -i '^usage' "$RUNNER_TEMP/translate.log" || echo "(no usage reported)" |
| 161 | + echo '```' |
| 162 | + } > "$RUNNER_TEMP/pr-body.md" |
| 163 | +
|
| 164 | + - name: Open or update the draft PR |
| 165 | + uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 |
| 166 | + with: |
| 167 | + branch: translate/${{ inputs.lang }} |
| 168 | + delete-branch: true |
| 169 | + add-paths: i18n/${{ inputs.lang }}/ |
| 170 | + commit-message: "i18n(${{ inputs.lang }}): refresh translations" |
| 171 | + title: "i18n(${{ inputs.lang }}): refresh translations" |
| 172 | + body-path: ${{ runner.temp }}/pr-body.md |
| 173 | + labels: translation |
| 174 | + reviewers: ${{ steps.compose.outputs.reviewers }} |
| 175 | + draft: always-true |
| 176 | + |
| 177 | + - name: Fail the run if some pages failed or the site did not build |
| 178 | + if: steps.translate.outputs.exit_code != '0' || steps.build.outcome == 'failure' |
| 179 | + env: |
| 180 | + TRANSLATE_EXIT_CODE: ${{ steps.translate.outputs.exit_code }} |
| 181 | + BUILD_OUTCOME: ${{ steps.build.outcome }} |
| 182 | + run: | |
| 183 | + echo "::error::translate exit code ${TRANSLATE_EXIT_CODE}, strict build ${BUILD_OUTCOME}; the PR carries what succeeded and says so." |
| 184 | + exit 1 |
0 commit comments