Skip to content

Translate README and Create PR #41

Translate README and Create PR

Translate README and Create PR #41

name: Translate README and Create PR
on:
workflow_dispatch:
inputs:
languages:
description: "Languages to translate to (example: ['AR','BN','DE','ES','FR','HI'] or ['ZH','IT','PT','UK','UR'])"
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@v3
- id: 'auth'
uses: 'google-github-actions/auth@v1'
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
done
- name: Cleanup bucket
if: always()
run: |
gcloud storage rm gs://sag_readme_translated --recursive
- name: Create Pull Request
env:
GH_TOKEN: ${{ github.token }}
run: |
git config --global user.email "AndrewBPoppe@gmail.com"
git config --global user.name "Andrew Poppe"
# Create a new branch for the translated files
git checkout -b translated-branch
if [ $(git ls-remote --heads origin refs/heads/translated-branch | wc -l) == 1 ] ; then
git pull origin translated-branch
fi
# Copy the translated files to the repository
# mv -r translated_files ./docs/readme/
# Commit and push the changes
git add ./docs/readme/translated_files
git commit -m "Add translated files"
git push origin translated-branch
# Create a pull request
gh pr create --base main --head translated-branch --title "Translated README" --body "This is an automated pull request to add translated README files for these languages: ${{ github.event.inputs.languages }}"