From 1abd2be96d7aff1bd4a559e73e905edb26894770 Mon Sep 17 00:00:00 2001 From: 3d-gussner <3d.gussner@gmail.com> Date: Mon, 10 Jan 2022 09:52:43 +0100 Subject: [PATCH] Syntax check `lang_en.txt` Display correct line having issues --- lang/lang-check.py | 185 ++++++++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 87 deletions(-) diff --git a/lang/lang-check.py b/lang/lang-check.py index 06410c01..87f4d67e 100755 --- a/lang/lang-check.py +++ b/lang/lang-check.py @@ -121,10 +121,11 @@ def parse_txt(lang, no_warning, warn_empty): print(green("Start %s lang-check" % lang)) - lines = 1 + lines = 0 with open(file_path) as src: while True: message = src.readline() + lines += 1 #print(message) #Debug #check syntax 1st line starts with `#MSG` if (message[0:4] != '#MSG'): @@ -151,7 +152,7 @@ def parse_txt(lang, no_warning, warn_empty): if cols is None and rows is None: if not no_warning: print(yellow("[W]: No display definition on line %d" % lines)) - cols = len(translation) # propably fullscreen + cols = len(source) # propably fullscreen if rows is None: rows = 1 elif rows > 1 and cols != 20: @@ -159,55 +160,61 @@ def parse_txt(lang, no_warning, warn_empty): #Wrap text to 20 chars and rows source = src.readline()[:-1] #read whole line + lines += 1 #check if 2nd line of origin message beginns and ends with " double quote if (source[0]!="\""): - print(red('[E]: Critical syntax error: Missing " at beginning of message in source on line %d' % lines)) + print(red('[E]: Critical syntax error: Missing " double quotes at beginning of message in source on line %d' % lines)) print(red(source)) exit(1) if (source[-1]=="\""): source = source.strip('"') #remove " double quotes from message else: - print(red('[E]: Critical syntax error: Missing " at end of message in source on line %d' % lines)) + print(red('[E]: Critical syntax error: Missing " double quotes at end of message in source on line %d' % lines)) print(red(source)) exit(1) - #print (source) #Debug - translation = src.readline()[:-1]#read whole line - #check if 3rd line of translation message beginns and ends with " double quote - if (translation[0]!="\""): - print(red('[E]: Critical syntax error: Missing " at beginning of message in translation on line %d' % lines)) - print(red(translation)) - exit(1) - if (translation[-1]=="\""): - #print ("End ok") - translation = translation.strip('"') #remove " double quote from message - else: - print(red('[E]: Critical syntax error: Missing " at end of message in translation on line %d' % lines)) - print(red(translation)) - exit(1) - #print (translation) - if translation == '\\x00': - # crude hack to handle intentionally-empty translations - translation = '' - #check if source is ascii only + #print(source) #Debug + if lang != "en": + translation = src.readline()[:-1]#read whole line + lines += 1 + #check if 3rd line of translation message beginns and ends with " double quote + if (translation[0]!="\""): + print(red('[E]: Critical syntax error: Missing " double quotes at beginning of message in translation on line %d' % lines)) + print(red(translation)) + exit(1) + if (translation[-1]=="\""): + #print ("End ok") + translation = translation.strip('"') #remove " double quote from message + else: + print(red('[E]: Critical syntax error: Missing " double quotes at end of message in translation on line %d' % lines)) + print(red(translation)) + exit(1) + #print(translation) #Debug + if translation == '\\x00': + # crude hack to handle intentionally-empty translations + translation = '' + #check if source is ascii only if source.isascii() == False: print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines)) print(red(source)) exit(1) #check if translation is ascii only - if translation.isascii() == False: - print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines)) - print(red(translation)) - exit(1) + if lang != "en": + if translation.isascii() == False: + print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines)) + print(red(translation)) + exit(1) # handle backslash sequences source = unescape(source) - translation = unescape(translation) + if lang != "en": + translation = unescape(translation) #print (translation) #Debug wrapped_source = wrap_text(source, cols) rows_count_source = len(wrapped_source) - wrapped_translation = wrap_text(translation, cols) - rows_count_translation = len(wrapped_translation) + if lang != "en": + wrapped_translation = wrap_text(translation, cols) + rows_count_translation = len(wrapped_translation) # Check for potential errors in the definition if not no_warning: @@ -224,70 +231,74 @@ def parse_txt(lang, no_warning, warn_empty): print() # Missing translation - if len(translation) == 0 and (warn_empty or rows > 1): - if rows == 1: - print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, lines))) - else: - print(yellow("[W]: Empty translation on line %d" % lines)) - print_ruler(6, cols); - print_wrapped(wrapped_source, rows, cols) - print() + if lang != "en": + if len(translation) == 0 and (warn_empty or rows > 1): + if rows == 1: + print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, lines))) + else: + print(yellow("[W]: Empty translation on line %d" % lines)) + print_ruler(6, cols); + print_wrapped(wrapped_source, rows, cols) + print() - # Check for translation lenght - if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols): - 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))) - print_source_translation(source, translation, - wrapped_source, wrapped_translation, - rows, cols) + # Check for translation lenght + if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols): + 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))) + print_source_translation(source, translation, + wrapped_source, wrapped_translation, + rows, cols) - # Different count of % sequences - if source.count('%') != translation.count('%') and len(translation) > 0: - print(red('[E]: Unequal count of %% escapes on line %d:' % (lines))) - print_source_translation(source, translation, - wrapped_source, wrapped_translation, - rows, cols) + # Different count of % sequences + if source.count('%') != translation.count('%') and len(translation) > 0: + print(red('[E]: Unequal count of %% escapes on line %d:' % (lines))) + print_source_translation(source, translation, + wrapped_source, wrapped_translation, + rows, cols) - # Different first/last character - if not no_warning and len(source) > 0 and len(translation) > 0: - source_end = source.rstrip()[-1] - translation_end = translation.rstrip()[-1] - 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: - print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines))) - if end_diff: - print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines))) - print_source_translation(source, translation, - wrapped_source, wrapped_translation, - rows, cols) + # Different first/last character + if not no_warning and len(source) > 0 and len(translation) > 0: + source_end = source.rstrip()[-1] + translation_end = translation.rstrip()[-1] + 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: + print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines))) + if end_diff: + print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines))) + print_source_translation(source, translation, + wrapped_source, wrapped_translation, + rows, cols) - # Short translation - if not no_warning and len(source) > 0 and len(translation) > 0: - if len(translation.rstrip()) < len(source.rstrip()) / 2: - print(yellow('[W]: Short translation on line %d:' % (lines))) - print_source_translation(source, translation, - wrapped_source, wrapped_translation, - rows, cols) + # Short translation + if not no_warning and len(source) > 0 and len(translation) > 0: + if len(translation.rstrip()) < len(source.rstrip()) / 2: + print(yellow('[W]: Short translation on line %d:' % (lines))) + print_source_translation(source, translation, + wrapped_source, wrapped_translation, + rows, cols) - # 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) - if len(src.readline()) != 1: # empty line - print(red('[E]: Critical Syntax error: Missing empty line between messages between lines: %d and %d' % (lines+3,lines+4))) + # 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) + delimiter = src.readline() + lines += 1 + if ("" == delimiter): + break + elif len(delimiter) != 1: # empty line + print(red('[E]: Critical Syntax error: Missing empty line between messages between lines: %d and %d' % (lines-1,lines))) break - lines += 4 print(green("End %s lang-check" % lang))