From 9db108d9b5709ddf7d52c0557e10642b2bae32b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Ruz=20Nieto?= <40019177+aruznieto@users.noreply.github.com> Date: Sun, 3 Sep 2023 20:30:38 +0200 Subject: [PATCH] Crowdin Add Automation (#206) Automatically sync changes to the "app_en.arb" to crowdin --- .github/workflows/add-crowdin-keys.yml | 33 +++++++++++++ chameleonultragui/lib/l10n/updateCrowdin.py | 52 +++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 .github/workflows/add-crowdin-keys.yml create mode 100644 chameleonultragui/lib/l10n/updateCrowdin.py diff --git a/.github/workflows/add-crowdin-keys.yml b/.github/workflows/add-crowdin-keys.yml new file mode 100644 index 00000000..7df59a34 --- /dev/null +++ b/.github/workflows/add-crowdin-keys.yml @@ -0,0 +1,33 @@ +name: Add Crowdin Translation Strings + +on: + pull_request: + branches: [ main ] + types: + - closed + +jobs: + check_changes: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + env: + CROWDIN_API: ${{ secrets.CROWDIN_API }} + steps: + - name: Check out code + uses: actions/checkout@v3 + with: + fetch-depth: 2 + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: 3.11 + - name: Watching for changes in app_en.arb and run Python script + run: | + CHANGED_FILES=$(git diff --name-only -r HEAD^1 HEAD) + # Check if the specific file is in the changed files list + if echo "$CHANGED_FILES" | grep -q 'app_en.arb'; then + FILE=$(git diff HEAD^1:chameleonultragui/lib/l10n/app_en.arb HEAD:chameleonultragui/lib/l10n/app_en.arb --no-ext-diff --unified=0 -a --no-prefix | egrep "^\+") + python chameleonultragui/lib/l10n/updateCrowdin.py "$FILE" + else + echo "The file 'app_en.arb' has not changed." + fi diff --git a/chameleonultragui/lib/l10n/updateCrowdin.py b/chameleonultragui/lib/l10n/updateCrowdin.py new file mode 100644 index 00000000..0ab5746b --- /dev/null +++ b/chameleonultragui/lib/l10n/updateCrowdin.py @@ -0,0 +1,52 @@ +import sys +import os +import json +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 ' + str(os.getenv('CROWDIN_API')), + 'Content-Type': 'application/json'})).read().decode()) + +def add_string_to_source(string): + projectId = 611911 + sourceId = 33 + + try: + string_parsed = string.split('+++ chameleonultragui/lib/l10n/app_en.arb ', 1)[1].replace(' ', '').replace(',', '').replace('\n','').split('+')[1:] + for s in string_parsed: + data = {'identifier': key.replace('"', ''), 'text': value.replace('"', ''), 'fileId': sourceId} + try: + key, value = s.split(':') + request('POST', f"https://api.crowdin.com/api/v2/projects/{projectId}/strings", data) + print("Added: ", key) + except Exception as e: + print(e) + + except: + print(e) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Uso: python script.py ") + sys.exit(1) + + string_to_be_added = sys.argv[1] + add_string_to_source(string_to_be_added)