chore: Update translation syntax checker

This commit is contained in:
Suraj Shetty 2021-02-01 20:09:42 +05:30
parent f57efd1150
commit 8586c08639

View File

@ -2,7 +2,7 @@ import re
import sys import sys
errors_encounter = 0 errors_encounter = 0
pattern = re.compile(r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)") 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)*)*\)")
words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]") words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]")
start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}") start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}")
f_string_pattern = re.compile(r"_\(f[\"']") f_string_pattern = re.compile(r"_\(f[\"']")
@ -28,7 +28,7 @@ for _file in files_to_scan:
has_f_string = f_string_pattern.search(line) has_f_string = f_string_pattern.search(line)
if has_f_string: if has_f_string:
errors_encounter += 1 errors_encounter += 1
print(f'\nF-strings are not supported for translations at line number {line_number + 1}\n{line.strip()[:100]}') print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}')
continue continue
else: else:
continue continue
@ -36,7 +36,7 @@ for _file in files_to_scan:
match = pattern.search(line) match = pattern.search(line)
error_found = False error_found = False
if not match and line.endswith(',\n'): if not match and line.endswith((',\n', '[\n')):
# concat remaining text to validate multiline pattern # concat remaining text to validate multiline pattern
line = "".join(file_lines[line_number - 1:]) line = "".join(file_lines[line_number - 1:])
line = line[start_matches.start() + 1:] line = line[start_matches.start() + 1:]
@ -44,11 +44,11 @@ for _file in files_to_scan:
if not match: if not match:
error_found = True error_found = True
print(f'\nTranslation syntax error at line number {line_number + 1}\n{line.strip()[:100]}') print(f'\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}')
if not error_found and not words_pattern.search(line): if not error_found and not words_pattern.search(line):
error_found = True error_found = True
print(f'\nTranslation is useless because it has no words at line number {line_number + 1}\n{line.strip()[:100]}') print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}')
if error_found: if error_found:
errors_encounter += 1 errors_encounter += 1