forked from wrook/jquery.gettext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmo2json.py
executable file
·38 lines (32 loc) · 901 Bytes
/
mo2json.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
#!/usr/bin/env python
try:
import json
except:
import simplejson as json
import gettext
import sys
def gettext_json(domain, path, lang=[], indent=False):
tr = gettext.translation(domain, path, lang)
# for unknown reasons, instead of having plural entries like
# key: [sg, pl1...]
# tr._catalog has (key, n): pln,
keys = tr._catalog.keys()
keys.sort()
ret = {}
for k in keys:
v = tr._catalog[k]
if type(k) is tuple:
if k[0] not in ret:
ret[k[0]] = []
ret[k[0]].append(v)
else:
ret[k] = v
return json.dumps(ret, ensure_ascii=False, indent=indent)
def main():
domain, path, locale, filename = sys.argv[1:]
result = gettext_json(domain, path, [locale])
fileout = open(filename, "w")
fileout.write(result)
fileout.close()
if __name__ == "__main__":
main()