blob: 9146b3b32b8af935ea8a21b89563a262e9ac26a5 [file] [log] [blame]
Suraj Shetty0a6770a2020-10-14 10:21:31 +05301import re
2import sys
3
4errors_encounter = 0
Suraj Shetty8586c082021-02-01 20:09:42 +05305pattern = 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)*)*\)")
Suraj Shetty0a6770a2020-10-14 10:21:31 +05306words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]")
7start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}")
8f_string_pattern = re.compile(r"_\(f[\"']")
9starts_with_f_pattern = re.compile(r"_\(f")
10
11# skip first argument
12files = sys.argv[1:]
13files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))]
14
15for _file in files_to_scan:
16 with open(_file, 'r') as f:
17 print(f'Checking: {_file}')
18 file_lines = f.readlines()
19 for line_number, line in enumerate(file_lines, 1):
20 if 'frappe-lint: disable-translate' in line:
21 continue
22
23 start_matches = start_pattern.search(line)
24 if start_matches:
25 starts_with_f = starts_with_f_pattern.search(line)
26
27 if starts_with_f:
28 has_f_string = f_string_pattern.search(line)
29 if has_f_string:
30 errors_encounter += 1
Suraj Shetty8586c082021-02-01 20:09:42 +053031 print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}')
Suraj Shetty0a6770a2020-10-14 10:21:31 +053032 continue
33 else:
34 continue
35
36 match = pattern.search(line)
37 error_found = False
38
Suraj Shetty8586c082021-02-01 20:09:42 +053039 if not match and line.endswith((',\n', '[\n')):
Suraj Shetty0a6770a2020-10-14 10:21:31 +053040 # concat remaining text to validate multiline pattern
41 line = "".join(file_lines[line_number - 1:])
42 line = line[start_matches.start() + 1:]
43 match = pattern.match(line)
44
45 if not match:
46 error_found = True
Suraj Shetty8586c082021-02-01 20:09:42 +053047 print(f'\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}')
Suraj Shetty0a6770a2020-10-14 10:21:31 +053048
49 if not error_found and not words_pattern.search(line):
50 error_found = True
Suraj Shetty8586c082021-02-01 20:09:42 +053051 print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}')
Suraj Shetty0a6770a2020-10-14 10:21:31 +053052
53 if error_found:
54 errors_encounter += 1
55
56if errors_encounter > 0:
57 print('\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.')
58 sys.exit(1)
59else:
60 print('\nGood To Go!')