Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Ali / Add automatic translation action #6649

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/sync_crowdin_translations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sync Crowdin translations

This action will automatically extract strings from the binary-static repo and upload them to Crowdin. It will also check whether Crowdin has new translations available, and if so, it will automatically download these translations and create a PR to binary-static's `master` branch to merge them in.
If there are new strings to translate, it will send a slack message to inform the translation team if there is TRANSLATIONS_SLACK_WEBHOOK available.

Required GitHub secrets:

- `PERSONAL_ACCESS_TOKEN`: (GitHub PAT) To allow the action to authenticate with Git for git operations.
- `CROWDIN_API_KEY`: To allow us to download and upload new language files to and from Crowdin.
- `TRANSLATIONS_SLACK_WEBHOOK`: (Optional) To allow send a slack message to inform the translation team in case new strings found.
100 changes: 100 additions & 0 deletions .github/workflows/sync_crowdin_translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Sync Crowdin translations

on:
workflow_dispatch:
push:
branches:
- master

jobs:
sync_crowdin_translations:
if: github.repository == 'binary-com/binary-static'
runs-on: ubuntu-latest
steps:
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: '12'

- name: Checkout master branch
uses: actions/checkout@v2
with:
repository: "binary-com/binary-static"
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

# In this step we're doing a couple things:
# - We generate a new messages.pot
# - We hash the newly generated messages.pot and compare it with the messages.pot on Crowdin.
# - We download the latest translation files from Crowdin, if there are new files, we create a PR.
- name: Generate and push to Crowdin
run: |
branch_name="binary_static_translations"

echo "Setting up Git identity"
git config --global user.name "DerivFE"
git config --global user.email "80095553+DerivFE@users.noreply.github.com"

echo "Installing Crowdin CLI"
sudo npm i -g @crowdin/cli

echo "Installing project dependencies and building the project"
npm install
npm run build

echo "Checking out new branch [$branch_name]"
git checkout -b "$branch_name"



echo "Updating translations source file"
last_messages_hash="$(git hash-object $(git rev-parse --show-toplevel)/src/translations/messages.pot)"
node ./scripts/render.js -t
current_messages_hash="$(git hash-object $(git rev-parse --show-toplevel)/src/translations/messages.pot)"
echo "- [crowdin]: message.pot hash is: $last_messages_hash"
echo "- [generated]: message.pot hash is: $current_messages_hash"

# We compare the generated messages.pot with the last messages.pot.
# Only send a Slack message and upload it to Crowdin if there were any changes made to messages.pot.
if [ "$last_messages_hash" != "$current_messages_hash" ]; then
echo "Hashes are different, uploading to Crowdin"
echo "Uploading new strings to Crowdin"
crowdin upload sources -T ${{ secrets.CROWDIN_API_KEY }}

# Send a message to Slack (granted we have a webhook secret).
# This check also allows a repo admin to disable the Slack message by removing the secret.
if [ -n "${{ secrets.TRANSLATIONS_SLACK_WEBHOOK }}" ]; then
echo "Sending message to Slack (#team_translations)"
curl -X POST -H 'Content-type: application/json' --data '{"text":"There are new or updated strings available for Binary.com (https://crowdin.com/project/binary-static)."}' ${{ secrets.TRANSLATIONS_SLACK_WEBHOOK }}
fi
fi


echo "Downloading translation files from Crowdin (*.po)"
crowdin download -T ${{ secrets.CROWDIN_API_KEY }}
echo "Updating javascript translation files (*.js)"
node ./scripts/render.js -j



if [ -z "$(git status --porcelain)" ]; then
echo "Found no new translation files that need to be merged with master. Not creating a PR."
else
echo "Found updated translation files that need to be merged with master. Creating a PR."

# Commit the newly downloaded files
cd $(git rev-parse --show-toplevel)
git add .
git commit -m "translations: 📚 sync translations with crowdin"

# Force push to this branch in case a previous run created it.
git push --set-upstream origin "$branch_name" -f

sudo apt install gh
gh auth login --with-token <<< ${{ secrets.PERSONAL_ACCESS_TOKEN }}
gh pr close "$branch_name" || true
gh pr create --fill --base "master" --head "binary-com:$branch_name"

echo "Attempting to approve and merge the PR."
gh pr merge "$branch_name" -s
echo "**The translation PR has beed merged successfully.**"
fi