-
Notifications
You must be signed in to change notification settings - Fork 41
/
make_messages.py
executable file
·106 lines (90 loc) · 3.28 KB
/
make_messages.py
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
106
#!/usr/bin/env python
import os
import sys
import subprocess
from lxml import etree
import logging
import ajenti.log
def check_call(*args):
try:
subprocess.call(*args)
except Exception as e:
logging.error('Call failed')
logging.error(' '.join(args[0]))
logging.error(str(e))
ajenti.log.init()
LOCALEDIR = 'ajenti/locales'
LANGUAGES = [x for x in os.listdir(LOCALEDIR) if not '.' in x]
pot_path = os.path.join(LOCALEDIR, 'ajenti.po')
if len(sys.argv) != 2:
logging.error('Usage: ./make_messages.py [extract|compile]')
sys.exit(1)
if subprocess.call(['which', 'xgettext']) != 0:
logging.error('xgettext app not found')
sys.exit(0)
if sys.argv[1] == 'extract':
os.unlink(pot_path)
for (dirpath, dirnames, filenames) in os.walk('ajenti', followlinks=True):
if '/custom_' in dirpath:
continue
if '/elements' in dirpath:
continue
for f in filenames:
path = os.path.join(dirpath, f)
if f.endswith('.py'):
logging.info('Extracting from %s' % path)
check_call([
'xgettext',
'-c',
'--from-code=utf-8',
'--omit-header',
'-o', pot_path,
'-j' if os.path.exists(pot_path) else '-dajenti',
path,
])
if f.endswith('.xml'):
logging.info('Extracting from %s' % path)
content = open(path).read()
xml = etree.fromstring('<xml xmlns:bind="bind" xmlns:binder="binder">' + content + '</xml>')
try:
msgs = []
def traverse(n):
for k, v in n.items():
if v.startswith('{') and v.endswith('}'):
msgs.append(v[1:-1])
try:
if "_('" in v:
eval(v, {'_': msgs.append})
except:
pass
for c in n:
traverse(c)
traverse(xml)
fake_content = ''.join('gettext("%s");\n' % msg for msg in msgs)
fake_content = 'void main() { ' + fake_content + ' }'
open(path, 'w').write(fake_content)
check_call([
'xgettext',
'-C',
'--from-code=utf-8',
'--omit-header',
'-o', pot_path,
'-j' if os.path.exists(pot_path) else '-dajenti',
path,
])
finally:
open(path, 'w').write(content)
if sys.argv[1] == 'compile':
for lang in LANGUAGES:
po_dir = os.path.join(LOCALEDIR, lang, 'LC_MESSAGES')
po_path = os.path.join(po_dir, 'ajenti.po')
mo_path = os.path.join(po_dir, 'ajenti.mo')
if not os.path.exists(po_dir):
os.makedirs(po_dir)
logging.info('Compiling %s' % lang)
check_call([
'msgfmt',
po_path,
'-v',
'-o', mo_path
])