-
Notifications
You must be signed in to change notification settings - Fork 23
/
make_locale.py
74 lines (64 loc) · 2.63 KB
/
make_locale.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
''' This script constructs the language resources we need. '''
import re
import glob
import os
UUID = "[email protected]"
def get_network_settings_translations():
''' Read the network settings translations for all languages. '''
infile = "lang-resources/gnome-network-panel.desktop"
with open(infile, 'r') as f:
fdata = f.read()
return dict(re.findall("^Comment\[(.*)\]=(.*)$", fdata,
flags=re.MULTILINE))
def get_control_center_translations(lang):
''' Read the control center translations for a given language. '''
infile = "lang-resources/po-gnome-control-center/{}.po".format(lang)
with open(infile, 'r') as f:
fdata = f.read()
pattern = 'msgid "([^"]*)"\nmsgstr "([^"]*)"\n'
raw_translations = re.findall(pattern, fdata, flags=re.MULTILINE)
raw_translations = dict(raw_translations)
translations = {}
for k in ['Off', 'Manual', 'Automatic', 'Proxy']:
translation = raw_translations.get(k)
if translation is None:
print("No translation for {} in {}".format(k, lang))
translations[k] = k
elif translation == "":
# Empty translation string => use key text.
translations[k] = k
else:
translations[k] = translation
return translations
def make_all_po_files():
''' Populates the directory po. '''
# create the target if necessary.
if not os.path.exists('po'):
os.makedirs('po')
# all po files are based on templates
with open("po.template", 'r') as f:
template = f.read()
# load translations and fill in the template, saving to po files.
network_settings = get_network_settings_translations()
for f in glob.glob("lang-resources/po-gnome-control-center/*.po"):
lang = os.path.basename(f).rsplit(".", 1)[0]
translations = get_control_center_translations(lang)
translations['Network Settings'] = network_settings.get(
lang, 'Network Settings')
filename = "po/{}.po".format(lang)
print("writing {}".format(filename))
with open(filename, 'w') as f:
po = template
for key, value in translations.items():
po = po.replace('{{' + key + '}}', value)
f.write(po)
def make_locale():
''' Constructs the locale. '''
make_all_po_files()
for po in glob.glob("po/*.po"):
lang = os.path.basename(po).rsplit('.', 1)[0]
cmd = "mkdir -p locale/{0}/LC_MESSAGES; msgfmt {1} -o locale/{0}/LC_MESSAGES/{2}.mo".format(lang, po, UUID)
print(cmd)
os.system(cmd)
if __name__ == '__main__':
make_locale()