2019-05-07 10:57:31 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-04-22 18:43:54 +00:00
|
|
|
#
|
|
|
|
# Version 1.0.1
|
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
# Change log:
|
2021-04-23 06:46:32 +00:00
|
|
|
# 7 May 2019, Ondrej Tuma, Initial
|
|
|
|
# 9 June 2020, 3d-gussner, Added version and Change log
|
|
|
|
# 9 June 2020, 3d-gussner, Wrap text to 20 char and rows
|
|
|
|
# 9 June 2020, 3d-gussner, colored output
|
|
|
|
# 2 Apr. 2021, 3d-gussner, Fix and improve text warp
|
2021-04-26 09:39:37 +00:00
|
|
|
# 22 Apr. 2021, DRracer , add English source to output
|
|
|
|
# 23 Apr. 2021, wavexx , improve
|
|
|
|
# 24 Apr. 2021, wavexx , improve
|
|
|
|
# 26 Apr. 2021, 3d-gussner, add character ruler
|
2021-04-22 18:43:54 +00:00
|
|
|
#############################################################################
|
|
|
|
#
|
2019-05-07 10:57:31 +00:00
|
|
|
"""Check lang files."""
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from traceback import print_exc
|
2021-04-23 10:26:15 +00:00
|
|
|
from sys import stdout, stderr
|
2021-04-22 18:43:54 +00:00
|
|
|
import textwrap
|
2021-04-23 22:52:26 +00:00
|
|
|
import re
|
2019-05-07 10:57:31 +00:00
|
|
|
|
2021-04-23 10:26:15 +00:00
|
|
|
def color_maybe(color_attr, text):
|
|
|
|
if stdout.isatty():
|
|
|
|
return '\033[0;' + str(color_attr) + 'm' + text + '\033[0m'
|
|
|
|
else:
|
|
|
|
return text
|
|
|
|
|
|
|
|
red = lambda text: color_maybe(31, text)
|
|
|
|
green = lambda text: color_maybe(32, text)
|
|
|
|
yellow = lambda text: color_maybe(33, text)
|
2019-05-07 10:57:31 +00:00
|
|
|
|
2021-04-23 13:30:35 +00:00
|
|
|
|
|
|
|
def print_wrapped(wrapped_text, rows, cols):
|
|
|
|
if type(wrapped_text) == str:
|
|
|
|
wrapped_text = [wrapped_text]
|
|
|
|
for r, line in enumerate(wrapped_text):
|
|
|
|
r_ = str(r + 1).rjust(3)
|
|
|
|
if r >= rows:
|
|
|
|
r_ = color_maybe(31, r_)
|
|
|
|
print((' {} |{:' + str(cols) + 's}|').format(r_, line))
|
|
|
|
|
|
|
|
def print_truncated(text, cols):
|
|
|
|
if len(text) <= cols:
|
|
|
|
prefix = text.ljust(cols)
|
|
|
|
suffix = ''
|
|
|
|
else:
|
|
|
|
prefix = text[0:cols]
|
2021-04-23 13:49:19 +00:00
|
|
|
suffix = color_maybe(31, text[cols:])
|
2021-04-23 13:30:35 +00:00
|
|
|
print(' |' + prefix + '|' + suffix)
|
|
|
|
|
2021-04-23 21:19:16 +00:00
|
|
|
def print_source_translation(source, translation, wrapped_source, wrapped_translation, rows, cols):
|
|
|
|
if rows == 1:
|
|
|
|
print(' source text:')
|
2021-04-26 09:39:37 +00:00
|
|
|
print(' |01234567890123456789|')
|
2021-04-23 21:19:16 +00:00
|
|
|
print_truncated(source, cols)
|
|
|
|
print(' translated text:')
|
2021-04-26 09:39:37 +00:00
|
|
|
print(' |01234567890123456789|')
|
2021-04-23 21:19:16 +00:00
|
|
|
print_truncated(translation, cols)
|
|
|
|
else:
|
|
|
|
print(' source text:')
|
2021-04-26 09:39:37 +00:00
|
|
|
print(' |01234567890123456789|')
|
2021-04-23 21:19:16 +00:00
|
|
|
print_wrapped(wrapped_source, rows, cols)
|
|
|
|
print(' translated text:')
|
2021-04-26 09:39:37 +00:00
|
|
|
print(' |01234567890123456789|')
|
2021-04-23 21:19:16 +00:00
|
|
|
print_wrapped(wrapped_translation, rows, cols)
|
|
|
|
print()
|
|
|
|
|
2021-04-23 22:52:26 +00:00
|
|
|
def highlight_trailing_white(text):
|
|
|
|
if type(text) == str:
|
|
|
|
return re.sub(r' $', '·', text)
|
|
|
|
else:
|
|
|
|
ret = text[:]
|
|
|
|
ret[-1] = highlight_trailing_white(ret[-1])
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def wrap_text(text, cols):
|
|
|
|
# wrap text
|
|
|
|
ret = list(textwrap.TextWrapper(width=cols).wrap(text))
|
|
|
|
if len(ret):
|
|
|
|
# add back trailing whitespace
|
|
|
|
ret[-1] += ' ' * (len(text) - len(text.rstrip()))
|
|
|
|
return ret
|
2021-04-23 13:30:35 +00:00
|
|
|
|
2021-04-23 14:15:24 +00:00
|
|
|
def unescape(text):
|
|
|
|
if '\\' not in text:
|
|
|
|
return text
|
|
|
|
return text.encode('ascii').decode('unicode_escape')
|
|
|
|
|
2021-04-23 20:49:27 +00:00
|
|
|
def ign_char_first(c):
|
|
|
|
return c.isalnum() or c in {'%', '?'}
|
|
|
|
|
|
|
|
def ign_char_last(c):
|
|
|
|
return c.isalnum() or c in {'.', "'"}
|
|
|
|
|
2021-04-23 14:15:24 +00:00
|
|
|
|
2019-05-07 10:57:31 +00:00
|
|
|
def parse_txt(lang, no_warning):
|
|
|
|
"""Parse txt file and check strings to display definition."""
|
|
|
|
if lang == "en":
|
|
|
|
file_path = "lang_en.txt"
|
|
|
|
else:
|
|
|
|
file_path = "lang_en_%s.txt" % lang
|
|
|
|
|
2021-04-22 18:43:54 +00:00
|
|
|
print(green("Start %s lang-check" % lang))
|
|
|
|
|
2019-05-07 10:57:31 +00:00
|
|
|
lines = 1
|
|
|
|
with open(file_path) as src:
|
|
|
|
while True:
|
|
|
|
comment = src.readline().split(' ')
|
2021-04-22 18:43:54 +00:00
|
|
|
#print (comment) #Debug
|
2019-05-07 10:57:31 +00:00
|
|
|
|
2021-04-22 18:43:54 +00:00
|
|
|
#Check if columns and rows are defined
|
2019-05-07 10:57:31 +00:00
|
|
|
cols = None
|
|
|
|
rows = None
|
|
|
|
for item in comment[1:]:
|
|
|
|
key, val = item.split('=')
|
|
|
|
if key == 'c':
|
|
|
|
cols = int(val)
|
2021-04-22 18:43:54 +00:00
|
|
|
#print ("c=",cols) #Debug
|
2019-05-07 10:57:31 +00:00
|
|
|
elif key == 'r':
|
|
|
|
rows = int(val)
|
2021-04-22 18:43:54 +00:00
|
|
|
#print ("r=",rows) #Debug
|
2019-05-07 10:57:31 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Unknown display definition %s on line %d" %
|
|
|
|
(' '.join(comment), lines))
|
|
|
|
if cols is None and rows is None:
|
|
|
|
if not no_warning:
|
2021-04-22 18:43:54 +00:00
|
|
|
print(yellow("[W]: No display definition on line %d" % lines))
|
2019-05-07 10:57:31 +00:00
|
|
|
cols = len(translation) # propably fullscreen
|
|
|
|
if rows is None:
|
|
|
|
rows = 1
|
2021-04-23 13:03:44 +00:00
|
|
|
elif rows > 1 and cols != 20:
|
|
|
|
print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
|
2019-05-07 10:57:31 +00:00
|
|
|
|
2021-04-23 13:11:49 +00:00
|
|
|
#Wrap text to 20 chars and rows
|
|
|
|
source = src.readline()[:-1].strip('"')
|
|
|
|
#print (source) #Debug
|
|
|
|
translation = src.readline()[:-1].strip('"')
|
|
|
|
if translation == '\\x00':
|
|
|
|
# crude hack to handle intentionally-empty translations
|
|
|
|
translation = ''
|
2021-04-23 14:15:24 +00:00
|
|
|
|
|
|
|
# handle backslash sequences
|
|
|
|
source = unescape(source)
|
|
|
|
translation = unescape(translation)
|
|
|
|
|
2021-04-23 13:11:49 +00:00
|
|
|
#print (translation) #Debug
|
2021-04-23 22:52:26 +00:00
|
|
|
wrapped_source = wrap_text(source, cols)
|
2021-04-23 13:11:49 +00:00
|
|
|
rows_count_source = len(wrapped_source)
|
2021-04-23 22:52:26 +00:00
|
|
|
wrapped_translation = wrap_text(translation, cols)
|
2021-04-23 13:11:49 +00:00
|
|
|
rows_count_translation = len(wrapped_translation)
|
|
|
|
#End wrap text
|
2021-04-23 10:18:50 +00:00
|
|
|
|
2021-04-23 13:30:35 +00:00
|
|
|
# Check for potential errors in the definition
|
2021-04-23 20:49:00 +00:00
|
|
|
if not no_warning:
|
|
|
|
if rows == 1 and (len(source) > cols or rows_count_source > rows):
|
|
|
|
print(yellow('[W]: Source text longer than %d cols as defined on line %d:' % (cols, lines)))
|
|
|
|
print_truncated(source, cols)
|
|
|
|
print()
|
|
|
|
elif rows_count_source > rows:
|
|
|
|
print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, lines)))
|
|
|
|
print_wrapped(wrapped_source, rows, cols)
|
|
|
|
print()
|
2021-04-23 13:30:35 +00:00
|
|
|
|
|
|
|
# Check for translation lenght
|
|
|
|
if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
|
2021-04-23 22:16:46 +00:00
|
|
|
print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)'
|
|
|
|
% (lines, cols, rows, rows_count_translation-rows)))
|
2021-04-23 21:19:16 +00:00
|
|
|
print_source_translation(source, translation,
|
|
|
|
wrapped_source, wrapped_translation,
|
|
|
|
rows, cols)
|
2019-05-07 10:57:31 +00:00
|
|
|
|
2021-04-23 21:58:11 +00:00
|
|
|
# Different count of % sequences
|
|
|
|
if source.count('%') != translation.count('%') and len(translation) > 0:
|
2021-04-23 22:16:46 +00:00
|
|
|
print(red('[E]: Unequal count of %% escapes on line %d:' % (lines)))
|
2021-04-23 21:58:11 +00:00
|
|
|
print_source_translation(source, translation,
|
|
|
|
wrapped_source, wrapped_translation,
|
|
|
|
rows, cols)
|
|
|
|
|
2021-04-23 20:49:27 +00:00
|
|
|
# Different first/last character
|
|
|
|
if not no_warning and len(source) > 0 and len(translation) > 0:
|
2021-04-23 22:52:26 +00:00
|
|
|
source_end = source.rstrip()[-1]
|
|
|
|
translation_end = translation.rstrip()[-1]
|
2021-04-23 20:49:27 +00:00
|
|
|
start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
|
|
|
|
end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
|
|
|
|
if start_diff or end_diff:
|
|
|
|
if start_diff:
|
2021-04-23 22:16:46 +00:00
|
|
|
print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines)))
|
2021-04-23 20:49:27 +00:00
|
|
|
if end_diff:
|
2021-04-23 22:16:46 +00:00
|
|
|
print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines)))
|
2021-04-23 21:19:16 +00:00
|
|
|
print_source_translation(source, translation,
|
|
|
|
wrapped_source, wrapped_translation,
|
|
|
|
rows, cols)
|
2021-04-23 21:08:02 +00:00
|
|
|
|
|
|
|
# Short translation
|
|
|
|
if not no_warning and len(source) > 0 and len(translation) > 0:
|
2021-04-23 22:52:26 +00:00
|
|
|
if len(translation.rstrip()) < len(source.rstrip()) / 2:
|
2021-04-23 21:08:02 +00:00
|
|
|
print(yellow('[W]: Short translation on line %d:' % (lines)))
|
2021-04-23 21:19:16 +00:00
|
|
|
print_source_translation(source, translation,
|
|
|
|
wrapped_source, wrapped_translation,
|
|
|
|
rows, cols)
|
2021-04-23 20:49:27 +00:00
|
|
|
|
2021-04-23 22:52:26 +00:00
|
|
|
# Incorrect trailing whitespace in translation
|
|
|
|
if not no_warning and len(translation) > 0 and \
|
|
|
|
(source.rstrip() == source or (rows == 1 and len(source) == cols)) and \
|
|
|
|
translation.rstrip() != translation and \
|
|
|
|
(rows > 1 or len(translation) != len(source)):
|
|
|
|
print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (lines)))
|
|
|
|
source = highlight_trailing_white(source)
|
|
|
|
translation = highlight_trailing_white(translation)
|
|
|
|
wrapped_translation = highlight_trailing_white(wrapped_translation)
|
|
|
|
print_source_translation(source, translation,
|
|
|
|
wrapped_source, wrapped_translation,
|
|
|
|
rows, cols)
|
|
|
|
|
2019-05-07 10:57:31 +00:00
|
|
|
if len(src.readline()) != 1: # empty line
|
|
|
|
break
|
|
|
|
lines += 4
|
2021-04-22 18:43:54 +00:00
|
|
|
print(green("End %s lang-check" % lang))
|
2019-05-07 10:57:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main function."""
|
|
|
|
parser = ArgumentParser(
|
|
|
|
description=__doc__,
|
2021-04-22 18:43:54 +00:00
|
|
|
usage="%(prog)s lang")
|
2019-05-07 10:57:31 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"lang", nargs='?', default="en", type=str,
|
2019-09-14 13:49:33 +00:00
|
|
|
help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
|
2019-05-07 10:57:31 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-warning", action="store_true",
|
|
|
|
help="Disable warnings")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
|
|
parse_txt(args.lang, args.no_warning)
|
|
|
|
return 0
|
|
|
|
except Exception as exc:
|
|
|
|
print_exc()
|
|
|
|
parser.error("%s" % exc)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
exit(main())
|