lang-check.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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):
  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. #End wrap text
  140. # Check for potential errors in the definition
  141. if not no_warning:
  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_truncated(source, cols)
  145. print()
  146. elif rows_count_source > rows:
  147. print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, lines)))
  148. print_wrapped(wrapped_source, rows, cols)
  149. print()
  150. # Check for translation lenght
  151. if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
  152. print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)'
  153. % (lines, cols, rows, rows_count_translation-rows)))
  154. print_source_translation(source, translation,
  155. wrapped_source, wrapped_translation,
  156. rows, cols)
  157. # Different count of % sequences
  158. if source.count('%') != translation.count('%') and len(translation) > 0:
  159. print(red('[E]: Unequal count of %% escapes on line %d:' % (lines)))
  160. print_source_translation(source, translation,
  161. wrapped_source, wrapped_translation,
  162. rows, cols)
  163. # Different first/last character
  164. if not no_warning and len(source) > 0 and len(translation) > 0:
  165. source_end = source.rstrip()[-1]
  166. translation_end = translation.rstrip()[-1]
  167. start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
  168. end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
  169. if start_diff or end_diff:
  170. if start_diff:
  171. print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines)))
  172. if end_diff:
  173. print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines)))
  174. print_source_translation(source, translation,
  175. wrapped_source, wrapped_translation,
  176. rows, cols)
  177. # Short translation
  178. if not no_warning and len(source) > 0 and len(translation) > 0:
  179. if len(translation.rstrip()) < len(source.rstrip()) / 2:
  180. print(yellow('[W]: Short translation on line %d:' % (lines)))
  181. print_source_translation(source, translation,
  182. wrapped_source, wrapped_translation,
  183. rows, cols)
  184. # Incorrect trailing whitespace in translation
  185. if not no_warning and len(translation) > 0 and \
  186. (source.rstrip() == source or (rows == 1 and len(source) == cols)) and \
  187. translation.rstrip() != translation and \
  188. (rows > 1 or len(translation) != len(source)):
  189. print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (lines)))
  190. source = highlight_trailing_white(source)
  191. translation = highlight_trailing_white(translation)
  192. wrapped_translation = highlight_trailing_white(wrapped_translation)
  193. print_source_translation(source, translation,
  194. wrapped_source, wrapped_translation,
  195. rows, cols)
  196. if len(src.readline()) != 1: # empty line
  197. break
  198. lines += 4
  199. print(green("End %s lang-check" % lang))
  200. def main():
  201. """Main function."""
  202. parser = ArgumentParser(
  203. description=__doc__,
  204. usage="%(prog)s lang")
  205. parser.add_argument(
  206. "lang", nargs='?', default="en", type=str,
  207. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  208. parser.add_argument(
  209. "--no-warning", action="store_true",
  210. help="Disable warnings")
  211. args = parser.parse_args()
  212. try:
  213. parse_txt(args.lang, args.no_warning)
  214. return 0
  215. except Exception as exc:
  216. print_exc()
  217. parser.error("%s" % exc)
  218. return 1
  219. if __name__ == "__main__":
  220. exit(main())