lang-check.py 3.6 KB

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