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

Commit 0bce384

Browse files
authored
add automatic translation action (#6649)
1 parent baa6d6f commit 0bce384

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Sync Crowdin translations
2+
3+
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.
4+
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.
5+
6+
Required GitHub secrets:
7+
8+
- `PERSONAL_ACCESS_TOKEN`: (GitHub PAT) To allow the action to authenticate with Git for git operations.
9+
- `CROWDIN_API_KEY`: To allow us to download and upload new language files to and from Crowdin.
10+
- `TRANSLATIONS_SLACK_WEBHOOK`: (Optional) To allow send a slack message to inform the translation team in case new strings found.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Sync Crowdin translations
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
sync_crowdin_translations:
11+
if: github.repository == 'binary-com/binary-static'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup node
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: '12'
18+
19+
- name: Checkout master branch
20+
uses: actions/checkout@v2
21+
with:
22+
repository: "binary-com/binary-static"
23+
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
24+
25+
# In this step we're doing a couple things:
26+
# - We generate a new messages.pot
27+
# - We hash the newly generated messages.pot and compare it with the messages.pot on Crowdin.
28+
# - We download the latest translation files from Crowdin, if there are new files, we create a PR.
29+
- name: Generate and push to Crowdin
30+
run: |
31+
branch_name="binary_static_translations"
32+
33+
echo "Setting up Git identity"
34+
git config --global user.name "DerivFE"
35+
git config --global user.email "80095553+DerivFE@users.noreply.github.com"
36+
37+
echo "Installing Crowdin CLI"
38+
sudo npm i -g @crowdin/cli
39+
40+
echo "Installing project dependencies and building the project"
41+
npm install
42+
npm run build
43+
44+
echo "Checking out new branch [$branch_name]"
45+
git checkout -b "$branch_name"
46+
47+
48+
49+
echo "Updating translations source file"
50+
last_messages_hash="$(git hash-object $(git rev-parse --show-toplevel)/src/translations/messages.pot)"
51+
node ./scripts/render.js -t
52+
current_messages_hash="$(git hash-object $(git rev-parse --show-toplevel)/src/translations/messages.pot)"
53+
echo "- [crowdin]: message.pot hash is: $last_messages_hash"
54+
echo "- [generated]: message.pot hash is: $current_messages_hash"
55+
56+
# We compare the generated messages.pot with the last messages.pot.
57+
# Only send a Slack message and upload it to Crowdin if there were any changes made to messages.pot.
58+
if [ "$last_messages_hash" != "$current_messages_hash" ]; then
59+
echo "Hashes are different, uploading to Crowdin"
60+
echo "Uploading new strings to Crowdin"
61+
crowdin upload sources -T ${{ secrets.CROWDIN_API_KEY }}
62+
63+
# Send a message to Slack (granted we have a webhook secret).
64+
# This check also allows a repo admin to disable the Slack message by removing the secret.
65+
if [ -n "${{ secrets.TRANSLATIONS_SLACK_WEBHOOK }}" ]; then
66+
echo "Sending message to Slack (#team_translations)"
67+
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 }}
68+
fi
69+
fi
70+
71+
72+
echo "Downloading translation files from Crowdin (*.po)"
73+
crowdin download -T ${{ secrets.CROWDIN_API_KEY }}
74+
echo "Updating javascript translation files (*.js)"
75+
node ./scripts/render.js -j
76+
77+
78+
79+
if [ -z "$(git status --porcelain)" ]; then
80+
echo "Found no new translation files that need to be merged with master. Not creating a PR."
81+
else
82+
echo "Found updated translation files that need to be merged with master. Creating a PR."
83+
84+
# Commit the newly downloaded files
85+
cd $(git rev-parse --show-toplevel)
86+
git add .
87+
git commit -m "translations: 📚 sync translations with crowdin"
88+
89+
# Force push to this branch in case a previous run created it.
90+
git push --set-upstream origin "$branch_name" -f
91+
92+
sudo apt install gh
93+
gh auth login --with-token <<< ${{ secrets.PERSONAL_ACCESS_TOKEN }}
94+
gh pr close "$branch_name" || true
95+
gh pr create --fill --base "master" --head "binary-com:$branch_name"
96+
97+
echo "Attempting to approve and merge the PR."
98+
gh pr merge "$branch_name" -s
99+
echo "**The translation PR has beed merged successfully.**"
100+
fi

0 commit comments

Comments
 (0)