Sync Translations #760
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync Translations | |
on: | |
workflow_dispatch: | |
# We use a machine account PAT from secrets so workflows are triggered | |
# the default token is not needed and should be fully restricted | |
permissions: {} | |
jobs: | |
sync_translations: | |
name: 'Sync Translations with Crowdin' | |
timeout-minutes: 20 | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.MACHINE_ACCOUNT_PAT }} | |
ref: 'main' | |
fetch-depth: 0 | |
- name: Credential Prep | |
run: | | |
echo "CROWDIN_APIv2_PAT=${{ secrets.CROWDIN_APIv2_PAT }}" >> $GITHUB_ENV | |
shell: bash | |
- name: GIT Setup | |
run: | | |
git config --global user.name 'AnkiDroid Translations' | |
git config --global user.email 'ankidroid@ankidroid.org' | |
git checkout -b i18n_sync | |
git reset --hard origin/main | |
shell: bash | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Install dependencies and build | |
run: | | |
cd ./tools/localization | |
yarn | |
yarn build | |
- name: Push translation sources to crowdin | |
run: | | |
cd ./tools/localization | |
yarn start upload | |
- name: Pull translation updates from crowdin | |
run: | | |
cd ./tools/localization | |
yarn start download | |
- name: Extract downloaded ankidroid.zip file | |
run: | | |
cd ./tools/localization | |
yarn start extract | |
- name: Update translation to AnkiDroid res | |
run: | | |
cd ./tools/localization | |
yarn start update | |
- name: Commit changes | |
run: | | |
git add docs/marketing/localized_description AnkiDroid/src/main/res | |
git commit -am 'Updated strings from Crowdin' | |
git push --set-upstream origin +i18n_sync | |
echo "The results of the sync are on the i18n_sync branch, PR them from there for merge." | |
echo "https://github.com/ankidroid/Anki-Android/compare/i18n_sync?expand=1" | |
- name: Check if there are strings changes | |
id: tr_check | |
run: | | |
git checkout i18n_sync | |
COMMITS_COUNT=`git rev-list --count HEAD ^main` | |
if [ "$COMMITS_COUNT" -gt 0 ]; then | |
echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT | |
else | |
echo "HAS_CHANGES=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Create PR for strings changes if needed | |
if: ${{ steps.tr_check.outputs.HAS_CHANGES }} | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.MACHINE_ACCOUNT_PAT }} | |
script: | | |
const now = new Date(); | |
// Date format used: YYYY/MM/DD HH:MM , UTC time(ex: 2023/01/13 08:38) | |
const formattedDate = | |
now.getUTCFullYear() + "/" + | |
("0" + (now.getUTCMonth() + 1)).slice(-2) + "/" + | |
("0" + now.getUTCDate()).slice(-2) + " " + | |
("0" + now.getUTCHours()).slice(-2) + ":" + | |
("0" + now.getUTCMinutes()).slice(-2); | |
try { | |
await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: "Updated strings from Crowdin " + formattedDate, | |
head: "i18n_sync", | |
base: "main", | |
body: "Contains the newest strings changes from Crowdin.\n\nhttps://crowdin.com/project/ankidroid/activity-stream", | |
maintainer_can_modify: true | |
}); | |
} catch(err) { | |
if (err.status === 422) { | |
console.log("A PR containing translations sync already exists!"); | |
} else { | |
console.log("Unexpected error creating pull request: " + err); | |
throw err; | |
} | |
} |