lang-check.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 parse_txt(lang, no_warning):
  45. """Parse txt file and check strings to display definition."""
  46. if lang == "en":
  47. file_path = "lang_en.txt"
  48. else:
  49. file_path = "lang_en_%s.txt" % lang
  50. print(green("Start %s lang-check" % lang))
  51. lines = 1
  52. with open(file_path) as src:
  53. while True:
  54. comment = src.readline().split(' ')
  55. #print (comment) #Debug
  56. #Check if columns and rows are defined
  57. cols = None
  58. rows = None
  59. for item in comment[1:]:
  60. key, val = item.split('=')
  61. if key == 'c':
  62. cols = int(val)
  63. #print ("c=",cols) #Debug
  64. elif key == 'r':
  65. rows = int(val)
  66. #print ("r=",rows) #Debug
  67. else:
  68. raise RuntimeError(
  69. "Unknown display definition %s on line %d" %
  70. (' '.join(comment), lines))
  71. if cols is None and rows is None:
  72. if not no_warning:
  73. print(yellow("[W]: No display definition on line %d" % lines))
  74. cols = len(translation) # propably fullscreen
  75. if rows is None:
  76. rows = 1
  77. elif rows > 1 and cols != 20:
  78. print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
  79. #Wrap text to 20 chars and rows
  80. source = src.readline()[:-1].strip('"')
  81. #print (source) #Debug
  82. translation = src.readline()[:-1].strip('"')
  83. if translation == '\\x00':
  84. # crude hack to handle intentionally-empty translations
  85. translation = ''
  86. #print (translation) #Debug
  87. wrapped_source = list(textwrap.TextWrapper(width=cols).wrap(source))
  88. rows_count_source = len(wrapped_source)
  89. wrapped_translation = list(textwrap.TextWrapper(width=cols).wrap(translation))
  90. rows_count_translation = len(wrapped_translation)
  91. #End wrap text
  92. # Check for potential errors in the definition
  93. if rows == 1 and (len(source) > cols or rows_count_source > rows):
  94. print(yellow('[W]: Source text longer than %d cols as defined on line %d:' % (cols, lines)))
  95. print_truncated(source, cols)
  96. print()
  97. elif rows_count_source > rows:
  98. print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, lines)))
  99. print_wrapped(wrapped_source, rows, cols)
  100. print()
  101. # Check for translation lenght
  102. if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
  103. print(red('[E]: Text is longer then definition on line %d: rows diff=%d cols=%d rows=%d'
  104. % (lines, rows_count_translation-rows, cols, rows)))
  105. if rows == 1:
  106. print(yellow(' source text:'))
  107. print_truncated(source, cols)
  108. print(yellow(' translated text:'))
  109. print_truncated(translation, cols)
  110. else:
  111. print(yellow(' source text:'))
  112. print_wrapped(wrapped_source, rows, cols)
  113. print(yellow(' translated text:'))
  114. print_wrapped(wrapped_translation, rows, cols)
  115. print()
  116. if len(src.readline()) != 1: # empty line
  117. break
  118. lines += 4
  119. print(green("End %s lang-check" % lang))
  120. def main():
  121. """Main function."""
  122. parser = ArgumentParser(
  123. description=__doc__,
  124. usage="%(prog)s lang")
  125. parser.add_argument(
  126. "lang", nargs='?', default="en", type=str,
  127. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  128. parser.add_argument(
  129. "--no-warning", action="store_true",
  130. help="Disable warnings")
  131. args = parser.parse_args()
  132. try:
  133. parse_txt(args.lang, args.no_warning)
  134. return 0
  135. except Exception as exc:
  136. print_exc()
  137. parser.error("%s" % exc)
  138. return 1
  139. if __name__ == "__main__":
  140. exit(main())