lang-check.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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]
  36. #print (source) #Debug
  37. translation = src.readline()[:-1]
  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.strip('"')):
  44. rows_count_source += 1
  45. #print (line) #Debug
  46. #wrap translation
  47. rows_count_translation = 0
  48. for line in wrapper.wrap(translation.strip('"')):
  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:
  74. print(red("[E]: Text %s is longer then definition on line %d rows diff=%d\n[EN]:Text %s cols=%d rows=%d\n" % (translation, lines, rows_count_translation-rows, source, cols, rows)))
  75. if len(src.readline()) != 1: # empty line
  76. break
  77. lines += 4
  78. print(green("End %s lang-check" % lang))
  79. def main():
  80. """Main function."""
  81. parser = ArgumentParser(
  82. description=__doc__,
  83. usage="%(prog)s lang")
  84. parser.add_argument(
  85. "lang", nargs='?', default="en", type=str,
  86. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  87. parser.add_argument(
  88. "--no-warning", action="store_true",
  89. help="Disable warnings")
  90. args = parser.parse_args()
  91. try:
  92. parse_txt(args.lang, args.no_warning)
  93. return 0
  94. except Exception as exc:
  95. print_exc()
  96. parser.error("%s" % exc)
  97. return 1
  98. if __name__ == "__main__":
  99. exit(main())