Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 1 | import re |
| 2 | import sys |
| 3 | |
| 4 | errors_encounter = 0 |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 5 | pattern = 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 Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 8 | words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]") |
| 9 | start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}") |
| 10 | f_string_pattern = re.compile(r"_\(f[\"']") |
| 11 | starts_with_f_pattern = re.compile(r"_\(f") |
| 12 | |
| 13 | # skip first argument |
| 14 | files = sys.argv[1:] |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 15 | files_to_scan = [_file for _file in files if _file.endswith((".py", ".js"))] |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 16 | |
| 17 | for _file in files_to_scan: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 18 | with open(_file) as f: |
| 19 | print(f"Checking: {_file}") |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 20 | file_lines = f.readlines() |
| 21 | for line_number, line in enumerate(file_lines, 1): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 22 | if "frappe-lint: disable-translate" in line: |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 23 | 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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 33 | print( |
| 34 | f"\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}" |
| 35 | ) |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 36 | continue |
| 37 | else: |
| 38 | continue |
| 39 | |
| 40 | match = pattern.search(line) |
| 41 | error_found = False |
| 42 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 43 | if not match and line.endswith((",\n", "[\n")): |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 44 | # concat remaining text to validate multiline pattern |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 45 | line = "".join(file_lines[line_number - 1 :]) |
| 46 | line = line[start_matches.start() + 1 :] |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 47 | match = pattern.match(line) |
| 48 | |
| 49 | if not match: |
| 50 | error_found = True |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 51 | print(f"\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}") |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 52 | |
| 53 | if not error_found and not words_pattern.search(line): |
| 54 | error_found = True |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 55 | print( |
| 56 | f"\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}" |
| 57 | ) |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 58 | |
| 59 | if error_found: |
| 60 | errors_encounter += 1 |
| 61 | |
| 62 | if errors_encounter > 0: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 63 | print( |
| 64 | '\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.' |
| 65 | ) |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 66 | sys.exit(1) |
| 67 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 68 | print("\nGood To Go!") |