lang-check.py 3.8 KB

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