contrib/push_locale: start using crowdin v2 API (instead of v1)
----- closes https://github.com/spesmilo/electrum/issues/6936 Note: if we used the python crowdin client (https://github.com/crowdin/crowdin-api-client-python), we would do something like this: ``` from crowdin_api import CrowdinClient class MyCrowdinClient(CrowdinClient): TOKEN = ... client = MyCrowdinClient() with open(locale_file_name, 'rb') as f: resp = client.storages.add_storage(f) storage_id = resp['data']['id'] client.source_files.update_file(projectId=crowdin_project_id, storageId=storage_id, fileId=crowdin_file_id) client.translations.build_crowdin_project_translation(projectId=crowdin_project_id) ```
This commit is contained in:
@@ -81,6 +81,7 @@ task:
|
|||||||
env:
|
env:
|
||||||
ELECTRUM_IMAGE: python:3.7
|
ELECTRUM_IMAGE: python:3.7
|
||||||
ELECTRUM_REQUIREMENTS: contrib/requirements/requirements-travis.txt
|
ELECTRUM_REQUIREMENTS: contrib/requirements/requirements-travis.txt
|
||||||
|
# in addition, crowdin_api_key is set as an "override" in https://cirrus-ci.com/settings/...
|
||||||
depends_on:
|
depends_on:
|
||||||
- Tox Python 3.9
|
- Tox Python 3.9
|
||||||
only_if: $CIRRUS_BRANCH == 'master'
|
only_if: $CIRRUS_BRANCH == 'master'
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import io
|
|
||||||
import zipfile
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -25,15 +23,12 @@ print("Found {} files to translate".format(len(files.splitlines())))
|
|||||||
# Generate fresh translation template
|
# Generate fresh translation template
|
||||||
if not os.path.exists('electrum/locale'):
|
if not os.path.exists('electrum/locale'):
|
||||||
os.mkdir('electrum/locale')
|
os.mkdir('electrum/locale')
|
||||||
print('Generate template')
|
print('Generating template...')
|
||||||
cmd = 'xgettext -s --from-code UTF-8 --language Python --no-wrap -f app.fil --output=electrum/locale/messages.pot'
|
cmd = 'xgettext -s --from-code UTF-8 --language Python --no-wrap -f app.fil --output=electrum/locale/messages.pot'
|
||||||
subprocess.check_output(cmd, shell=True)
|
subprocess.check_output(cmd, shell=True)
|
||||||
|
|
||||||
os.chdir('electrum')
|
os.chdir('electrum')
|
||||||
|
|
||||||
crowdin_identifier = 'electrum'
|
|
||||||
crowdin_file_name = 'files[electrum-client/messages.pot]'
|
|
||||||
locale_file_name = 'locale/messages.pot'
|
|
||||||
crowdin_api_key = None
|
crowdin_api_key = None
|
||||||
|
|
||||||
filename = os.path.expanduser('~/.crowdin_api_key')
|
filename = os.path.expanduser('~/.crowdin_api_key')
|
||||||
@@ -44,16 +39,36 @@ if os.path.exists(filename):
|
|||||||
if "crowdin_api_key" in os.environ:
|
if "crowdin_api_key" in os.environ:
|
||||||
crowdin_api_key = os.environ["crowdin_api_key"]
|
crowdin_api_key = os.environ["crowdin_api_key"]
|
||||||
|
|
||||||
if crowdin_api_key:
|
if not crowdin_api_key:
|
||||||
# Push to Crowdin
|
print('Missing crowdin_api_key. Cannot push.')
|
||||||
print('Push to Crowdin')
|
sys.exit(0)
|
||||||
url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key)
|
print('Found crowdin_api_key. Will push updated source-strings to crowdin.')
|
||||||
with open(locale_file_name, 'rb') as f:
|
|
||||||
files = {crowdin_file_name: f}
|
|
||||||
response = requests.request('POST', url, files=files)
|
|
||||||
print("", "update-file:", "-"*20, response.text, "-"*20, sep="\n")
|
|
||||||
# Build translations
|
|
||||||
print('Build translations')
|
|
||||||
response = requests.request('GET', 'https://api.crowdin.com/api/project/' + crowdin_identifier + '/export?key=' + crowdin_api_key)
|
|
||||||
print("", "export:", "-" * 20, response.text, "-" * 20, sep="\n")
|
|
||||||
|
|
||||||
|
crowdin_project_id = 20482 # for "Electrum" project on crowdin
|
||||||
|
locale_file_name = "locale/messages.pot"
|
||||||
|
crowdin_file_name = "messages.pot"
|
||||||
|
crowdin_file_id = 68 # for "/electrum-client/messages.pot"
|
||||||
|
global_headers = {"Authorization": "Bearer {}".format(crowdin_api_key)}
|
||||||
|
|
||||||
|
# client.storages.add_storage(f)
|
||||||
|
print(f"Uploading to temp storage...")
|
||||||
|
url = f'https://api.crowdin.com/api/v2/storages'
|
||||||
|
with open(locale_file_name, 'rb') as f:
|
||||||
|
headers = {**global_headers, **{"Crowdin-API-FileName": crowdin_file_name}}
|
||||||
|
response = requests.request("POST", url, data=f, headers=headers)
|
||||||
|
print("", "storages.add_storage:", "-" * 20, response.text, "-" * 20, sep="\n")
|
||||||
|
storage_id = response.json()["data"]["id"]
|
||||||
|
|
||||||
|
# client.source_files.update_file(projectId=crowdin_project_id, storageId=storage_id, fileId=crowdin_file_id)
|
||||||
|
print(f"Copying from temp storage and updating file in perm storage...")
|
||||||
|
url = f'https://api.crowdin.com/api/v2/projects/{crowdin_project_id}/files/{crowdin_file_id}'
|
||||||
|
headers = {**global_headers, **{"content-type": "application/json"}}
|
||||||
|
response = requests.request("PUT", url, json={"storageId": storage_id}, headers=headers)
|
||||||
|
print("", "source_files.update_file:", "-" * 20, response.text, "-" * 20, sep="\n")
|
||||||
|
|
||||||
|
# client.translations.build_crowdin_project_translation(projectId=crowdin_project_id)
|
||||||
|
print(f"Rebuilding translations...")
|
||||||
|
url = f'https://api.crowdin.com/api/v2/projects/{crowdin_project_id}/translations/builds'
|
||||||
|
headers = {**global_headers, **{"content-type": "application/json"}}
|
||||||
|
response = requests.request("POST", url, headers=headers)
|
||||||
|
print("", "translations.build_crowdin_project_translation:", "-" * 20, response.text, "-" * 20, sep="\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user