blob: 2918f78434167e7e8bbee586d87a1824e72f86d8 [file] [log] [blame]
Suraj Shetty0a6770a2020-10-14 10:21:31 +05301import re
2import sys
3
4errors_encounter = 0
Akhil Narang3effaf22024-03-27 11:37:26 +05305pattern = re.compile(
6 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)*)*\)"
7)
Suraj Shetty0a6770a2020-10-14 10:21:31 +05308words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]")
9start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}")
10f_string_pattern = re.compile(r"_\(f[\"']")
11starts_with_f_pattern = re.compile(r"_\(f")
12
13# skip first argument
14files = sys.argv[1:]
Akhil Narang3effaf22024-03-27 11:37:26 +053015files_to_scan = [_file for _file in files if _file.endswith((".py", ".js"))]
Suraj Shetty0a6770a2020-10-14 10:21:31 +053016
17for _file in files_to_scan:
Akhil Narang3effaf22024-03-27 11:37:26 +053018 with open(_file) as f:
19 print(f"Checking: {_file}")
Suraj Shetty0a6770a2020-10-14 10:21:31 +053020 file_lines = f.readlines()
21 for line_number, line in enumerate(file_lines, 1):
Akhil Narang3effaf22024-03-27 11:37:26 +053022 if "frappe-lint: disable-translate" in line:
Suraj Shetty0a6770a2020-10-14 10:21:31 +053023 continue
24
25 start_matches = start_pattern.search(line)
26 if start_matches:
27 starts_with_f = starts_with_f_pattern.search(line)
28
29 if starts_with_f:
30 has_f_string = f_string_pattern.search(line)
31 if has_f_string:
32 errors_encounter += 1
Akhil Narang3effaf22024-03-27 11:37:26 +053033 print(
34 f"\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}"
35 )
Suraj Shetty0a6770a2020-10-14 10:21:31 +053036 continue
37 else:
38 continue
39
40 match = pattern.search(line)
41 error_found = False
42
Akhil Narang3effaf22024-03-27 11:37:26 +053043 if not match and line.endswith((",\n", "[\n")):
Suraj Shetty0a6770a2020-10-14 10:21:31 +053044 # concat remaining text to validate multiline pattern
Akhil Narang3effaf22024-03-27 11:37:26 +053045 line = "".join(file_lines[line_number - 1 :])
46 line = line[start_matches.start() + 1 :]
Suraj Shetty0a6770a2020-10-14 10:21:31 +053047 match = pattern.match(line)
48
49 if not match:
50 error_found = True
Akhil Narang3effaf22024-03-27 11:37:26 +053051 print(f"\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}")
Suraj Shetty0a6770a2020-10-14 10:21:31 +053052
53 if not error_found and not words_pattern.search(line):
54 error_found = True
Akhil Narang3effaf22024-03-27 11:37:26 +053055 print(
56 f"\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}"
57 )
Suraj Shetty0a6770a2020-10-14 10:21:31 +053058
59 if error_found:
60 errors_encounter += 1
61
62if errors_encounter > 0:
Akhil Narang3effaf22024-03-27 11:37:26 +053063 print(
64 '\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.'
65 )
Suraj Shetty0a6770a2020-10-14 10:21:31 +053066 sys.exit(1)
67else:
Akhil Narang3effaf22024-03-27 11:37:26 +053068 print("\nGood To Go!")