From 0ba2f3921679d021a075246b036b98c27f18b353 Mon Sep 17 00:00:00 2001 From: Ondrej Tuma Date: Tue, 7 May 2019 12:57:31 +0200 Subject: [PATCH] Check translation dictionary files to display definition --- lang/lang-build.sh | 2 ++ lang/lang-check.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 lang/lang-check.py diff --git a/lang/lang-build.sh b/lang/lang-build.sh index c8ad6ff4..e528ff94 100755 --- a/lang/lang-build.sh +++ b/lang/lang-build.sh @@ -81,6 +81,8 @@ generate_binary() rm -f lang_$1.tmp rm -f lang_$1.dat LNG=$1 + #check lang dictionary + /usr/bin/env python lang-check.py $1 #create lang_xx.tmp - different processing for 'en' language if [ "$1" = "en" ]; then #remove comments and empty lines diff --git a/lang/lang-check.py b/lang/lang-check.py new file mode 100644 index 00000000..6b918df8 --- /dev/null +++ b/lang/lang-check.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Check lang files.""" +from argparse import ArgumentParser +from traceback import print_exc +from sys import stderr + + +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 + + lines = 1 + with open(file_path) as src: + while True: + comment = src.readline().split(' ') + src.readline() # source + translation = src.readline()[:-1] + + cols = None + rows = None + for item in comment[1:]: + key, val = item.split('=') + if key == 'c': + cols = int(val) + elif key == 'r': + rows = int(val) + 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: + print("[W]: No display definition on line %d" % lines) + cols = len(translation) # propably fullscreen + if rows is None: + rows = 1 + + if len(translation) > cols*rows: + stderr.write( + "[E]: Text %s is longer then definiton on line %d\n" % + (translation, lines)) + stderr.flush() + + if len(src.readline()) != 1: # empty line + break + lines += 4 + + +def main(): + """Main function.""" + parser = ArgumentParser( + description=__doc__, + usage="$(prog)s lang") + parser.add_argument( + "lang", nargs='?', default="en", type=str, + help="Check lang file (en|cs|de|es|fr|it)") + 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())