Skip to content

Commit

Permalink
feat: added translations download and upload automation for crowdin
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienne-deriv committed Jul 28, 2023
1 parent ca31c81 commit 3ace8f1
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 0 deletions.
137 changes: 137 additions & 0 deletions .github/workflows/download-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
name: 'Download translations from Crowdin'

permissions:
actions: write
checks: write
contents: write
deployments: write
pull-requests: write
statuses: write


on:
repository_dispatch:
types:
- file-fully-reviewed

jobs:
synchronize-with-crowdin:
runs-on: ubuntu-latest
if: github.repository == 'binary-com/deriv-app'
steps:
- name: Checkout master branch
uses: actions/checkout@v3
with:
fetch-depth: 1
ref: master

- name: Get cached dependencies
id: cache_npm
uses: actions/cache@v3
with:
key: translations-build-${{ hashFiles('**/package-lock.json') }}
path: |
node_modules
packages/account/node_modules
packages/api/node_modules
packages/appstore/node_modules
packages/bot-skeleton/node_modules
packages/bot-web-ui/node_modules
packages/cashier/node_modules
packages/components/node_modules
packages/core/node_modules
packages/hooks/node_modules
packages/cfd/node_modules
packages/indicators/node_modules
packages/p2p/node_modules
packages/reports/node_modules
packages/shared/node_modules
packages/stores/node_modules
packages/trader/node_modules
packages/translations/node_modules
packages/utils/node_modules
packages/analytics/node_modules
- name: Install dependencies
if: ${{ steps.cache_npm.outputs.cache-hit != 'true' }}
run: |
npm run bootstrap
# In this step we're doing a couple things:
# - 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="deriv_app_translations_2"
echo "Setting up Git identity"
git config --global user.name "DerivFE"
git config --global user.email "80095553+DerivFE@users.noreply.github.com"
if [ -z "$(git ls-remote --heads origin refs/heads/$branch_name)" ]; then
echo "Found no translations branch, creating the translations branch $branch_name."
git checkout -b "$branch_name"
else
echo "Found translations branch, checking out to the translations branch $branch_name"
git fetch origin "$branch_name"
git checkout "$branch_name" -f
fi
echo "Installing Crowdin CLI"
sudo npm i -g @crowdin/cli
# Download latest translations from Deriv-app Crowdin
cd $(git rev-parse --show-toplevel)/packages/translations
echo "Attempting to download updated translations from Deriv-app Crowdin"
crowdin download -T ${{ secrets.CROWDIN_API_KEY }}
# Download latest translations from P2P Crowdin
cd $(git rev-parse --show-toplevel)/packages/p2p
echo "Attempting to download updated translations from P2P Crowdin"
crowdin download sources -T ${{ secrets.CROWDIN_API_KEY }}
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 on crowdin webhook trigger"
# Force push to this branch in case a previous run created it.
echo "Updating $branch_name branch with the updated translations..."
git push --set-upstream origin "$branch_name" -f
sudo apt install gh
gh auth login --with-token <<< ${{ github.token }}
# Check if the translations branch has already created the pull request
if ! gh pr list --head "$branch_name" --state open | grep -q "$branch_name"; then
echo "Translations pull request has not been created yet, creating one..."
pr_title="translations: 📚 sync translations on crowdin webhook trigger"
pr_body="Synchronized approved translation files based on the Crowdin webhook trigger."
gh pr create --title $pr_title --body $pr_body --base "master" --head "deriv-com:$branch_name"
fi
if [ -n "${{ secrets.SLACK_TRANSLATIONS_TEAM_WEBHOOK }}" ]; then
echo "Sending message to Slack (#project_deriv_translations)"
project="${{ github.event.client_payload.project.name }}"
pull_request_id=$(gh pr list --head "$branch_name" --state open | grep "$branch_name" | awk '{print $1}' | sed 's/#//')
pull_request="https://github.com/$GITHUB_REPOSITORY/pull/$pull_request_id"
updated_language="${{ github.event.client_payload.targetLanguage.name }}"
content="Paimon noticed that there was an update in Crowdin for a language, here are the details of the changes:\n"
content+=":file_folder: *Project*: $project\n"
content+=":flag-${{ github.event.client_payload.targetLanguage.id }}: *Updated language*: $updated_language\n"
content+=":paperclip: *Pull Request*: $pull_request\n"
content+="_Paimon will soon post the generated test link for the pull request if its successful, hold on tight!_"
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"$content\" }" ${{ secrets.SLACK_TRANSLATIONS_TEAM_WEBHOOK }}
fi
fi
14 changes: 14 additions & 0 deletions .github/workflows/generate_app_id.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ jobs:
```
</details>
- name: Retrieve pull request head branch
uses: xt0rted/pull-request-comment-branch@v2
id: comment-branch

- name: Notify translations team
if: ${{ success() && github.ref_name == 'deriv_app_translations_2' }}
run: |
content="Paimon has retrieved the generated test link for the updated translations:\n"
content+=":paperclip: *Pull Request*: ${{ steps.generate_app_id.outputs.pr_url }}\n"
content+=":link: *Test Link*: ${{ steps.vercel_preview_url.outputs.vercel_preview_url }}\n"
content+=":id: *App ID*: ${{ steps.generate_app_id.outputs.app_id }}"
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"$content\" }" ${{ secrets.SLACK_TRANSLATIONS_TEAM_WEBHOOK }}
- name: Store generated URL in artifact
run: echo "HOME_URL=${{ steps.vercel_preview_url.outputs.vercel_preview_url }}?qa_server=red.binaryws.com&app_id=${{ steps.generate_app_id.outputs.app_id }}" >> ${{ github.workspace }}/url.txt
- name: Upload artifact
Expand Down
130 changes: 130 additions & 0 deletions .github/workflows/upload-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
name: 'Upload translations to Crowdin'

permissions:
actions: write
checks: write
contents: write
deployments: write
pull-requests: write
statuses: write

on:
push:
branches:
- master

jobs:
push_and_pull_crowdin_translations:
if: github.repository == 'binary-com/deriv-app'
runs-on: ubuntu-latest
steps:
- name: Setup node
uses: actions/setup-node@v2

# We checkout the master branch so we can run the translations
# script on the latest changes.
- name: Checkout master branch
uses: actions/checkout@v2.3.4

- name: Get cached dependencies
id: cache_npm
uses: actions/cache@v3
with:
key: translations-build-${{ hashFiles('**/package-lock.json') }}
path: |
node_modules
packages/account/node_modules
packages/api/node_modules
packages/appstore/node_modules
packages/bot-skeleton/node_modules
packages/bot-web-ui/node_modules
packages/cashier/node_modules
packages/components/node_modules
packages/core/node_modules
packages/hooks/node_modules
packages/cfd/node_modules
packages/indicators/node_modules
packages/p2p/node_modules
packages/reports/node_modules
packages/shared/node_modules
packages/stores/node_modules
packages/trader/node_modules
packages/translations/node_modules
packages/utils/node_modules
packages/analytics/node_modules
- name: Install dependencies
if: ${{ steps.cache_npm.outputs.cache-hit != 'true' }}
run: |
npm run bootstrap
# In this step we're doing a couple things:
# - We generate a new messages.json
# - We hash the newly generated messages.json and compare it with the messages.json 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="deriv_app_translations_2"
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 "Checking out new branch [$branch_name]"
# git checkout -b "$branch_name"
echo "Running the translation script (extract-translations.js)"
cd $(git rev-parse --show-toplevel)/packages/translations/scripts
node extract-translations.js
new_messages_json_hash="$(git hash-object $(git rev-parse --show-toplevel)/packages/translations/crowdin/messages.json)"
echo "- [generated]: message.json hash is $new_messages_json_hash"
echo "Downloading messages.json from Crowdin for comparison"
cd $(git rev-parse --show-toplevel)/packages/translations
crowdin download sources -T ${{ secrets.CROWDIN_API_KEY }}
crowdin_messages_json_hash="$(git hash-object messages.json)"
echo "- [crowdin]: message.json hash is $crowdin_messages_json_hash"
rm messages.json
echo "Running the translation script (extract-translations.js) in p2p"
cd $(git rev-parse --show-toplevel)/packages/p2p/scripts
node extract-translations.js
new_p2p_messages_json_hash="$(git hash-object $(git rev-parse --show-toplevel)/packages/p2p/crowdin/messages.json)"
echo "- [generated]: message.json hash is $new_p2p_messages_json_hash"
echo "Downloading messages.json from Crowdin for comparison"
cd $(git rev-parse --show-toplevel)/packages/p2p
crowdin download sources -T ${{ secrets.CROWDIN_API_KEY }}
crowdin_p2p_messages_json_hash="$(git hash-object messages.json)"
echo "- [crowdin]: message.json hash is $crowdin_p2p_messages_json_hash"
rm messages.json
# We compare the generated messages.json with the messages.json from Crowdin.
# Only send a Slack message and upload it to Crowdin if there were any changes made to messages.json.
if [ "$crowdin_messages_json_hash" != "$new_messages_json_hash" ]; then
echo "Hashes are different, uploading to Crowdin"
echo "- [crowdin]: $crowdin_messages_json_hash";
echo "- [generated]: $new_messages_json_hash";
# Upload to Crowdin.
cd $(git rev-parse --show-toplevel)/packages/translations
echo "Uploading new strings to Crowdin"
crowdin upload sources -T ${{ secrets.CROWDIN_API_KEY }}
fi
# We compare the generated messages.json with the messages.json from Crowdin in p2p.
# Only send a Slack message and upload it to Crowdin if there were any changes made to messages.json.
if [ "$crowdin_p2p_messages_json_hash" != "$new_p2p_messages_json_hash" ]; then
echo "Hashes are different, uploading to Crowdin"
echo "- [crowdin]: $crowdin_p2p_messages_json_hash";
echo "- [generated]: $new_p2p_messages_json_hash";
# Upload to Crowdin.
cd $(git rev-parse --show-toplevel)/packages/p2p
echo "Uploading new strings to Crowdin"
crowdin upload sources -T ${{ secrets.CROWDIN_API_KEY }}
fi

0 comments on commit 3ace8f1

Please sign in to comment.