Skip to content

Commit

Permalink
feat: modified parser for frappe JS translate syntax
Browse files Browse the repository at this point in the history
(cherry picked from commit da872a0)
  • Loading branch information
ankush authored and mergify[bot] committed Aug 4, 2022
1 parent fed4ae9 commit 0e87d21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion frappe/tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_args(code):
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):
Expand Down
30 changes: 27 additions & 3 deletions frappe/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -820,15 +832,22 @@ 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 == "(":
if funcname:
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 == ")":
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 0e87d21

Please sign in to comment.