Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 1 | import re |
| 2 | import sys |
| 3 | |
| 4 | errors_encounter = 0 |
Suraj Shetty | 8586c08 | 2021-02-01 20:09:42 +0530 | [diff] [blame] | 5 | 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)*)*\)") |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 6 | words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]") |
| 7 | start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}") |
| 8 | f_string_pattern = re.compile(r"_\(f[\"']") |
| 9 | starts_with_f_pattern = re.compile(r"_\(f") |
| 10 | |
| 11 | # skip first argument |
| 12 | files = sys.argv[1:] |
| 13 | files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))] |
| 14 | |
| 15 | for _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 Shetty | 8586c08 | 2021-02-01 20:09:42 +0530 | [diff] [blame] | 31 | print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}') |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 32 | continue |
| 33 | else: |
| 34 | continue |
| 35 | |
| 36 | match = pattern.search(line) |
| 37 | error_found = False |
| 38 | |
Suraj Shetty | 8586c08 | 2021-02-01 20:09:42 +0530 | [diff] [blame] | 39 | if not match and line.endswith((',\n', '[\n')): |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 40 | # 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 Shetty | 8586c08 | 2021-02-01 20:09:42 +0530 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | if not error_found and not words_pattern.search(line): |
| 50 | error_found = True |
Suraj Shetty | 8586c08 | 2021-02-01 20:09:42 +0530 | [diff] [blame] | 51 | print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}') |
Suraj Shetty | 0a6770a | 2020-10-14 10:21:31 +0530 | [diff] [blame] | 52 | |
| 53 | if error_found: |
| 54 | errors_encounter += 1 |
| 55 | |
| 56 | if errors_encounter > 0: |
| 57 | print('\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.') |
| 58 | sys.exit(1) |
| 59 | else: |
| 60 | print('\nGood To Go!') |