瀏覽代碼

refactor: make translate regex readable

version-14
Ankush Menat 3 年之前
父節點
當前提交
6fc3c30dc1
共有 1 個文件被更改,包括 30 次插入2 次删除
  1. +30
    -2
      frappe/translate.py

+ 30
- 2
frappe/translate.py 查看文件

@@ -23,6 +23,35 @@ from frappe.utils import get_bench_path, is_html, strip, strip_html_tags
from frappe.query_builder import Field, DocType
from pypika.terms import PseudoColumn

TRANSLATE_PATTERN = re.compile(
r"_\(" # starts with literal `_(` work with both python / JS

# BEGIN: message search
r"([\"']{,3})" # start of message string identifier - allows: ', ", """, '''; 1st capture group
r"(?P<message>((?!\1).)*)" # Keep matching until string closing identifier is met which is same as 1st capture group
r"\1" # match exact string closing identifier
# END: message search

# BEGIN: python context search
r"(\s*,\s*context\s*=\s*" # capture `context=` with ignoring whitespace
r"([\"'])" # start of context string identifier; 5th capture group
r"(?P<py_context>((?!\5).)*)" # capture context string till closing id is found
r"\5" # match context string closure
r")*" # match one or more context string (?wat this should be 0 or 1)
# END: python context search

# BEGIN: JS context search
r"(\s*,\s*(.)*?\s*(,\s*" # skip message format replacements: ["format", ...] | null | []
r"([\"'])" # start of context string; 11th capture group
r"(?P<js_context>((?!\11).)*)" # capture context string till closing id is found
r"\11" # match context string closure
r")*"
r")*" # match one or more context string
# END: JS context search

r"\)" # Closing function call
)


def get_language(lang_list: List = None) -> str:
"""Set `frappe.local.lang` from HTTP headers at beginning of request
@@ -651,9 +680,8 @@ def extract_messages_from_code(code):
frappe.clear_last_message()

messages = []
pattern = 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)*)*\)"

for m in re.compile(pattern).finditer(code):
for m in TRANSLATE_PATTERN.finditer(code):
message = m.group('message')
context = m.group('py_context') or m.group('js_context')
pos = m.start()


Loading…
取消
儲存