lang-check.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. # 23 Apr. 2021, wavexx , improve
  14. # 24 Apr. 2021, wavexx , improve
  15. # 26 Apr. 2021, 3d-gussner, add character ruler
  16. #############################################################################
  17. #
  18. """Check lang files."""
  19. from argparse import ArgumentParser
  20. from traceback import print_exc
  21. from sys import stdout, stderr
  22. import textwrap
  23. import re
  24. def color_maybe(color_attr, text):
  25. if stdout.isatty():
  26. return '\033[0;' + str(color_attr) + 'm' + text + '\033[0m'
  27. else:
  28. return text
  29. red = lambda text: color_maybe(31, text)
  30. green = lambda text: color_maybe(32, text)
  31. yellow = lambda text: color_maybe(33, text)
  32. cyan = lambda text: color_maybe(36, text)
  33. def print_wrapped(wrapped_text, rows, cols):
  34. if type(wrapped_text) == str:
  35. wrapped_text = [wrapped_text]
  36. for r, line in enumerate(wrapped_text):
  37. r_ = str(r + 1).rjust(3)
  38. if r >= rows:
  39. r_ = red(r_)
  40. print((' {} |{:' + str(cols) + 's}|').format(r_, line))
  41. def print_truncated(text, cols):
  42. if len(text) <= cols:
  43. prefix = text.ljust(cols)
  44. suffix = ''
  45. else:
  46. prefix = text[0:cols]
  47. suffix = red(text[cols:])
  48. print(' |' + prefix + '|' + suffix)
  49. def print_ruler(spc, cols):
  50. print(' ' * spc + cyan(('₀₁₂₃₄₅₆₇₈₉'*4)[:cols]))
  51. def print_source_translation(source, translation, wrapped_source, wrapped_translation, rows, cols):
  52. if rows == 1:
  53. print(' source text:')
  54. print_ruler(4, cols);
  55. print_truncated(source, cols)
  56. print(' translated text:')
  57. print_ruler(4, cols);
  58. print_truncated(translation, cols)
  59. else:
  60. print(' source text:')
  61. print_ruler(6, cols);
  62. print_wrapped(wrapped_source, rows, cols)
  63. print(' translated text:')
  64. print_ruler(6, cols);
  65. print_wrapped(wrapped_translation, rows, cols)
  66. print()
  67. def highlight_trailing_white(text):
  68. if type(text) == str:
  69. return re.sub(r' $', '·', text)
  70. else:
  71. ret = text[:]
  72. ret[-1] = highlight_trailing_white(ret[-1])
  73. return ret
  74. def wrap_text(text, cols):
  75. # wrap text
  76. ret = list(textwrap.TextWrapper(width=cols).wrap(text))
  77. if len(ret):
  78. # add back trailing whitespace
  79. ret[-1] += ' ' * (len(text) - len(text.rstrip()))
  80. return ret
  81. def unescape(text):
  82. if '\\' not in text:
  83. return text
  84. return text.encode('ascii').decode('unicode_escape')
  85. def ign_char_first(c):
  86. return c.isalnum() or c in {'%', '?'}
  87. def ign_char_last(c):
  88. return c.isalnum() or c in {'.', "'"}
  89. def parse_txt(lang, no_warning, warn_empty):
  90. """Parse txt file and check strings to display definition."""
  91. if lang == "en":
  92. file_path = "lang_en.txt"
  93. else:
  94. file_path = "lang_en_%s.txt" % lang
  95. print(green("Start %s lang-check" % lang))
  96. lines = 1
  97. with open(file_path) as src:
  98. while True:
  99. comment = src.readline().split(' ')
  100. #print (comment) #Debug
  101. #Check if columns and rows are defined
  102. cols = None
  103. rows = None
  104. for item in comment[1:]:
  105. key, val = item.split('=')
  106. if key == 'c':
  107. cols = int(val)
  108. #print ("c=",cols) #Debug
  109. elif key == 'r':
  110. rows = int(val)
  111. #print ("r=",rows) #Debug
  112. else:
  113. raise RuntimeError(
  114. "Unknown display definition %s on line %d" %
  115. (' '.join(comment), lines))
  116. if cols is None and rows is None:
  117. if not no_warning:
  118. print(yellow("[W]: No display definition on line %d" % lines))
  119. cols = len(translation) # propably fullscreen
  120. if rows is None:
  121. rows = 1
  122. elif rows > 1 and cols != 20:
  123. print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
  124. #Wrap text to 20 chars and rows
  125. source = src.readline()[:-1].strip('"')
  126. #print (source) #Debug
  127. translation = src.readline()[:-1].strip('"')
  128. if translation == '\\x00':
  129. # crude hack to handle intentionally-empty translations
  130. translation = ''
  131. # handle backslash sequences
  132. source = unescape(source)
  133. translation = unescape(translation)
  134. #print (translation) #Debug
  135. wrapped_source = wrap_text(source, cols)
  136. rows_count_source = len(wrapped_source)
  137. wrapped_translation = wrap_text(translation, cols)
  138. rows_count_translation = len(wrapped_translation)
  139. # Check for potential errors in the definition
  140. if not no_warning:
  141. # Incorrect number of rows/cols on the definition
  142. if rows == 1 and (len(source) > cols or rows_count_source > rows):
  143. print(yellow('[W]: Source text longer than %d cols as defined on line %d:' % (cols, lines)))
  144. print_ruler(4, cols);
  145. print_truncated(source, cols)
  146. print()
  147. elif rows_count_source > rows:
  148. print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, lines)))
  149. print_ruler(6, cols);
  150. print_wrapped(wrapped_source, rows, cols)
  151. print()
  152. # Missing translation
  153. if len(translation) == 0 and (warn_empty or rows > 1):
  154. if rows == 1:
  155. print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, lines)))
  156. else:
  157. print(yellow("[W]: Empty translation on line %d" % lines))
  158. print_ruler(6, cols);
  159. print_wrapped(wrapped_source, rows, cols)
  160. print()
  161. # Check for translation lenght
  162. if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
  163. print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)'
  164. % (lines, cols, rows, rows_count_translation-rows)))
  165. print_source_translation(source, translation,
  166. wrapped_source, wrapped_translation,
  167. rows, cols)
  168. # Different count of % sequences
  169. if source.count('%') != translation.count('%') and len(translation) > 0:
  170. print(red('[E]: Unequal count of %% escapes on line %d:' % (lines)))
  171. print_source_translation(source, translation,
  172. wrapped_source, wrapped_translation,
  173. rows, cols)
  174. # Different first/last character
  175. if not no_warning and len(source) > 0 and len(translation) > 0:
  176. source_end = source.rstrip()[-1]
  177. translation_end = translation.rstrip()[-1]
  178. start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
  179. end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
  180. if start_diff or end_diff:
  181. if start_diff:
  182. print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines)))
  183. if end_diff:
  184. print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines)))
  185. print_source_translation(source, translation,
  186. wrapped_source, wrapped_translation,
  187. rows, cols)
  188. # Short translation
  189. if not no_warning and len(source) > 0 and len(translation) > 0:
  190. if len(translation.rstrip()) < len(source.rstrip()) / 2:
  191. print(yellow('[W]: Short translation on line %d:' % (lines)))
  192. print_source_translation(source, translation,
  193. wrapped_source, wrapped_translation,
  194. rows, cols)
  195. # Incorrect trailing whitespace in translation
  196. if not no_warning and len(translation) > 0 and \
  197. (source.rstrip() == source or (rows == 1 and len(source) == cols)) and \
  198. translation.rstrip() != translation and \
  199. (rows > 1 or len(translation) != len(source)):
  200. print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (lines)))
  201. source = highlight_trailing_white(source)
  202. translation = highlight_trailing_white(translation)
  203. wrapped_translation = highlight_trailing_white(wrapped_translation)
  204. print_source_translation(source, translation,
  205. wrapped_source, wrapped_translation,
  206. rows, cols)
  207. if len(src.readline()) != 1: # empty line
  208. break
  209. lines += 4
  210. print(green("End %s lang-check" % lang))
  211. def main():
  212. """Main function."""
  213. parser = ArgumentParser(
  214. description=__doc__,
  215. usage="%(prog)s lang")
  216. parser.add_argument(
  217. "lang", nargs='?', default="en", type=str,
  218. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  219. parser.add_argument(
  220. "--no-warning", action="store_true",
  221. help="Disable warnings")
  222. parser.add_argument(
  223. "--warn-empty", action="store_true",
  224. help="Warn about empty translations")
  225. args = parser.parse_args()
  226. try:
  227. parse_txt(args.lang, args.no_warning, args.warn_empty)
  228. return 0
  229. except Exception as exc:
  230. print_exc()
  231. parser.error("%s" % exc)
  232. return 1
  233. if __name__ == "__main__":
  234. exit(main())