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
# 22 Apr. 2021, DRracer, add English source to output
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
from sys import stderr
2021-04-22 18:43:54 +00:00
import textwrap
2019-05-07 10:57:31 +00:00
2021-04-22 18:43:54 +00:00
red = lambda text : ' \033 [0;31m ' + text + ' \033 [0m '
green = lambda text : ' \033 [0;32m ' + text + ' \033 [0m '
yellow = lambda text : ' \033 [0;33m ' + text + ' \033 [0m '
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
source = src . readline ( ) [ : - 1 ]
#print (source) #Debug
2019-05-07 10:57:31 +00:00
translation = src . readline ( ) [ : - 1 ]
2021-04-22 18:43:54 +00:00
#print (translation) #Debug
#Wrap text to 20 chars and rows
wrapper = textwrap . TextWrapper ( width = 20 )
#wrap original/source
rows_count_source = 0
for line in wrapper . wrap ( source . strip ( ' " ' ) ) :
rows_count_source + = 1
#print (line) #Debug
#wrap translation
rows_count_translation = 0
for line in wrapper . wrap ( translation . strip ( ' " ' ) ) :
rows_count_translation + = 1
#print (line) #Debug
#End wrap text
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-22 18:43:54 +00:00
if rows_count_translation > rows_count_source and rows_count_translation > rows :
2021-04-23 06:46:32 +00:00
print ( red ( " [E]: Text %s is longer then definition on line %d rows diff= %d \n [EN]:Text %s cols= %d rows= %d \n " % ( translation , lines , rows_count_translation - rows , source , cols , rows ) ) )
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 ( ) )