Skip to content

application

Class info

Re-exported classes

Class Module Description
LazyText datacook.i18n A message id that is translated whenever it is used as a string.

🛈 DocStrings

App paths (platformdirs). Translation helpers live in datacook.i18n.

LazyText

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)

N_

N_(text: str) -> str

Mark a message for extraction only (e.g. pydantic field titles).

Source code in src/datacook/i18n.py
def N_(text: str) -> str:  # noqa: N802
    """Mark a message for extraction only (e.g. pydantic field titles)."""
    return text

_

_(text: str) -> LazyText

Mark a message for translation; translated lazily when used as a string.

Source code in src/datacook/i18n.py
def _(text: str) -> LazyText:
    """Mark a message for translation; translated lazily when used as a string."""
    return LazyText(text)

set_language

set_language(language: str | None) -> None

Switch the active language (None or "en" -> source strings).

Source code in src/datacook/i18n.py
def set_language(language: str | None) -> None:
    """Switch the active language (`None` or "en" -> source strings)."""
    global _translation
    if language in (None, "en"):
        new: gettext.NullTranslations = gettext.NullTranslations()
    else:
        new = gettext.translation(DOMAIN, localedir=str(LANG_DIR), languages=[language])
    with _lock:
        _translation = new

translate

translate(text: str) -> str

Translate a message id with the active language ("" stays "", not the PO header).

Source code in src/datacook/i18n.py
def translate(text: str) -> str:
    """Translate a message id with the active language ("" stays "", not the PO header)."""
    return _translation.gettext(text) if text else text