Przeglądaj źródła

feat: modified parser for frappe JS translate syntax

(cherry picked from commit da872a0edf)
version-14
Ankush Menat 2 lat temu
committed by Mergify
rodzic
commit
0e87d216c7
2 zmienionych plików z 28 dodań i 4 usunięć
  1. +1
    -1
      frappe/tests/test_translate.py
  2. +27
    -3
      frappe/translate.py

+ 1
- 1
frappe/tests/test_translate.py Wyświetl plik

@@ -216,7 +216,7 @@ class TestTranslate(unittest.TestCase):
self.assertEqual(args, ("attr with", None, "context"))

args = get_args("""__("attr with", ["format", "replacements"])""")
self.assertEqual(args, ("attr with", None))
self.assertEqual(args, "attr with")


def verify_translation_files(app):


+ 27
- 3
frappe/translate.py Wyświetl plik

@@ -803,6 +803,18 @@ def extract_javascript(code, keywords=("__"), options=None):
concatenate_next = False
last_token = None
call_stack = -1

# Tree level = depth inside function call tree
# Example: __("0", ["1", "2"], "3")
# Depth __()
# / | \
# 0 "0" [...] "3" <- only 0th level strings matter
# / \
# 1 "1" "2"
tree_level = 0
opening_operators = {"[", "{"}
closing_operators = {"]", "}"}
all_container_operators = opening_operators.union(closing_operators)
dotted = any("." in kw for kw in keywords)

for token in tokenize(
@@ -820,6 +832,7 @@ def extract_javascript(code, keywords=("__"), options=None):
message_lineno = token.lineno
messages = [unquote_string(token.value)]
call_stack = 0
tree_level = 0
token = Token("operator", ")", token.lineno)

if token.type == "operator" and token.value == "(":
@@ -827,8 +840,14 @@ def extract_javascript(code, keywords=("__"), options=None):
message_lineno = token.lineno
call_stack += 1

elif call_stack >= 0 and token.type == "operator" and token.value in all_container_operators:
if token.value in opening_operators:
tree_level += 1
if token.value in closing_operators:
tree_level -= 1

elif call_stack == -1 and token.type == "linecomment" or token.type == "multilinecomment":
pass
pass # ignore comments

elif funcname and call_stack == 0:
if token.type == "operator" and token.value == ")":
@@ -848,10 +867,13 @@ def extract_javascript(code, keywords=("__"), options=None):
concatenate_next = False
messages = []
call_stack = -1
tree_level = 0

elif token.type in ("string", "template_string"):
new_value = unquote_string(token.value)
if concatenate_next:
if tree_level > 0:
pass
elif concatenate_next:
last_argument = (last_argument or "") + new_value
concatenate_next = False
else:
@@ -863,13 +885,15 @@ def extract_javascript(code, keywords=("__"), options=None):
messages.append(last_argument)
last_argument = None
else:
messages.append(None)
if tree_level == 0:
messages.append(None)
concatenate_next = False
elif token.value == "+":
concatenate_next = True

elif call_stack > 0 and token.type == "operator" and token.value == ")":
call_stack -= 1
tree_level = 0

elif funcname and call_stack == -1:
funcname = None


Ładowanie…
Anuluj
Zapisz