您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

60 行
2.0 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 'frappe-lint: disable-translate' in line:
  18. continue
  19. start_matches = start_pattern.search(line)
  20. if start_matches:
  21. starts_with_f = starts_with_f_pattern.search(line)
  22. if starts_with_f:
  23. has_f_string = f_string_pattern.search(line)
  24. if has_f_string:
  25. errors_encounter += 1
  26. print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}')
  27. continue
  28. else:
  29. continue
  30. match = pattern.search(line)
  31. error_found = False
  32. if not match and line.endswith((',\n', '[\n')):
  33. # concat remaining text to validate multiline pattern
  34. line = "".join(file_lines[line_number - 1:])
  35. line = line[start_matches.start() + 1:]
  36. match = pattern.match(line)
  37. if not match:
  38. error_found = True
  39. print(f'\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}')
  40. if not error_found and not words_pattern.search(line):
  41. error_found = True
  42. print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}')
  43. if error_found:
  44. errors_encounter += 1
  45. if errors_encounter > 0:
  46. print('\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.')
  47. sys.exit(1)
  48. else:
  49. print('\nGood To Go!')