Skip to content

Commit

Permalink
Rewrite updateCrowdin (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxushka authored Oct 10, 2023
1 parent ce07728 commit 34626e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/add-crowdin-keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Watching for changes in app_en.arb and run Python script
- name: Update translation
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"
python chameleonultragui/lib/l10n/updateCrowdin.py
else
echo "The file 'app_en.arb' has not changed."
fi
69 changes: 47 additions & 22 deletions chameleonultragui/lib/l10n/updateCrowdin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os
import json
import urllib
from urllib.request import Request, urlopen

def progressbar(it, prefix='', size=60, out=sys.stdout):
Expand All @@ -24,30 +25,54 @@ def request(method, url, data=None):
headers={'Accept': 'application/json',
'Authorization': 'Bearer ' + str(os.getenv('CROWDIN_API')),
'Content-Type': 'application/json'})).read().decode())
def fetch(url):
return json.loads(urlopen(Request(url, method='GET')).read().decode())

def add_string_to_source(string):
print(string)
if __name__ == '__main__':
projectId = 611911
sourceId = 33

try:
string_parsed = string.split('app_en.arb', 1)[1].replace(' ', '').replace(',', '').replace('\n','').split('+')[1:]
for s in string_parsed:
current_translation = fetch('https://raw.githubusercontent.com/GameTec-live/ChameleonUltraGUI/main/chameleonultragui/lib/l10n/app_en.arb')
branch_translation = json.load(open('chameleonultragui/lib/l10n/app_en.arb'))
strings = request('GET', f'https://api.crowdin.com/api/v2/projects/{projectId}/strings?limit=500')

for key, value in branch_translation.items():
failed = False
if key not in current_translation.keys():
try:
key, value = s.split(':')
data = {'identifier': key.replace('"', ''), 'text': value.replace('"', ''), 'fileId': sourceId}
request('POST', f"https://api.crowdin.com/api/v2/projects/{projectId}/strings", data)
print("Added: ", key)
except Exception as e:
print(e)

except Exception as e:
print(e)

if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python updateCrowdin.py <string>")
sys.exit(1)

string_to_be_added = sys.argv[1]
add_string_to_source(string_to_be_added)
data = {'identifier': key, 'text': value, 'fileId': sourceId}
string = request('POST', f'https://api.crowdin.com/api/v2/projects/{projectId}/strings', data)
data = {'stringId': string['data']['id'], 'languageId': 'en', 'text': value}
translation = request('POST', f'https://api.crowdin.com/api/v2/projects/{projectId}/translations', data)
data = {'translationId': translation['data']['id']}
translation = request('POST', f'https://api.crowdin.com/api/v2/projects/{projectId}/approvals', data)
except urllib.error.HTTPError as e:
failed = True
print(e.reason)
if failed or (key in current_translation.keys() and current_translation[key] != value):
for string in strings['data']:
if string['data']['identifier'] == key:
try:
data = [{'op': 'replace', 'path': '/text', 'value': value}]
string = request('PATCH', f'https://api.crowdin.com/api/v2/projects/{projectId}/strings/' + str(string['data']['id']), data)
data = {'stringId': string['data']['id'], 'languageId': 'en', 'text': value}
translation = request('POST', f'https://api.crowdin.com/api/v2/projects/{projectId}/translations', data)
data = {'translationId': translation['data']['id']}
translation = request('POST', f'https://api.crowdin.com/api/v2/projects/{projectId}/approvals', data)
except urllib.error.HTTPError as e:
print(e.reason)

# remove old strings
to_remove = []
for key, value in current_translation.items():
if key not in branch_translation.keys():
to_remove.append(key)

for string in strings['data']:
if string['data']['identifier'] not in branch_translation.keys():
to_remove.append(key)

for remove in to_remove:
for string in strings['data']:
if string['data']['identifier'] == remove:
request('DELETE', f'https://api.crowdin.com/api/v2/projects/{projectId}/strings/' + str(string['data']['id']))

0 comments on commit 34626e1

Please sign in to comment.