Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: Create the Github Action for Sync Translations with POEditor #2672

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fcb2c90
feat(*): create the workflow for the manage translation poedior integ…
Ayush8923 Nov 22, 2024
419f5fc
fix(*): update the github secrets and pull request script
Ayush8923 Nov 24, 2024
cd232e8
fix(*): update the branch name in translation workflow
Ayush8923 Nov 24, 2024
d0bed4f
fix(*): update the workflow if the PR is already open then don't crea…
Ayush8923 Nov 24, 2024
0a6cd7b
fix(*): update the scripting sequence
Ayush8923 Nov 24, 2024
8e2d27c
fix(*): added the check workflow only run if in the any language file…
Ayush8923 Nov 24, 2024
4221335
fix(*): added the overwrite and sync terms params in the upload api
Ayush8923 Nov 25, 2024
969f61c
fix(*): update the file name of the sync translation workflow
Ayush8923 Nov 25, 2024
cffdbf7
fix(*): remove the condition for testing
Ayush8923 Nov 25, 2024
21c9a33
fix(*): run the workflow only for specific path
Ayush8923 Nov 25, 2024
a5a2b63
fix(*): update the automated PR description
Ayush8923 Nov 25, 2024
059974f
fix(*): remove the condition for testing
Ayush8923 Nov 25, 2024
e8e72aa
fix(*): added the check workflow only run if in the any language file…
Ayush8923 Nov 25, 2024
4efc8a5
fix(*): update the automated PR branch name
Ayush8923 Nov 25, 2024
9e28d8b
fix(*): update the workflow indetation
Ayush8923 Nov 25, 2024
e00db25
fix(*): update the workflow and update the PR description
Ayush8923 Nov 25, 2024
f07c90a
fix(*): update the branch name for testing the workflow
Ayush8923 Nov 25, 2024
f2d33f1
fix(*): update the branch name for testing the workflow
Ayush8923 Nov 25, 2024
acad05d
fix(*): update the branch name for testing the workflow
Ayush8923 Nov 25, 2024
4ea854a
fix(*): added the check workflow only run if in the any language file…
Ayush8923 Nov 25, 2024
d1652b2
fix(*): remove the condition for testing the workflow
Ayush8923 Nov 25, 2024
2df7083
fix(*): added the check workflow only run if in the any language file…
Ayush8923 Nov 25, 2024
0975b47
Merge branch 'master' into feat/translation-poeditor-integration
Ayush8923 Nov 25, 2024
1b24909
Merge branch 'master' into feat/translation-poeditor-integration
Ayush8923 Nov 25, 2024
a0459d6
Merge branch 'master' into feat/translation-poeditor-integration
Ayush8923 Nov 25, 2024
2e8e192
fix(*): update the translation workflow
Ayush8923 Nov 25, 2024
732810c
Merge branch 'feat/translation-poeditor-integration' of https://githu…
Ayush8923 Nov 25, 2024
36a0e6b
fix(*): added the fi to end of block
Ayush8923 Nov 25, 2024
37cef8c
debug(*): added the console to debug why the PR is not automatic open
Ayush8923 Nov 25, 2024
629d404
fix(*): update the github pull reuest list api
Ayush8923 Nov 25, 2024
1e4700f
fix(*): update the automated PR branch name and remvoe the debugging …
Ayush8923 Nov 25, 2024
9af3ea9
Merge branch 'master' into feat/translation-poeditor-integration
Ayush8923 Nov 25, 2024
c53cf8b
Merge branch 'master' into feat/translation-poeditor-integration
Ayush8923 Nov 26, 2024
2476369
fix(*): update the changes according to the reviewers suggestions
Ayush8923 Nov 27, 2024
c9197b0
Merge branch 'feat/translation-poeditor-integration' of https://githu…
Ayush8923 Nov 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions .github/workflows/sync-translations-poeditor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
name: Sync Translations with POEditor

on:
push:
branches:
- feat/translation-poeditor-integration
Comment on lines +5 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the branch name before merging this PR to the master.


permissions:
contents: write
pull-requests: write

jobs:
sync-translations:
runs-on: ubuntu-latest
env:
BASE_BRANCH: "master"

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4

- name: Install dependencies
run: npm ci

- name: Configure Git
run: |
git config user.name ${{ secrets.GIT_BOT_USERNAME }}
git config user.email ${{ secrets.GIT_BOT_EMAIL }}
git remote set-url origin https://${{ secrets.GIT_BOT_ACCESS_TOKEN }}@github.com/Aam-Digital/ndb-core.git

- name: Create or update branch for translations
run: |
branch_name="chore/update-poeditor-translations"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this action supposed to run? On each PR? Then a fixed branch name would overwrite changes from other PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this action will run after every commit on the master branch. I believe we don’t need to create another PR if the translation automated PR has already been created. Instead, we can update the language files in the same PR. What do you suggest in this?


# Check if branch exists on remote
if git ls-remote --exit-code origin $branch_name; then
echo "Branch $branch_name already exists. Fetching it..."
git fetch origin $branch_name:$branch_name

git checkout $branch_name
git pull origin $branch_name
else
echo "Creating new branch $branch_name..."
git checkout -b $branch_name
fi

- name: Extract i18n keys
run: npm run extract-i18n

- name: Check if any files changed
run: |
if git diff --quiet; then
echo "No changes detected."
echo "changed=false" >> $GITHUB_ENV
else
echo "Changes detected."
echo "changed=true" >> $GITHUB_ENV
fi
Comment on lines +53 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change detection could also be handled at Github Action level:

on:
  push:
    paths:
      - 'src/assets/locale/*.xlf'
    branches:
      - feat/translation-poeditor-integration
      

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomwwinter Yes, this can be managed at the GitHub Action level, and I have implemented it that way before. However, following Sebastian's suggestion, we can handle this through CI. For reference, please check this comments. Please let me know, if I need to changes this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, missed that conversation.


- name: Commit file changes
if: env.changed == 'true'
run: |
git add src/assets/locale/*.xlf
git commit -m "Update translations: Extracted i18n keys"

- name: Push changes to master
if: env.changed == 'true'
run: git push origin HEAD

- name: Upload translations to POEditor
if: env.changed == 'true'
run: |
declare -A files

# Populate file mappings dynamically
files["en"]="src/assets/locale/messages.xlf"
files["de"]="src/assets/locale/messages.de.xlf"
files["it"]="src/assets/locale/messages.it.xlf"
files["fr"]="src/assets/locale/messages.fr.xlf"

for lang in "${!files[@]}"; do
echo "Uploading ${files[$lang]} for language $lang"
response=$(curl -s -X POST https://api.poeditor.com/v2/projects/upload \
-F "api_token=${{ secrets.POEDITOR_API_TOKEN }}" \
-F "id=${{ secrets.POEDITOR_PROJECT_ID }}" \
-F "language=$lang" \
-F "updating=terms_translations" \
-F "overwrite=1" \
-F "sync_terms=1" \
-F "file=@${files[$lang]}")
status=$(echo $response | jq -r '.response.status')
if [ "$status" != "success" ]; then
echo "Error uploading file for $lang: $response"
exit 1
fi
Comment on lines +86 to +98
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the POEditor upload API, where need to send the language xlf file. For reference, here is the documentation of the POEditor Upload API.

echo "Waiting for 30 seconds to respect rate limits..."
sleep 30
done

- name: Export translations from POEditor
if: env.changed == 'true'
run: |
languages=("en" "fr" "it" "de")
for lang in "${languages[@]}"; do
echo "Exporting translations for language $lang"
response=$(curl -s -X POST https://api.poeditor.com/v2/projects/export \
-F "api_token=${{ secrets.POEDITOR_API_TOKEN }}" \
-F "id=${{ secrets.POEDITOR_PROJECT_ID }}" \
-F "language=$lang" \
-F "type=xlf")
status=$(echo $response | jq -r '.response.status')
if [ "$status" == "success" ]; then
url=$(echo $response | jq -r '.result.url')
if [ -n "$url" ]; then
echo "Downloading translations for $lang from $url"
if [ "$lang" == "en" ]; then
curl -s -o src/assets/locale/messages.xlf $url
else
curl -s -o src/assets/locale/messages.$lang.xlf $url
fi
else
echo "No URL found for downloading translations for $lang"
exit 1
fi
Comment on lines +109 to +127
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the POEditor export API, where we can export the updated language files. For reference, here is the documentation of the POEditor Export API.

else
echo "Error exporting translations for $lang: $response"
exit 1
fi
echo "Waiting for 30 seconds to respect rate limits..."
sleep 30
done

- name: Commit updated translations
if: env.changed == 'true'
run: |
git add src/assets/locale/*.xlf
git commit -m "translations: load latest translations from POEditor"

- name: Push changes
if: env.changed == 'true'
run: |
git push origin HEAD

- name: Create Pull Request
if: env.changed == 'true'
uses: actions/github-script@v6
with:
script: |
const { repo, owner } = context.repo;
const headBranch = 'chore/update-poeditor-translations';
const baseBranch = process.env.BASE_BRANCH;

const pulls = await github.rest.pulls.list({
owner,
repo,
state: 'open',
head: `${owner}:${headBranch}`,
});

if (pulls.data.length < 1) {
const diff = await github.rest.repos.compareCommits({
owner,
repo,
base: baseBranch,
head: headBranch,
});

if (diff.data.commits.length === 0) {
return;
}
const result = await github.rest.pulls.create({
title: 'Update POEditor translations messages',
owner,
repo,
head: headBranch,
base: baseBranch,
body: [
'This PR updates translations from POEditor.',
'Generated by GitHub Actions.'
].join('\n'),
});
await github.rest.issues.addLabels({
owner,
repo,
issue_number: result.data.number,
labels: ['automated pr'],
});
} else {
const existingPR = pulls.data[0];
await github.rest.pulls.update({
owner: owner,
repo: repo,
pull_number: existingPR.number,
body: [
existingPR.body,
`Updated by Job ${context.job}`,
].join('\n'),
});
}
Loading