Skip to content

Commit

Permalink
fix: lazy translate circular imports (frappe#24672)
Browse files Browse the repository at this point in the history
* Revert "fix: remove _lt from frappe.model.std_fields (frappe#24662)"

This reverts commit 833d108.

* fix: circular import
  • Loading branch information
ankush authored Feb 1, 2024
1 parent 3fb418f commit aad0c19
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
40 changes: 38 additions & 2 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,45 @@ def _lt(msg: str, lang: str | None = None, context: str | None = None):
Note: Result is not guaranteed to equivalent to pure strings for all operations.
"""
from frappe.translate import LazyTranslate
return _LazyTranslate(msg, lang, context)

return LazyTranslate(msg, lang, context)

@functools.total_ordering
class _LazyTranslate:
__slots__ = ("msg", "lang", "context")

def __init__(self, msg: str, lang: str | None = None, context: str | None = None) -> None:
self.msg = msg
self.lang = lang
self.context = context

@property
def value(self) -> str:
return _(str(self.msg), self.lang, self.context)

def __str__(self):
return self.value

def __add__(self, other):
if isinstance(other, (str, _LazyTranslate)):
return self.value + str(other)
raise NotImplementedError

def __radd__(self, other):
if isinstance(other, (str, _LazyTranslate)):
return str(other) + self.value
return NotImplementedError

def __repr__(self) -> str:
return f"'{self.value}'"

# NOTE: it's required to override these methods and raise error as default behaviour will
# return `False` in all cases.
def __eq__(self, other):
raise NotImplementedError

def __lt__(self, other):
raise NotImplementedError


def as_unicode(text, encoding: str = "utf-8") -> str:
Expand Down
3 changes: 2 additions & 1 deletion frappe/gettext/extractors/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re

import frappe
from frappe.model.utils import InvalidIncludePath, render_include

TRANSLATE_PATTERN = re.compile(
r"_\(\s*" # starts with literal `_(`, ignore following whitespace/newlines
Expand Down Expand Up @@ -36,6 +35,8 @@ def extract_messages_from_code(code):
"""
from jinja2 import TemplateError

from frappe.model.utils import InvalidIncludePath, render_include

try:
code = frappe.as_unicode(render_include(code))

Expand Down
38 changes: 0 additions & 38 deletions frappe/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,44 +1029,6 @@ def print_language(language: str):
frappe.local.jenv = _jenv


@functools.total_ordering
class LazyTranslate:
__slots__ = ("msg", "lang", "context")

def __init__(self, msg: str, lang: str | None = None, context: str | None = None) -> None:
self.msg = msg
self.lang = lang
self.context = context

@property
def value(self) -> str:
return frappe._(str(self.msg), self.lang, self.context)

def __str__(self):
return self.value

def __add__(self, other):
if isinstance(other, (str, LazyTranslate)):
return self.value + str(other)
raise NotImplementedError

def __radd__(self, other):
if isinstance(other, (str, LazyTranslate)):
return str(other) + self.value
return NotImplementedError

def __repr__(self) -> str:
return f"'{self.value}'"

# NOTE: it's required to override these methods and raise error as default behaviour will
# return `False` in all cases.
def __eq__(self, other):
raise NotImplementedError

def __lt__(self, other):
raise NotImplementedError


# Backward compatibility
get_full_dict = get_all_translations
load_lang = get_translations_from_apps

0 comments on commit aad0c19

Please sign in to comment.