From 36ad6cd20a07aecf69e92e9fa724beef14be536a Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Mon, 22 Mar 2021 17:28:05 +0200 Subject: Basic language string mechanism Added a set of English strings. Lang can load a language. LabelWidget can replace IDs in the label. IssueID #192 --- po/compile.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 po/compile.py (limited to 'po') 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 @@ +#!/usr/bin/env python3 +# Parses all the .po files and generates binary language strings to be loaded +# at runtime via embedded data. + +import os + +ESCAPES = { + '\\': '\\', + '"': '"', + 'n': '\n', + 'r': '\r', + 't': '\t' +} + + +def unquote(string): + txt = string.strip() + if txt[0] != '"' or txt[-1] != '"': + raise Exception("invalid quoted string: " + string) + txt = txt[1:-1] + out = '' + is_escape = False + for c in txt: + if is_escape: + out += ESCAPES[c] + is_escape = False + continue + if c == '\\': + is_escape = True + else: + out += c + return out + + +messages = [] +for src in os.listdir('.'): + if not src.endswith('.po'): + continue + msg_id, msg_str = None, None + for line in open(src, 'rt', encoding='utf-8').readlines(): + line = line.strip() + if line.startswith('msgid'): + msg_id = unquote(line[6:]) + elif line.startswith('msgstr'): + msg_str = unquote(line[7:]) + messages.append((msg_id, msg_str)) + # Make a binary blob with strings sorted by ID. + compiled = bytes() + for msg in sorted(messages): + compiled += msg[0].encode('utf-8') + bytes([0]) + compiled += msg[1].encode('utf-8') + bytes([0]) + #print(compiled) + open(f'../res/lang/{src[:-3]}.bin', 'wb').write(compiled) + + -- cgit v1.2.3