Skip to content

LazyText

🛈 DocStrings

A message id that is translated whenever it is used as a string.

Source code in src/datacook/i18n.py
class LazyText:
    """A message id that is translated whenever it is used as a string."""

    __slots__ = ("msgid",)

    def __init__(self, msgid: str) -> None:
        self.msgid = msgid

    def __str__(self) -> str:
        return translate(self.msgid)

    def __repr__(self) -> str:
        return f"_({self.msgid!r})"

    def __format__(self, spec: str) -> str:
        return format(str(self), spec)

    def __eq__(self, other: object) -> bool:
        if isinstance(other, LazyText):
            return self.msgid == other.msgid
        if isinstance(other, str):
            return str(self) == other
        return NotImplemented

    def __hash__(self) -> int:
        return hash(self.msgid)

    def __add__(self, other: str) -> str:
        return str(self) + other

    def __radd__(self, other: str) -> str:
        return other + str(self)

:fa-brands-github: Show source on GitHub