Skip to content

Documentation for loaders module

This module defines the PyI18n loaders which load translations from files in YAML or JSON format.

LoaderType

Enum for the different loader types.

Source code in pyi18n/loaders.py
14
15
16
17
18
class LoaderType:
    """ Enum for the different loader types. """
    BASE: str = "base"
    YAML: str = "yaml"
    JSON: str = "json"

PyI18nBaseLoader

PyI18n Base Loader class, supports yaml and json

Attributes:

Name Type Description
load_path str

path to translations

namespaced bool

tells loader should look for namespaces

_type str

loader type

Source code in pyi18n/loaders.py
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class PyI18nBaseLoader:
    """ PyI18n Base Loader class, supports yaml and json

    Attributes:
        load_path (str): path to translations
        namespaced (bool): tells loader should look for namespaces

        _type (str): loader type
    """

    _type: str = LoaderType.BASE

    def __init__(self,
                 load_path: str = "locales/",
                 namespaced: bool = False
                 ) -> None:

        """ Initialize loader class

        Args:
            load_path (str): path to translations
            namespaced (bool): namespaces support

        Return:
            None
        """
        self.load_path: str = load_path
        self.namespaced: bool = namespaced

    def load(self, locales: tuple, ser_mod: object) -> dict:
        """ Load translations for given locales,
            should be overridden in child classes.

        Args:
            locales (tuple): locales to load

        Return:
            dict: loaded translations

        Notes:
            Custom load function should be implemented
            in child classes and return python dict
        """

        file_extension: str = ser_mod.__name__.replace('yaml', 'yml')

        loaded: dict = {}
        for locale in locales:

            file_path: str = f"{self.load_path}{locale}.{file_extension}"
            if not exists(file_path):
                continue

            try:
                loaded[locale] = self.__load_file(file_path,
                                                  file_extension,
                                                  ser_mod,
                                                  locale
                                                  )
            except (json.decoder.JSONDecodeError, yaml.YAMLError):
                continue

        return loaded

    def __load_file(self,
                    file_path: str,
                    ext: str,
                    ser_mod: object,
                    locale: str
                    ) -> dict:
        """ loads content, should not be called directly

        Return:
            dict: loaded content
        """
        with open(file_path, 'r', encoding="utf-8") as _f:
            load_params: dict = {"Loader": yaml.FullLoader} \
                if ext == "yml" else {}

            return ser_mod.load(_f, **load_params)[locale]

    def _load_namespaced(self, locales: tuple, ser_mod: Any) -> dict:
        """Load translations from namespaces.

        Should be overridden in child classes.
        This will look for a locale (directories) and load all namespaces.

        Args:
            locales (tuple): locales to load
            ser_mod (object): module for serialization

        Return:
            dict: loaded translations
        """
        loaded: dict = {}
        for locale in locales:
            path: str = join(self.load_path, locale)
            loaded_locale: dict = load_locale(path, ser_mod, self._type)

            if not loaded_locale:
                continue

            loaded.setdefault(locale, loaded_locale)

        return loaded

    def type(self) -> str:
        """ Return loader type

        Return:
            str: loader type
        """
        return self._type

    def get_path(self) -> str:
        """ Return loader path

        Return:
            str: loader path
        """
        return self.load_path

__init__(load_path='locales/', namespaced=False)

Initialize loader class

Parameters:

Name Type Description Default
load_path str

path to translations

'locales/'
namespaced bool

namespaces support

False
Return

None

Source code in pyi18n/loaders.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(self,
             load_path: str = "locales/",
             namespaced: bool = False
             ) -> None:

    """ Initialize loader class

    Args:
        load_path (str): path to translations
        namespaced (bool): namespaces support

    Return:
        None
    """
    self.load_path: str = load_path
    self.namespaced: bool = namespaced

__load_file(file_path, ext, ser_mod, locale)

loads content, should not be called directly

Return

dict: loaded content

Source code in pyi18n/loaders.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __load_file(self,
                file_path: str,
                ext: str,
                ser_mod: object,
                locale: str
                ) -> dict:
    """ loads content, should not be called directly

    Return:
        dict: loaded content
    """
    with open(file_path, 'r', encoding="utf-8") as _f:
        load_params: dict = {"Loader": yaml.FullLoader} \
            if ext == "yml" else {}

        return ser_mod.load(_f, **load_params)[locale]

get_path()

Return loader path

Return

str: loader path

Source code in pyi18n/loaders.py
135
136
137
138
139
140
141
def get_path(self) -> str:
    """ Return loader path

    Return:
        str: loader path
    """
    return self.load_path

load(locales, ser_mod)

Load translations for given locales, should be overridden in child classes.

Parameters:

Name Type Description Default
locales tuple

locales to load

required
Return

dict: loaded translations

Notes

Custom load function should be implemented in child classes and return python dict

Source code in pyi18n/loaders.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def load(self, locales: tuple, ser_mod: object) -> dict:
    """ Load translations for given locales,
        should be overridden in child classes.

    Args:
        locales (tuple): locales to load

    Return:
        dict: loaded translations

    Notes:
        Custom load function should be implemented
        in child classes and return python dict
    """

    file_extension: str = ser_mod.__name__.replace('yaml', 'yml')

    loaded: dict = {}
    for locale in locales:

        file_path: str = f"{self.load_path}{locale}.{file_extension}"
        if not exists(file_path):
            continue

        try:
            loaded[locale] = self.__load_file(file_path,
                                              file_extension,
                                              ser_mod,
                                              locale
                                              )
        except (json.decoder.JSONDecodeError, yaml.YAMLError):
            continue

    return loaded

type()

Return loader type

Return

str: loader type

Source code in pyi18n/loaders.py
127
128
129
130
131
132
133
def type(self) -> str:
    """ Return loader type

    Return:
        str: loader type
    """
    return self._type

PyI18nJsonLoader

Bases: PyI18nBaseLoader

PyI18n JSON Loader class

Attributes:

Name Type Description
load_path str

path to translations

namespaced bool

tells loader should look for namespaces

_type str

loader type

Source code in pyi18n/loaders.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class PyI18nJsonLoader(PyI18nBaseLoader):
    """ PyI18n JSON Loader class

    Attributes:
        load_path (str): path to translations
        namespaced (bool): tells loader should look for namespaces

        _type (str): loader type
    """

    _type: str = LoaderType.JSON

    def load(self, locales: tuple) -> dict:
        """ Load translations for given locales using json

        Inherits from PyI18nBaseLoader

        Args:
            locales (tuple): locales to load

        Return:
            dict: loaded translations
        """

        if self.namespaced:
            return super()._load_namespaced(locales, json)

        return super().load(locales, json)

load(locales)

Load translations for given locales using json

Inherits from PyI18nBaseLoader

Parameters:

Name Type Description Default
locales tuple

locales to load

required
Return

dict: loaded translations

Source code in pyi18n/loaders.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def load(self, locales: tuple) -> dict:
    """ Load translations for given locales using json

    Inherits from PyI18nBaseLoader

    Args:
        locales (tuple): locales to load

    Return:
        dict: loaded translations
    """

    if self.namespaced:
        return super()._load_namespaced(locales, json)

    return super().load(locales, json)

PyI18nYamlLoader

Bases: PyI18nBaseLoader

PyI18n YAML Loader class

Attributes:

Name Type Description
load_path str

path to translations

namespaced bool

tells loader should look for namespaces

_type str

loader type

Source code in pyi18n/loaders.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class PyI18nYamlLoader(PyI18nBaseLoader):
    """ PyI18n YAML Loader class

    Attributes:
        load_path (str): path to translations
        namespaced (bool): tells loader should look for namespaces

        _type (str): loader type
    """

    _type: str = LoaderType.YAML

    def load(self, locales: tuple) -> dict:
        """ Load translations for given locales using yaml

        Inherits from PyI18nBaseLoader

        Args:
            locales (tuple): locales to load

        Return:
            dict: loaded translations
        """
        if self.namespaced:
            return super()._load_namespaced(locales, yaml)

        return super().load(locales, yaml)

load(locales)

Load translations for given locales using yaml

Inherits from PyI18nBaseLoader

Parameters:

Name Type Description Default
locales tuple

locales to load

required
Return

dict: loaded translations

Source code in pyi18n/loaders.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def load(self, locales: tuple) -> dict:
    """ Load translations for given locales using yaml

    Inherits from PyI18nBaseLoader

    Args:
        locales (tuple): locales to load

    Return:
        dict: loaded translations
    """
    if self.namespaced:
        return super()._load_namespaced(locales, yaml)

    return super().load(locales, yaml)