From 5cf39f746a22418c9eb3288005a29718c3152e2d Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 17:26:36 +0530 Subject: [PATCH 1/6] chore: Check existence of words in translation linter --- .github/helper/translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index ee04de9161..1226feea90 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -3,7 +3,7 @@ import sys errors_encounter = 0 pattern = re.compile(r"_\(([\"']{,3})(?P((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P((?!\11).)*)\11)*)*\)") -start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}") +start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*[a-zA-Z]") # skip first argument files = sys.argv[1:] From a113f5c285610c0dda6e57c0fe24d34ae930e54b Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 17:28:21 +0530 Subject: [PATCH 2/6] fix: add test strings --- .github/helper/translation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index 1226feea90..734f10482a 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -5,6 +5,12 @@ errors_encounter = 0 pattern = re.compile(r"_\(([\"']{,3})(?P((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P((?!\11).)*)\11)*)*\)") start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*[a-zA-Z]") +# _('this is valid') +# _('{0} {1}') +# _("{0} {1}") +# _("{0} valid") +# _("valid {0}") + # skip first argument files = sys.argv[1:] files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))] From d1b92c66bbf77fe8fa16c4e2afb42feaab2ca393 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 17:36:44 +0530 Subject: [PATCH 3/6] fix: eager lookup --- .github/helper/translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index 734f10482a..1b86e7e72f 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -3,7 +3,7 @@ import sys errors_encounter = 0 pattern = re.compile(r"_\(([\"']{,3})(?P((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P((?!\11).)*)\11)*)*\)") -start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*[a-zA-Z]") +start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]") # _('this is valid') # _('{0} {1}') From 8708e56896773e6b7fc7f65c4bc8017289919987 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 21:00:15 +0530 Subject: [PATCH 4/6] fix: Handle f string and fix words check --- .github/helper/translation.py | 36 +++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index 1b86e7e72f..4070267137 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -3,26 +3,47 @@ import sys errors_encounter = 0 pattern = re.compile(r"_\(([\"']{,3})(?P((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P((?!\11).)*)\11)*)*\)") -start_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}") +f_string_pattern = re.compile(r"_\(f[\"']") +starts_with_f_pattern = re.compile(r"_\(f") # _('this is valid') # _('{0} {1}') # _("{0} {1}") +# _(f"invalid") +# _(fieldname) # _("{0} valid") +# _('asdf asdf') # _("valid {0}") # skip first argument files = sys.argv[1:] files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))] +files_to_scan = ['/Users/farisansari/Projects/benches/frappe-bench/apps/frappe/.github/helper/translation.py'] + for _file in files_to_scan: with open(_file, 'r') as f: print(f'Checking: {_file}') file_lines = f.readlines() for line_number, line in enumerate(file_lines, 1): start_matches = start_pattern.search(line) - if start_matches: + if start_matches and not '# frappe-lint: disable-translate' in line: + starts_with_f = starts_with_f_pattern.search(line) + + if starts_with_f: + has_f_string = f_string_pattern.search(line) + if has_f_string: + errors_encounter += 1 + print(f'\nF-strings are not supported for translations at line number {line_number + 1}\n{line.strip()[:100]}') + continue + else: + continue + match = pattern.search(line) + error_found = False + if not match and line.endswith(',\n'): # concat remaining text to validate multiline pattern line = "".join(file_lines[line_number - 1:]) @@ -30,11 +51,18 @@ for _file in files_to_scan: match = pattern.match(line) if not match: + error_found = True + print(f'\nTranslation syntax error at line number {line_number + 1}\n{line.strip()[:100]}') + + if not error_found and not words_pattern.search(line): + error_found = True + print(f'\nTranslation is useless because it has no words at line number {line_number + 1}\n{line.strip()[:100]}') + + if error_found: errors_encounter += 1 - print(f'\nTranslation syntax error at line number: {line_number + 1}\n{line.strip()[:100]}') if errors_encounter > 0: - print('\nYou can visit "https://frappeframework.com/docs/user/en/translations" to resolve this error.') + print('\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.') sys.exit(1) else: print('\nGood To Go!') From 18bc0d14cf50adbdffebc34e4d336ab9a5bcc58e Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 21:03:22 +0530 Subject: [PATCH 5/6] fix: remove hardcoded path --- .github/helper/translation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index 4070267137..576716ba38 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -21,8 +21,6 @@ starts_with_f_pattern = re.compile(r"_\(f") files = sys.argv[1:] files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))] -files_to_scan = ['/Users/farisansari/Projects/benches/frappe-bench/apps/frappe/.github/helper/translation.py'] - for _file in files_to_scan: with open(_file, 'r') as f: print(f'Checking: {_file}') From c7ed6ce977d5bc7dd7e7e6150aa1654daadab037 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 12 Oct 2020 21:12:47 +0530 Subject: [PATCH 6/6] fix: Remove test strings and early return --- .github/helper/translation.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/helper/translation.py b/.github/helper/translation.py index 576716ba38..340f4f8772 100644 --- a/.github/helper/translation.py +++ b/.github/helper/translation.py @@ -8,15 +8,6 @@ start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}") f_string_pattern = re.compile(r"_\(f[\"']") starts_with_f_pattern = re.compile(r"_\(f") -# _('this is valid') -# _('{0} {1}') -# _("{0} {1}") -# _(f"invalid") -# _(fieldname) -# _("{0} valid") -# _('asdf asdf') -# _("valid {0}") - # skip first argument files = sys.argv[1:] files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))] @@ -26,8 +17,11 @@ for _file in files_to_scan: print(f'Checking: {_file}') file_lines = f.readlines() for line_number, line in enumerate(file_lines, 1): + if 'frappe-lint: disable-translate' in line: + continue + start_matches = start_pattern.search(line) - if start_matches and not '# frappe-lint: disable-translate' in line: + if start_matches: starts_with_f = starts_with_f_pattern.search(line) if starts_with_f: