🌐 Sync Translations #458
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: | |
permissions: | |
contents: read | |
jobs: | |
sync_translations: | |
permissions: | |
contents: write # for Git to git push | |
pull-requests: write # to create the PR with changes | |
name: 'Sync Translations with Crowdin' | |
timeout-minutes: 20 | |
runs-on: macos-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
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 '[email protected]' | |
git checkout -b i18n_sync | |
git reset --hard origin/main | |
shell: bash | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 18 | |
- 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: | |
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 { | |
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.", | |
maintainer_can_modify: true | |
}); | |
} catch(err) { | |
if (err.status === 422) { | |
console.log("A PR containing translations sync already exists!"); | |
} else { | |
throw err; | |
} | |
} |