lang-check.py 9.6 KB

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