diff options
Diffstat (limited to 'po/compile.py')
-rwxr-xr-x | po/compile.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/po/compile.py b/po/compile.py new file mode 100755 index 00000000..6e565733 --- /dev/null +++ b/po/compile.py | |||
@@ -0,0 +1,55 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | # Parses all the .po files and generates binary language strings to be loaded | ||
3 | # at runtime via embedded data. | ||
4 | |||
5 | import os | ||
6 | |||
7 | ESCAPES = { | ||
8 | '\\': '\\', | ||
9 | '"': '"', | ||
10 | 'n': '\n', | ||
11 | 'r': '\r', | ||
12 | 't': '\t' | ||
13 | } | ||
14 | |||
15 | |||
16 | def unquote(string): | ||
17 | txt = string.strip() | ||
18 | if txt[0] != '"' or txt[-1] != '"': | ||
19 | raise Exception("invalid quoted string: " + string) | ||
20 | txt = txt[1:-1] | ||
21 | out = '' | ||
22 | is_escape = False | ||
23 | for c in txt: | ||
24 | if is_escape: | ||
25 | out += ESCAPES[c] | ||
26 | is_escape = False | ||
27 | continue | ||
28 | if c == '\\': | ||
29 | is_escape = True | ||
30 | else: | ||
31 | out += c | ||
32 | return out | ||
33 | |||
34 | |||
35 | messages = [] | ||
36 | for src in os.listdir('.'): | ||
37 | if not src.endswith('.po'): | ||
38 | continue | ||
39 | msg_id, msg_str = None, None | ||
40 | for line in open(src, 'rt', encoding='utf-8').readlines(): | ||
41 | line = line.strip() | ||
42 | if line.startswith('msgid'): | ||
43 | msg_id = unquote(line[6:]) | ||
44 | elif line.startswith('msgstr'): | ||
45 | msg_str = unquote(line[7:]) | ||
46 | messages.append((msg_id, msg_str)) | ||
47 | # Make a binary blob with strings sorted by ID. | ||
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 | |||
55 | |||