Skip to content

PyDrocsid.translations

Translations

Container of multiple translation namespaces

Source code in PyDrocsid/translations.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Translations:
    """Container of multiple translation namespaces"""

    LANGUAGE: str
    FALLBACK: str = "en"

    def __init__(self) -> None:
        self._namespaces: dict[str, _Namespace] = {}

    def register_namespace(self, name: str, path: Path, prio: int = 0) -> None:
        """Register a new source for a translation namespace."""

        if name not in self._namespaces:
            logger.debug("creating new translation namespace '%s'", name)
            self._namespaces[name] = _Namespace()
        else:
            logger.debug("extending translation namespace '%s'", name)

        # noinspection PyProtectedMember
        self._namespaces[name]._add_source(prio, path)

    def __getattr__(self, item: str) -> Any:
        """Return a translation namespace"""

        return self._namespaces[item]

__getattr__

__getattr__(item: str) -> Any

Return a translation namespace

Source code in PyDrocsid/translations.py
170
171
172
173
def __getattr__(self, item: str) -> Any:
    """Return a translation namespace"""

    return self._namespaces[item]

register_namespace

register_namespace(name: str, path: Path, prio: int = 0) -> None

Register a new source for a translation namespace.

Source code in PyDrocsid/translations.py
158
159
160
161
162
163
164
165
166
167
168
def register_namespace(self, name: str, path: Path, prio: int = 0) -> None:
    """Register a new source for a translation namespace."""

    if name not in self._namespaces:
        logger.debug("creating new translation namespace '%s'", name)
        self._namespaces[name] = _Namespace()
    else:
        logger.debug("extending translation namespace '%s'", name)

    # noinspection PyProtectedMember
    self._namespaces[name]._add_source(prio, path)

load_translations

load_translations(path: Path, prio: int = 0) -> None

Recursively load all translations in a given directory and register the appropriate namespaces.

Source code in PyDrocsid/translations.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def load_translations(path: Path, prio: int = 0) -> None:
    """Recursively load all translations in a given directory and register the appropriate namespaces."""

    # skip hidden directories
    if path.name.startswith("."):
        return

    # check if current directory contains a translations subdirectory
    if (p := path.joinpath("translations")).is_dir():
        # register translations directory and return
        t.register_namespace(path.name, p, prio=prio)
        return

    # recurse into subdirectories
    for p in path.iterdir():
        if p.is_dir() and not p.name.startswith("_"):
            load_translations(p, prio)

merge

merge(base: dict[Any, Any], src: dict[Any, Any]) -> None

Merge two dictionaries recursively by copying/merging the key-value pairs from src into base.

Parameters:

  • base (dict[Any, Any]) –

    the base dictionary

  • src (dict[Any, Any]) –

    the source dictionary to read from

Source code in PyDrocsid/translations.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def merge(base: dict[Any, Any], src: dict[Any, Any]) -> None:
    """
    Merge two dictionaries recursively by copying/merging the key-value pairs from src into base.

    :param base: the base dictionary
    :param src: the source dictionary to read from
    """

    for k, v in src.items():
        if k in base and isinstance(v, dict) and isinstance(base[k], dict):
            # recurse if value in both src and base is a dictionary
            merge(base[k], v)
        else:
            base[k] = v