diff --git a/frappe/__init__.py b/frappe/__init__.py index 84dd5c2675..7b2d04cef3 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -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) diff --git a/frappe/gettext/translate.py b/frappe/gettext/translate.py index bc7e37c155..e715d41ff8 100644 --- a/frappe/gettext/translate.py +++ b/frappe/gettext/translate.py @@ -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 diff --git a/frappe/tests/test_translate.py b/frappe/tests/test_translate.py index a4a27c559f..425ca28bb2 100644 --- a/frappe/tests/test_translate.py +++ b/frappe/tests/test_translate.py @@ -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) diff --git a/frappe/translate.py b/frappe/translate.py index 1cf656c959..94c9db918a 100644 --- a/frappe/translate.py +++ b/frappe/translate.py @@ -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={}, ):