lang-check.py 8.1 KB

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