pulp_glue.common.i18n

get_translation cached

get_translation(name: str) -> gettext.NullTranslations

Return a translations object for a certain import path.

Parameters:
  • name (str) –

    An import path.

Returns:
  • NullTranslations

    A gettext translations object containing all the gettext variations.

Examples:

Import the usual suspects like this:

from pulp_glue.common.i18n import get_translation

translation = get_translation(__package__)
_ = translation.gettext

Source code in pulp-glue/pulp_glue/common/i18n.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@lru_cache(maxsize=None)
def get_translation(name: str) -> gettext.NullTranslations:
    """
    Return a translations object for a certain import path.

    Parameters:
        name: An import path.

    Returns:
        A `gettext` translations object containing all the `gettext` variations.

    Examples:
        Import the usual suspects like this:
        ```
        from pulp_glue.common.i18n import get_translation

        translation = get_translation(__package__)
        _ = translation.gettext
        ```
    """
    name_parts = name.split(".")
    while name_parts:
        try:
            localedir = files(".".join(name_parts)) / "locale"
            return gettext.translation("messages", localedir=str(localedir), fallback=True)
        except TypeError:
            name_parts.pop()
    raise TypeError(f"No package found for {name}.")