diff options
Diffstat (limited to 'po/compile.py')
-rwxr-xr-x | po/compile.py | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/po/compile.py b/po/compile.py index cfe837ea..0a0eab91 100755 --- a/po/compile.py +++ b/po/compile.py | |||
@@ -38,29 +38,46 @@ def unquote(string): | |||
38 | 38 | ||
39 | def parse_po(src): | 39 | def parse_po(src): |
40 | messages = [] | 40 | messages = [] |
41 | is_multi = False | 41 | is_multi = False # string is multiple lines |
42 | msg_id, msg_str = None, None | 42 | is_plural = False |
43 | msg_id, msg_str, msg_index = None, None, None | ||
43 | for line in open(src, 'rt', encoding='utf-8').readlines(): | 44 | for line in open(src, 'rt', encoding='utf-8').readlines(): |
44 | line = line.strip() | 45 | line = line.strip() |
45 | if is_multi: | 46 | if is_multi: |
46 | if len(line) == 0: | 47 | if len(line) == 0 or line[0] != '"': |
47 | if msg_id: | 48 | if msg_id: |
48 | messages.append((msg_id, msg_str)) | 49 | messages.append((msg_id, msg_str, msg_index)) |
49 | is_multi = False | 50 | is_multi = False |
50 | continue | 51 | if len(line) == 0: continue |
51 | else: | 52 | else: |
52 | msg_str += unquote(line) | 53 | msg_str += unquote(line) |
53 | if line.startswith('msgid'): | 54 | if line.startswith('msgid_plural'): |
55 | msg_id = unquote(line[12:]) | ||
56 | is_plural = True | ||
57 | elif line.startswith('msgid'): | ||
54 | msg_id = unquote(line[6:]) | 58 | msg_id = unquote(line[6:]) |
55 | elif line == 'msgstr ""': | 59 | is_plural = False |
56 | # Multiline string. | 60 | elif line.startswith('msgstr'): |
57 | is_multi = True | 61 | if line[6] == '[': |
58 | msg_str = '' | 62 | msg_index = int(line[7]) |
59 | elif line.startswith('msgstr'): | 63 | line = line[9:] |
60 | msg_str = unquote(line[7:]) | 64 | else: |
61 | if msg_id: | 65 | msg_index = None |
62 | messages.append((msg_id, msg_str)) | 66 | line = line[7:] |
63 | return messages | 67 | if line.endswith(' ""'): |
68 | is_multi = True | ||
69 | msg_str = '' | ||
70 | else: | ||
71 | msg_str = unquote(line) | ||
72 | if msg_id: | ||
73 | messages.append((msg_id, msg_str, msg_index)) | ||
74 | # Apply plural indices to ids. | ||
75 | pluralized = [] | ||
76 | for msg_id, msg_str, msg_index in messages: | ||
77 | if not msg_index is None: | ||
78 | msg_id = f'{msg_id[:-1]}{msg_index}' | ||
79 | pluralized.append((msg_id, msg_str)) | ||
80 | return pluralized | ||
64 | 81 | ||
65 | 82 | ||
66 | if MODE == 'compile': | 83 | if MODE == 'compile': |
@@ -76,6 +93,7 @@ if MODE == 'compile': | |||
76 | elif MODE == 'new': | 93 | elif MODE == 'new': |
77 | messages = parse_po('en.po') | 94 | messages = parse_po('en.po') |
78 | f = open('new.po', 'wt', encoding='utf-8') | 95 | f = open('new.po', 'wt', encoding='utf-8') |
96 | # TODO: plurals | ||
79 | for msg_id, _ in messages: | 97 | for msg_id, _ in messages: |
80 | print(f'\nmsgid "{msg_id}"\nmsgstr ""\n', file=f) | 98 | print(f'\nmsgid "{msg_id}"\nmsgstr ""\n', file=f) |
81 | 99 | ||