Skip to content

Commit

Permalink
Crowdin Add Automation (#206)
Browse files Browse the repository at this point in the history
Automatically sync changes to the "app_en.arb" to crowdin
  • Loading branch information
aruznieto authored Sep 3, 2023
1 parent cc1d1cb commit 9db108d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/add-crowdin-keys.yml
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions chameleonultragui/lib/l10n/updateCrowdin.py
Original file line number Diff line number Diff line change
@@ -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 <cadena_a_agregar>")
sys.exit(1)

string_to_be_added = sys.argv[1]
add_string_to_source(string_to_be_added)

0 comments on commit 9db108d

Please sign in to comment.