Skip to content

Translate README and Create PR #61

Translate README and Create PR

Translate README and Create PR #61

name: Translate README and Create PR
on:
workflow_dispatch:
inputs:
languages:
description: "Languages to translate to (example: ['BN','DE','ES','FR','HI'] or ['ZH','IT','PT','UK'])"
required: true
type: string
jobs:
translate_and_create_pr:
runs-on: ubuntu-latest
permissions:
contents: 'write'
pull-requests: 'write'
id-token: 'write'
steps:
- name: Checkout code
uses: actions/checkout@v4
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: ${{ secrets.GCS_SA_KEY }}
create_credentials_file: true
- name: get_token
id: get_token
run: |
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
ACCESS_TOKEN=$(gcloud auth print-access-token)
echo "::add-mask::$ACCESS_TOKEN"
echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> $GITHUB_ENV
- name: Upload README.html to sag_readme bucket
run: |
gcloud init
gcloud storage cp README.html gs://sag_readme/
- name: Create sag_readme_translated bucket
run: |
gcloud storage buckets create gs://sag_readme_translated
- name: Translate using Google's batchTranslateText API
run: .github/workflows/translate_script.sh "${{ github.event.inputs.languages }}"
id: translate
- name: Wait for translation to complete
run: |
status=$(curl -s -H "Authorization: Bearer ${{ env.ACCESS_TOKEN }}" \
"https://translation.googleapis.com/v3/${{ steps.translate.outputs.OPERATION_ID }}")
until [ "$(echo "$status" | jq -r '.done')" == "true" ]; do
echo "Translation not yet complete, sleeping for 60 seconds..."
sleep 60
status=$(curl -s -H "Authorization: Bearer ${{ env.ACCESS_TOKEN }}" \
"https://translation.googleapis.com/v3/${{ steps.translate.outputs.OPERATION_ID }}")
done
echo "Translation completed."
- name: Download translated files
run: |
fileDir="docs/readme/translated_files"
mkdir $fileDir
gcloud storage cp gs://sag_readme_translated/* ${fileDir}/
for f in $(ls ${fileDir}/*.html); do
new_name=$(echo $f | sed 's|sag_readme_||' | sed 's|_translations||')
mv $f $new_name
lang=$(echo $new_name | sed 's|.*/README_||' | sed 's|.html||')
sed -i "s|html lang=\"en\"|html lang=\"${lang}\"|g" $new_name
sed -i "s|screenshots/EN/|screenshots/${lang}/|g" $new_name
sed -i "s|option value=\"README_EN.html\" selected|option value=\"README_EN.html\"|g" $new_name
sed -i "s|option value=\"README_${lang}.html\"|option value=\"README_${lang}.html\" selected|g" $new_name
done
- name: Store translated files as artifacts
uses: actions/upload-artifact@v4
with:
name: translated_files
path: docs/readme/translated_files
- name: Cleanup bucket
if: always()
run: |
gcloud storage rm gs://sag_readme_translated --recursive