forked from spesmilo/electrum
/
make_locale
executable file
·105 lines (91 loc) · 3.39 KB
/
make_locale
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import os
from os.path import isdir, join
import sys
import io
import zipfile
import requests
import glob
import itertools
from pathlib import Path
assert len(sys.argv) < 3
original_dir = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir('..')
code_directories = [
'android/app/src/main/python',
'electroncash_gui',
'ios/ElectronCash/electroncash_gui', # ios/ElectronCash could contain duplicate files
# copied by make_ios_project.sh.
'electroncash',
'electroncash_plugins',
]
file_types = ('*.py', '*.kv')
files = zip(
itertools.count(start=1),
itertools.chain.from_iterable(
glob.iglob(f'{code_directory}/**/{file_type}', recursive=True)
for code_directory in code_directories
for file_type in file_types
)
)
with open("app.fil", "w") as f:
for n, file in files:
f.write(f"{Path(file)}\n")
else:
print("Found {} files to translate".format(n))
# Generate fresh translation template
if not os.path.exists('electroncash/locale'):
os.mkdir('electroncash/locale')
cmd = 'xgettext -s --from-code UTF-8 --language Python --no-wrap -f app.fil --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 -c --output=electroncash/locale/messages.pot'
print('Generate template')
os.system(cmd)
os.chdir('electroncash')
crowdin_identifier = 'electron-cash'
crowdin_file_name = 'files[electron-cash-client/messages.pot]'
locale_file_name = 'locale/messages.pot'
crowdin_api_key = None
filename = os.path.expanduser('~/.crowdin_api_key')
if os.path.exists(filename):
with open(filename) as f:
crowdin_api_key = f.read().strip()
if "crowdin_api_key" in os.environ:
crowdin_api_key = os.environ["crowdin_api_key"]
if crowdin_api_key:
# Push to Crowdin
print('Push to Crowdin')
url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key)
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")
if len(sys.argv) < 2:
# Download & unzip
s = requests.request('GET', 'https://crowdin.com/backend/download/project/' + crowdin_identifier + '.zip').content
zfobj = zipfile.ZipFile(io.BytesIO(s))
else:
zfobj = zipfile.ZipFile(os.path.join(os.path.relpath(original_dir, os.getcwd()), sys.argv[1]))
print('Unzip translations')
for name in zfobj.namelist():
if name.endswith('/'):
if not os.path.exists(name):
os.mkdir(name)
else:
with open(name, 'wb') as output:
output.write(zfobj.read(name))
# Convert .po to .mo
print('Installing')
for lang in os.listdir('locale'):
if not isdir(join('locale', lang)) or lang == "__pycache__":
continue
# Check LC_MESSAGES folder
msg_dir = 'locale/{}/LC_MESSAGES'.format(lang)
if not os.path.exists(msg_dir):
os.mkdir(msg_dir)
cmd = 'msgfmt --output-file="{0}/electron-cash.mo" "{0}/electron-cash.po"'.format(msg_dir)
print('Installing', lang)
os.system(cmd)