Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

54 lignes
1.9 KiB

  1. import re
  2. import sys
  3. errors_encounter = 0
  4. pattern = re.compile(r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,(\s*?.*?\n*?)*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)")
  5. words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]")
  6. start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}")
  7. f_string_pattern = re.compile(r"_\(f[\"']")
  8. starts_with_f_pattern = re.compile(r"_\(f")
  9. # skip first argument
  10. files = sys.argv[1:]
  11. files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))]
  12. for _file in files_to_scan:
  13. with open(_file, 'r') as f:
  14. print(f'Checking: {_file}')
  15. file_lines = f.readlines()
  16. for line_number, line in enumerate(file_lines, 1):
  17. if 'xhiveframework-lint: disable-translate' in line:
  18. continue
  19. if start_matches := start_pattern.search(line):
  20. if starts_with_f := starts_with_f_pattern.search(line):
  21. if has_f_string := f_string_pattern.search(line):
  22. errors_encounter += 1
  23. print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}')
  24. continue
  25. match = pattern.search(line)
  26. error_found = False
  27. if not match and line.endswith((',\n', '[\n')):
  28. # concat remaining text to validate multiline pattern
  29. line = "".join(file_lines[line_number - 1:])
  30. line = line[start_matches.start() + 1:]
  31. match = pattern.match(line)
  32. if not match:
  33. error_found = True
  34. print(f'\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}')
  35. if not error_found and not words_pattern.search(line):
  36. error_found = True
  37. print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}')
  38. if error_found:
  39. errors_encounter += 1
  40. if errors_encounter > 0:
  41. print('\nVisit "https://xhiveframework.co/docs/user/en/translations" to learn about valid translation strings.')
  42. sys.exit(1)
  43. else:
  44. print('\nGood To Go!')