summaryrefslogtreecommitdiff
path: root/po/compile.py
diff options
context:
space:
mode:
Diffstat (limited to 'po/compile.py')
-rwxr-xr-xpo/compile.py41
1 files changed, 27 insertions, 14 deletions
diff --git a/po/compile.py b/po/compile.py
index 6e565733..650dc3eb 100755
--- a/po/compile.py
+++ b/po/compile.py
@@ -2,8 +2,9 @@
2# Parses all the .po files and generates binary language strings to be loaded 2# Parses all the .po files and generates binary language strings to be loaded
3# at runtime via embedded data. 3# at runtime via embedded data.
4 4
5import os 5import os, sys
6 6
7MODE = 'compile'
7ESCAPES = { 8ESCAPES = {
8 '\\': '\\', 9 '\\': '\\',
9 '"': '"', 10 '"': '"',
@@ -12,6 +13,9 @@ ESCAPES = {
12 't': '\t' 13 't': '\t'
13} 14}
14 15
16if '--new' in sys.argv:
17 MODE = 'new'
18
15 19
16def unquote(string): 20def unquote(string):
17 txt = string.strip() 21 txt = string.strip()
@@ -30,12 +34,10 @@ def unquote(string):
30 else: 34 else:
31 out += c 35 out += c
32 return out 36 return out
33 37
34 38
35messages = [] 39def parse_po(src):
36for src in os.listdir('.'): 40 messages = []
37 if not src.endswith('.po'):
38 continue
39 msg_id, msg_str = None, None 41 msg_id, msg_str = None, None
40 for line in open(src, 'rt', encoding='utf-8').readlines(): 42 for line in open(src, 'rt', encoding='utf-8').readlines():
41 line = line.strip() 43 line = line.strip()
@@ -44,12 +46,23 @@ for src in os.listdir('.'):
44 elif line.startswith('msgstr'): 46 elif line.startswith('msgstr'):
45 msg_str = unquote(line[7:]) 47 msg_str = unquote(line[7:])
46 messages.append((msg_id, msg_str)) 48 messages.append((msg_id, msg_str))
47 # Make a binary blob with strings sorted by ID. 49 return messages
48 compiled = bytes()
49 for msg in sorted(messages):
50 compiled += msg[0].encode('utf-8') + bytes([0])
51 compiled += msg[1].encode('utf-8') + bytes([0])
52 #print(compiled)
53 open(f'../res/lang/{src[:-3]}.bin', 'wb').write(compiled)
54 50
51
52if MODE == 'compile':
53 for src in os.listdir('.'):
54 if src.endswith('.po'):
55 # Make a binary blob with strings sorted by ID.
56 compiled = bytes()
57 for msg in sorted(parse_po(src)):
58 compiled += msg[0].encode('utf-8') + bytes([0])
59 compiled += msg[1].encode('utf-8') + bytes([0])
60 open(f'../res/lang/{src[:-3]}.bin', 'wb').write(compiled)
61
62elif MODE == 'new':
63 messages = parse_po('en.po')
64 f = open('new.po', 'wt', encoding='utf-8')
65 for msg_id, _ in messages:
66 print(f'\nmsgid "{msg_id}"\nmsgstr ""\n', file=f)
67
55 68