lang-check.py 4.1 KB

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