lang-check.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. #
  3. # Version 1.0.1
  4. #
  5. #############################################################################
  6. # Change log:
  7. # 7 May 2019, Ondrej Tuma, Initial
  8. # 9 June 2020, 3d-gussner, Added version and Change log
  9. # 9 June 2020, 3d-gussner, Wrap text to 20 char and rows
  10. # 9 June 2020, 3d-gussner, colored output
  11. # 2 Apr. 2021, 3d-gussner, Fix and improve text warp
  12. # 22 Apr. 2021, DRracer, add English source to output
  13. #############################################################################
  14. #
  15. """Check lang files."""
  16. from argparse import ArgumentParser
  17. from traceback import print_exc
  18. from sys import stdout, stderr
  19. import textwrap
  20. def color_maybe(color_attr, text):
  21. if stdout.isatty():
  22. return '\033[0;' + str(color_attr) + 'm' + text + '\033[0m'
  23. else:
  24. return text
  25. red = lambda text: color_maybe(31, text)
  26. green = lambda text: color_maybe(32, text)
  27. yellow = lambda text: color_maybe(33, text)
  28. def print_wrapped(wrapped_text, rows, cols):
  29. if type(wrapped_text) == str:
  30. wrapped_text = [wrapped_text]
  31. for r, line in enumerate(wrapped_text):
  32. r_ = str(r + 1).rjust(3)
  33. if r >= rows:
  34. r_ = color_maybe(31, r_)
  35. print((' {} |{:' + str(cols) + 's}|').format(r_, line))
  36. def print_truncated(text, cols):
  37. if len(text) <= cols:
  38. prefix = text.ljust(cols)
  39. suffix = ''
  40. else:
  41. prefix = text[0:cols]
  42. suffix = color_maybe(31, text[cols:])
  43. print(' |' + prefix + '|' + suffix)
  44. def unescape(text):
  45. if '\\' not in text:
  46. return text
  47. return text.encode('ascii').decode('unicode_escape')
  48. def ign_char_first(c):
  49. return c.isalnum() or c in {'%', '?'}
  50. def ign_char_last(c):
  51. return c.isalnum() or c in {'.', "'"}
  52. def parse_txt(lang, no_warning):
  53. """Parse txt file and check strings to display definition."""
  54. if lang == "en":
  55. file_path = "lang_en.txt"
  56. else:
  57. file_path = "lang_en_%s.txt" % lang
  58. print(green("Start %s lang-check" % lang))
  59. lines = 1
  60. with open(file_path) as src:
  61. while True:
  62. comment = src.readline().split(' ')
  63. #print (comment) #Debug
  64. #Check if columns and rows are defined
  65. cols = None
  66. rows = None
  67. for item in comment[1:]:
  68. key, val = item.split('=')
  69. if key == 'c':
  70. cols = int(val)
  71. #print ("c=",cols) #Debug
  72. elif key == 'r':
  73. rows = int(val)
  74. #print ("r=",rows) #Debug
  75. else:
  76. raise RuntimeError(
  77. "Unknown display definition %s on line %d" %
  78. (' '.join(comment), lines))
  79. if cols is None and rows is None:
  80. if not no_warning:
  81. print(yellow("[W]: No display definition on line %d" % lines))
  82. cols = len(translation) # propably fullscreen
  83. if rows is None:
  84. rows = 1
  85. elif rows > 1 and cols != 20:
  86. print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
  87. #Wrap text to 20 chars and rows
  88. source = src.readline()[:-1].strip('"')
  89. #print (source) #Debug
  90. translation = src.readline()[:-1].strip('"')
  91. if translation == '\\x00':
  92. # crude hack to handle intentionally-empty translations
  93. translation = ''
  94. # handle backslash sequences
  95. source = unescape(source)
  96. translation = unescape(translation)
  97. #print (translation) #Debug
  98. wrapped_source = list(textwrap.TextWrapper(width=cols).wrap(source))
  99. rows_count_source = len(wrapped_source)
  100. wrapped_translation = list(textwrap.TextWrapper(width=cols).wrap(translation))
  101. rows_count_translation = len(wrapped_translation)
  102. #End wrap text
  103. # Check for potential errors in the definition
  104. if not no_warning:
  105. if rows == 1 and (len(source) > cols or rows_count_source > rows):
  106. print(yellow('[W]: Source text longer than %d cols as defined on line %d:' % (cols, lines)))
  107. print_truncated(source, cols)
  108. print()
  109. elif rows_count_source > rows:
  110. print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, lines)))
  111. print_wrapped(wrapped_source, rows, cols)
  112. print()
  113. # Check for translation lenght
  114. if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
  115. print(red('[E]: Text is longer then definition on line %d: rows diff=%d cols=%d rows=%d'
  116. % (lines, rows_count_translation-rows, cols, rows)))
  117. if rows == 1:
  118. print(yellow(' source text:'))
  119. print_truncated(source, cols)
  120. print(yellow(' translated text:'))
  121. print_truncated(translation, cols)
  122. else:
  123. print(yellow(' source text:'))
  124. print_wrapped(wrapped_source, rows, cols)
  125. print(yellow(' translated text:'))
  126. print_wrapped(wrapped_translation, rows, cols)
  127. print()
  128. # Different first/last character
  129. if not no_warning and len(source) > 0 and len(translation) > 0:
  130. source_end = source.strip()[-1]
  131. translation_end = translation.strip()[-1]
  132. start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
  133. end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
  134. if start_diff or end_diff:
  135. if start_diff:
  136. print(yellow('[W]: Differing first character (%s => %s) on line %d:' % (source[0], translation[0], lines)))
  137. if end_diff:
  138. print(yellow('[W]: Differing last character (%s => %s) on line %d:' % (source[-1], translation[-1], lines)))
  139. if rows == 1:
  140. print(yellow(' source text:'))
  141. print_truncated(source, cols)
  142. print(yellow(' translated text:'))
  143. print_truncated(translation, cols)
  144. else:
  145. print(yellow(' source text:'))
  146. print_wrapped(wrapped_source, rows, cols)
  147. print(yellow(' translated text:'))
  148. print_wrapped(wrapped_translation, rows, cols)
  149. print()
  150. if len(src.readline()) != 1: # empty line
  151. break
  152. lines += 4
  153. print(green("End %s lang-check" % lang))
  154. def main():
  155. """Main function."""
  156. parser = ArgumentParser(
  157. description=__doc__,
  158. usage="%(prog)s lang")
  159. parser.add_argument(
  160. "lang", nargs='?', default="en", type=str,
  161. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  162. parser.add_argument(
  163. "--no-warning", action="store_true",
  164. help="Disable warnings")
  165. args = parser.parse_args()
  166. try:
  167. parse_txt(args.lang, args.no_warning)
  168. return 0
  169. except Exception as exc:
  170. print_exc()
  171. parser.error("%s" % exc)
  172. return 1
  173. if __name__ == "__main__":
  174. exit(main())