summary refs log tree commit diff stats
path: root/po/validate-textevent-translations
blob: c61f94136b850a33ebfd836e100e9a834908db2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3

import os
import re
import sys

ret = 0


def validate_translation(input, translation):
    if not translation:
        return True

    if re.findall(r'(?:(?<!%)%[^%OCRUHBIH]|\$[^at1234])', translation):
        print('Translation includes invalid formatting:', translation)

    in_vars = re.findall(r'(\$(?:\d|t))', input)
    if not all(var in translation for var in in_vars):
        print('Translation does not contain all variables:', translation)
        return False

    in_ascii = re.findall(r'\$a(\d{3})', translation)
    if any(int(i) > 256 for i in in_ascii):
        print('Translation contains invalid ascii value:')
        return False

    # We could try to validate colors but that is pretty flexible
    return True


def validate_language(path):
    global ret

    print('Validating', path)

    with open(path, 'r', encoding='utf-8') as f:
        in_event = False
        event_input = ''

        for line in f:
            if 'textevents.h' in line:
                in_event = True
            elif in_event is False:
                continue
            elif line.startswith('msgid'):
                event_input = line[7:-2]
            elif line.startswith('msgstr'):
                if not validate_translation(event_input, line[8:-2]):
                    ret = 1
                in_event = False
            elif line == '\n':
                print('Failed to find translation for', event_input)
                in_event = False


with open(sys.argv[1], 'r') as linguas:
    for lang in linguas:
        path = os.path.join(sys.argv[2], lang.strip() + '.po')
        validate_language(path)

sys.exit(ret)