lang-check.py 4.0 KB

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