lang-check.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 parse_txt(lang, no_warning):
  29. """Parse txt file and check strings to display definition."""
  30. if lang == "en":
  31. file_path = "lang_en.txt"
  32. else:
  33. file_path = "lang_en_%s.txt" % lang
  34. print(green("Start %s lang-check" % lang))
  35. lines = 1
  36. with open(file_path) as src:
  37. while True:
  38. comment = src.readline().split(' ')
  39. #print (comment) #Debug
  40. #Check if columns and rows are defined
  41. cols = None
  42. rows = None
  43. for item in comment[1:]:
  44. key, val = item.split('=')
  45. if key == 'c':
  46. cols = int(val)
  47. #print ("c=",cols) #Debug
  48. elif key == 'r':
  49. rows = int(val)
  50. #print ("r=",rows) #Debug
  51. else:
  52. raise RuntimeError(
  53. "Unknown display definition %s on line %d" %
  54. (' '.join(comment), lines))
  55. if cols is None and rows is None:
  56. if not no_warning:
  57. print(yellow("[W]: No display definition on line %d" % lines))
  58. cols = len(translation) # propably fullscreen
  59. if rows is None:
  60. rows = 1
  61. elif rows > 1 and cols != 20:
  62. print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
  63. #Wrap text to 20 chars and rows
  64. source = src.readline()[:-1].strip('"')
  65. #print (source) #Debug
  66. translation = src.readline()[:-1].strip('"')
  67. if translation == '\\x00':
  68. # crude hack to handle intentionally-empty translations
  69. translation = ''
  70. #print (translation) #Debug
  71. wrapped_source = list(textwrap.TextWrapper(width=cols).wrap(source))
  72. rows_count_source = len(wrapped_source)
  73. wrapped_translation = list(textwrap.TextWrapper(width=cols).wrap(translation))
  74. rows_count_translation = len(wrapped_translation)
  75. #End wrap text
  76. if (rows_count_translation > rows_count_source and rows_count_translation > rows) or \
  77. (rows == 1 and len(translation) > cols):
  78. print(red('[E]: Text "%s" is longer then definition on line %d (rows diff=%d)\n'
  79. '[EN]:Text "%s" cols=%d rows=%d\n' % (translation, lines, rows_count_translation-rows, source, cols, rows)))
  80. if len(src.readline()) != 1: # empty line
  81. break
  82. lines += 4
  83. print(green("End %s lang-check" % lang))
  84. def main():
  85. """Main function."""
  86. parser = ArgumentParser(
  87. description=__doc__,
  88. usage="%(prog)s lang")
  89. parser.add_argument(
  90. "lang", nargs='?', default="en", type=str,
  91. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  92. parser.add_argument(
  93. "--no-warning", action="store_true",
  94. help="Disable warnings")
  95. args = parser.parse_args()
  96. try:
  97. parse_txt(args.lang, args.no_warning)
  98. return 0
  99. except Exception as exc:
  100. print_exc()
  101. parser.error("%s" % exc)
  102. return 1
  103. if __name__ == "__main__":
  104. exit(main())