electrum-bitcoinprivate/contrib/make_locale

84 lines
2.7 KiB
Plaintext
Raw Normal View History

2017-08-26 23:01:12 -07:00
#!/usr/bin/env python3
import os
2018-07-24 19:39:25 -07:00
import subprocess
2017-08-26 23:01:12 -07:00
import io
import zipfile
import requests
2015-02-21 03:41:52 -08:00
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir('..')
2018-07-24 19:39:25 -07:00
code_directories = 'gui plugins lib'
cmd = "find {} -type f -name '*.py' -o -name '*.kv'".format(code_directories)
files = subprocess.check_output(cmd, shell=True)
with open("app.fil", "wb") as f:
f.write(files)
print("Found {} files to translate".format(len(files.splitlines())))
2015-02-21 03:41:52 -08:00
# Generate fresh translation template
if not os.path.exists('lib/locale'):
os.mkdir('lib/locale')
2018-07-24 19:39:25 -07:00
cmd = 'xgettext -s --from-code UTF-8 --language Python --no-wrap -f app.fil --output=lib/locale/messages.pot'
2017-08-26 23:01:12 -07:00
print('Generate template')
2015-02-21 03:41:52 -08:00
os.system(cmd)
os.chdir('lib')
crowdin_identifier = 'electrum'
2018-07-24 19:39:25 -07:00
crowdin_file_name = 'files[electrum-client/messages.pot]'
2015-02-21 03:41:52 -08:00
locale_file_name = 'locale/messages.pot'
2017-05-18 12:04:09 -07:00
crowdin_api_key = None
2017-08-26 23:01:12 -07:00
2018-07-24 19:39:25 -07:00
filename = os.path.expanduser('~/.crowdin_api_key')
2017-08-26 23:01:12 -07:00
if os.path.exists(filename):
2017-11-12 05:33:46 -08:00
with open(filename) as f:
crowdin_api_key = f.read().strip()
2017-08-26 23:01:12 -07:00
2017-05-18 12:04:09 -07:00
if "crowdin_api_key" in os.environ:
crowdin_api_key = os.environ["crowdin_api_key"]
2017-08-26 23:01:12 -07:00
2017-05-18 12:04:09 -07:00
if crowdin_api_key:
2015-02-21 03:41:52 -08:00
# Push to Crowdin
2017-08-26 23:01:12 -07:00
print('Push to Crowdin')
2016-08-02 00:53:28 -07:00
url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key)
2018-07-24 19:39:25 -07:00
with open(locale_file_name, 'rb') as f:
2017-11-12 05:33:46 -08:00
files = {crowdin_file_name: f}
2018-07-24 19:39:25 -07:00
response = requests.request('POST', url, files=files)
print("", "update-file:", "-"*20, response.text, "-"*20, sep="\n")
2015-02-21 03:41:52 -08:00
# Build translations
2017-08-26 23:01:12 -07:00
print('Build translations')
2018-07-24 19:39:25 -07:00
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")
2015-02-21 03:41:52 -08:00
# Download & unzip
2017-08-26 23:01:12 -07:00
print('Download translations')
2018-07-24 19:18:46 -07:00
s = requests.request('GET', 'https://crowdin.com/backend/download/project/' + crowdin_identifier + '.zip').content
2017-08-26 23:01:12 -07:00
zfobj = zipfile.ZipFile(io.BytesIO(s))
2015-02-21 03:41:52 -08:00
2017-08-26 23:01:12 -07:00
print('Unzip translations')
2015-02-21 03:41:52 -08:00
for name in zfobj.namelist():
if not name.startswith('electrum-client/locale'):
continue
if name.endswith('/'):
if not os.path.exists(name[16:]):
os.mkdir(name[16:])
else:
2017-11-12 05:33:46 -08:00
with open(name[16:], 'wb') as output:
output.write(zfobj.read(name))
2015-02-21 03:41:52 -08:00
# Convert .po to .mo
2017-08-26 23:01:12 -07:00
print('Installing')
2015-02-21 03:41:52 -08:00
for lang in os.listdir('locale'):
if lang.startswith('messages'):
continue
# Check LC_MESSAGES folder
mo_dir = 'locale/%s/LC_MESSAGES' % lang
if not os.path.exists(mo_dir):
os.mkdir(mo_dir)
cmd = 'msgfmt --output-file="%s/electrum.mo" "locale/%s/electrum.po"' % (mo_dir,lang)
2017-08-26 23:01:12 -07:00
print('Installing', lang)
2015-02-21 03:41:52 -08:00
os.system(cmd)