Skip to content

Commit

Permalink
Translations update workflow (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxushka authored Sep 2, 2023
1 parent 5812f3f commit bf6102c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/update-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Update translations

on:
schedule:
- cron: "0 */12 * * *"
workflow_dispatch:

jobs:
update-translations:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
CROWDIN_API: ${{ secrets.CROWDIN_API }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Run Python script
run: python update.py
working-directory: ./chameleonultragui/lib/l10n
- name: Check for changes
id: git-check
run: |
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git add chameleonultragui/lib/l10n/*
if [[ -z $(git status --untracked-files=no -s) ]]; then
echo "No changes"
else
echo "::set-output name=has_changes::true"
fi
- name: Create commit
uses: stefanzweifel/git-auto-commit-action@v4
if: steps.git-check.outputs.has_changes == 'true'
with:
commit_message: Update translations
branch: translations-branch
commit_options: '--no-verify'
status_options: '--untracked-files=no'
add_options: '-u'
push_options: '--force'
skip_dirty_check: true
skip_fetch: true
skip_checkout: true
disable_globbing: true
create_branch: true
- name: Create PR
if: steps.git-check.outputs.has_changes == 'true'
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
base: main
branch: translations-branch
commit-message: "New translations fetched from Crowdin"
title: "feat: Update translations"
42 changes: 42 additions & 0 deletions chameleonultragui/lib/l10n/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import os
import sys
import urllib.error
from urllib.request import Request, urlopen


def progressbar(it, prefix="", size=60, out=sys.stdout):
count = len(it)

def show(j):
x = int(size * j / count)
print(f"{prefix}[{u'█' * x}{('.' * (size - x))}] {j}/{count}", end='\r', file=out, flush=True)

show(0)
for i, item in enumerate(it):
yield item
show(i + 1)
print("\n", flush=True, file=out)


def request(method, url, data=None):
if not data:
data = {}
return json.loads(urlopen(Request(url, method=method, data=json.dumps(data).encode(),
headers={'Accept': 'application/json',
'Authorization': 'Bearer ' + os.getenv('CROWDIN_API'),
'Content-Type': 'application/json'})).read().decode())


for language in progressbar(request('GET', 'https://crowdin.com/api/v2/languages?limit=500')['data']):
try:
progress = request('GET',
f'https://crowdin.com/api/v2/projects/610545/languages/{language["data"]["id"]}/progress')
except urllib.error.HTTPError:
continue
if progress['data'][0]['data']['words']['translated']:
translation = request("POST", "https://crowdin.com/api/v2/projects/610545/translations/exports",
{"targetLanguageId": language["data"]["id"], "format": "arb-export",
"skipUntranslatedStrings": True, "fileIds": [9]})
export = urlopen(Request(translation['data']['url'], method='GET')).read()
open(f"app_{language['data']['osxLocale']}.arb", "wb+").write(export)

0 comments on commit bf6102c

Please sign in to comment.