diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-23 10:29:14 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-23 10:29:14 +0200 |
commit | 6c6f7a0303405536f634d0f694288c79f076edda (patch) | |
tree | a75d795ea4d09099ec8adc418ddc20ef83fd720a /po/compile.py | |
parent | 244599398b8fa457a160077513456b4b274d386d (diff) |
Lang: Applied string IDs everywhere
More or less everywhere?
IssueID #192
Diffstat (limited to 'po/compile.py')
-rwxr-xr-x | po/compile.py | 41 |
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 | ||
5 | import os | 5 | import os, sys |
6 | 6 | ||
7 | MODE = 'compile' | ||
7 | ESCAPES = { | 8 | ESCAPES = { |
8 | '\\': '\\', | 9 | '\\': '\\', |
9 | '"': '"', | 10 | '"': '"', |
@@ -12,6 +13,9 @@ ESCAPES = { | |||
12 | 't': '\t' | 13 | 't': '\t' |
13 | } | 14 | } |
14 | 15 | ||
16 | if '--new' in sys.argv: | ||
17 | MODE = 'new' | ||
18 | |||
15 | 19 | ||
16 | def unquote(string): | 20 | def 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 | ||
35 | messages = [] | 39 | def parse_po(src): |
36 | for 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 | |||
52 | if 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 | |||
62 | elif 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 | ||