Documentation for PyI18n
class
Main i18n localization class
Attributes:
Name | Type | Description |
---|---|---|
available_locales |
tuple
|
list of available locales |
load_path |
str
|
path to locales directory |
_loaded_translations |
dict
|
(class attribute) dictionary of loaded translations |
Examples:
>>> from pyi18n import PyI18n
>>> pyi18n = PyI18n(("en", "jp"), "locales/")
>>> pyi18n.gettext("en", "hello.world")
'Hello, world!'
>>> pyi18n.gettext("jp", "hello.world")
'こんにちは、世界!'
Source code in pyi18n/pyi18n.py
18 19 20 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 |
|
__find(path, locale)
Find translation for given path and locale
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
path to translation |
required |
locale |
str
|
locale to get translation for |
required |
Returns:
Type | Description |
---|---|
Union[dict, str]
|
Union[dict, str]: translation str, dict or error message |
Source code in pyi18n/pyi18n.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
__init__(available_locales, load_path='locales/', loader=None)
Initialize i18n class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
available_locales |
tuple
|
list of available locales |
required |
load_path |
str
|
path to locales directory |
'locales/'
|
Return
None
Source code in pyi18n/pyi18n.py
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 |
|
__pyi18n_init()
validator and loader for translations
Raises:
Type | Description |
---|---|
ValueError
|
if locale is not available in self.available_locales |
FileNotFoundError
|
if translation file is not found |
Source code in pyi18n/pyi18n.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
get_loader()
Return loader class
Returns:
Name | Type | Description |
---|---|---|
PyI18nBaseLoader |
PyI18nBaseLoader
|
loader class |
Source code in pyi18n/pyi18n.py
129 130 131 132 133 134 135 136 |
|
gettext(locale, path, **kwargs)
Get translation for given locale and path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
locale |
str
|
locale to get translation for |
required |
path |
str
|
path to translation |
required |
**kwargs |
dict
|
interpolation variables |
{}
|
Returns:
Type | Description |
---|---|
Union[dict, str]
|
Union[dict, str]: translation str, dict or error message |
Raises:
Type | Description |
---|---|
ValueError
|
if locale is not in self.available_locales |
Source code in pyi18n/pyi18n.py
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 |
|