fix: add _lt to extractors

This commit is contained in:
Ankush Menat 2024-01-10 21:38:02 +05:30
parent 1f6201b4af
commit 98b62e8220
4 changed files with 19 additions and 3 deletions

View file

@ -119,6 +119,17 @@ def _(msg: str, lang: str | None = None, context: str | None = None) -> str:
def _lt(msg: str, lang: str | None = None, context: str | None = None):
"""Lazily translate a string.
This function returns a "lazy string" which when casted to string via some operation applies
translation first before casting.
This is only useful for translating strings in global scope or anything that potentially runs
before `frappe.init()`
Note: Result is not guaranteed to equivalent to pure strings for all operations.
"""
from frappe.translate import LazyTranslate
return LazyTranslate(msg, lang, context)

View file

@ -7,7 +7,7 @@ from datetime import datetime
from pathlib import Path
from babel.messages.catalog import Catalog
from babel.messages.extract import extract_from_dir
from babel.messages.extract import DEFAULT_KEYWORDS, extract_from_dir
from babel.messages.mofile import read_mo, write_mo
from babel.messages.pofile import read_po, write_po
@ -128,6 +128,9 @@ def generate_pot(target_app: str | None = None):
apps = [target_app] if target_app else frappe.get_all_apps(True)
default_method_map = get_method_map("frappe")
keywords = DEFAULT_KEYWORDS.copy()
keywords["_lt"] = None
for app in apps:
app_path = frappe.get_pymodule_path(app)
catalog = get_catalog(app)
@ -138,7 +141,7 @@ def generate_pot(target_app: str | None = None):
method_map.extend(default_method_map)
for filename, lineno, message, comments, context in extract_from_dir(
app_path, method_map, directory_filter=directory_filter
app_path, method_map, directory_filter=directory_filter, keywords=keywords
):
if not message:
continue

View file

@ -200,6 +200,7 @@ class TestTranslate(FrappeTestCase):
)
_(not_a_string)
_(not_a_string, context="wat")
_lt("Communication")
"""
)
expected_output = [
@ -209,6 +210,7 @@ class TestTranslate(FrappeTestCase):
(5, "name with", "name context"),
(6, "broken on", "new line"),
(10, "broken on separate line", None),
(15, "Communication", None),
]
output = extract_messages_from_python_code(code)

View file

@ -632,7 +632,7 @@ def extract_messages_from_python_code(code: str) -> list[tuple[int, str, str | N
for message in extract_python(
io.BytesIO(code.encode()),
keywords=["_"],
keywords=["_", "_lt"],
comment_tags=(),
options={},
):