| 1 |
name: Translate |
| 2 |
|
| 3 |
on: |
| 4 |
schedule: |
| 5 |
- cron: "0 5 1,15 * *" # Run at 05:00 on the 1st and 15th of every month |
| 6 |
|
| 7 |
workflow_dispatch: |
| 8 |
inputs: |
| 9 |
debug_enabled: |
| 10 |
description: Run with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate) |
| 11 |
required: false |
| 12 |
default: "false" |
| 13 |
command: |
| 14 |
description: Command to run |
| 15 |
type: choice |
| 16 |
options: |
| 17 |
- translate-page |
| 18 |
- translate-lang |
| 19 |
- update-outdated |
| 20 |
- add-missing |
| 21 |
- update-and-add |
| 22 |
- remove-removable |
| 23 |
language: |
| 24 |
description: Language to translate to as a letter code (e.g. "es" for Spanish) |
| 25 |
type: string |
| 26 |
required: false |
| 27 |
default: "" |
| 28 |
en_path: |
| 29 |
description: File path in English to translate (e.g. docs/en/docs/index.md) |
| 30 |
type: string |
| 31 |
required: false |
| 32 |
default: "" |
| 33 |
commit_in_place: |
| 34 |
description: Commit changes directly instead of making a PR |
| 35 |
type: boolean |
| 36 |
required: false |
| 37 |
default: false |
| 38 |
max: |
| 39 |
description: Maximum number of items to translate (e.g. 10) |
| 40 |
type: number |
| 41 |
required: false |
| 42 |
default: 10 |
| 43 |
|
| 44 |
permissions: {} |
| 45 |
|
| 46 |
jobs: |
| 47 |
langs: |
| 48 |
runs-on: ubuntu-latest |
| 49 |
outputs: |
| 50 |
langs: ${{ steps.show-langs.outputs.langs }} |
| 51 |
commands: ${{ steps.show-langs.outputs.commands }} |
| 52 |
steps: |
| 53 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 54 |
with: |
| 55 |
persist-credentials: false |
| 56 |
- name: Set up Python |
| 57 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 58 |
with: |
| 59 |
python-version-file: ".python-version" |
| 60 |
- name: Setup uv |
| 61 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 62 |
with: |
| 63 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 64 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 65 |
version: "0.11.18" |
| 66 |
cache-dependency-glob: | |
| 67 |
pyproject.toml |
| 68 |
uv.lock |
| 69 |
- name: Install Dependencies |
| 70 |
run: uv sync --locked --no-dev --group github-actions --group translations |
| 71 |
- name: Export Language Codes |
| 72 |
id: show-langs |
| 73 |
run: | |
| 74 |
echo "langs=$(uv run ./scripts/translate.py llm-translatable-json)" >> $GITHUB_OUTPUT |
| 75 |
echo "commands=$(uv run ./scripts/translate.py commands-json)" >> $GITHUB_OUTPUT |
| 76 |
env: |
| 77 |
LANGUAGE: ${{ github.event.inputs.language }} |
| 78 |
COMMAND: ${{ github.event.inputs.command }} |
| 79 |
|
| 80 |
translate: |
| 81 |
if: github.repository_owner == 'fastapi' |
| 82 |
needs: langs |
| 83 |
runs-on: ubuntu-latest |
| 84 |
permissions: |
| 85 |
contents: write |
| 86 |
strategy: |
| 87 |
matrix: |
| 88 |
lang: ${{ fromJson(needs.langs.outputs.langs) }} |
| 89 |
command: ${{ fromJson(needs.langs.outputs.commands) }} |
| 90 |
steps: |
| 91 |
- name: Dump GitHub context |
| 92 |
env: |
| 93 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 94 |
run: echo "$GITHUB_CONTEXT" |
| 95 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 96 |
with: |
| 97 |
fetch-depth: 0 |
| 98 |
persist-credentials: true # Required for `git push` in `translate.py` |
| 99 |
- name: Set up Python |
| 100 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 101 |
with: |
| 102 |
python-version-file: ".python-version" |
| 103 |
- name: Setup uv |
| 104 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 105 |
with: |
| 106 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 107 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 108 |
version: "0.11.18" |
| 109 |
cache-dependency-glob: | |
| 110 |
pyproject.toml |
| 111 |
uv.lock |
| 112 |
- name: Install Dependencies |
| 113 |
run: uv sync --locked --no-dev --group github-actions --group translations |
| 114 |
# Allow debugging with tmate |
| 115 |
- name: Setup tmate session |
| 116 |
uses: mxschmitt/action-tmate@35b54afac29c97fb54faba5b513f8fbd1882f113 # v3.24 |
| 117 |
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }} |
| 118 |
with: |
| 119 |
limit-access-to-actor: true |
| 120 |
env: |
| 121 |
GITHUB_TOKEN: ${{ secrets.FASTAPI_TRANSLATIONS }} # zizmor: ignore[secrets-outside-env] |
| 122 |
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # zizmor: ignore[secrets-outside-env] |
| 123 |
- name: FastAPI Translate |
| 124 |
run: | |
| 125 |
uv run ./scripts/translate.py "$COMMAND" |
| 126 |
uv run ./scripts/translate.py make-pr |
| 127 |
env: |
| 128 |
GITHUB_TOKEN: ${{ secrets.FASTAPI_TRANSLATIONS }} # zizmor: ignore[secrets-outside-env] |
| 129 |
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # zizmor: ignore[secrets-outside-env] |
| 130 |
LANGUAGE: ${{ matrix.lang }} |
| 131 |
EN_PATH: ${{ github.event.inputs.en_path }} |
| 132 |
COMMAND: ${{ matrix.command }} |
| 133 |
COMMIT_IN_PLACE: ${{ github.event.inputs.commit_in_place == 'true' && 'true' || '' }} |
| 134 |
MAX: ${{ github.event.inputs.max }} |