|
| 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