2018-04-03 22:08:27 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
ret = 0
|
|
|
|
|
|
|
|
|
2018-04-05 00:48:59 +02:00
|
|
|
def log(prefix, suffix, fallback):
|
|
|
|
try:
|
|
|
|
print(prefix, suffix) # Non-utf8 output...
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
print(prefix, fallback)
|
|
|
|
|
|
|
|
|
2018-04-03 22:08:27 +02:00
|
|
|
def validate_translation(input, translation):
|
|
|
|
if not translation:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if re.findall(r'(?:(?<!%)%[^%OCRUHBIH]|\$[^at1234])', translation):
|
2018-04-05 00:48:59 +02:00
|
|
|
log('Translation includes invalid formatting:', translation, input)
|
|
|
|
return False
|
2018-04-03 22:08:27 +02:00
|
|
|
|
|
|
|
in_vars = re.findall(r'(\$(?:\d|t))', input)
|
|
|
|
if not all(var in translation for var in in_vars):
|
2018-04-05 00:48:59 +02:00
|
|
|
log('Translation does not contain all variables:', translation, input)
|
2018-04-03 22:08:27 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
in_ascii = re.findall(r'\$a(\d{3})', translation)
|
|
|
|
if any(int(i) > 256 for i in in_ascii):
|
2018-04-05 00:48:59 +02:00
|
|
|
log('Translation contains invalid ascii value:', translation, input)
|
2018-04-03 22:08:27 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
# We could try to validate colors but that is pretty flexible
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def validate_language(path):
|
|
|
|
global ret
|
|
|
|
|
|
|
|
print('Validating', path)
|
|
|
|
|
2018-04-03 22:38:53 +02:00
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
2018-04-03 22:08:27 +02:00
|
|
|
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)
|