API reference
i18n.StringFormatter
Source code in src/i18n/formatter.py
8 9 10 11 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 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 |
|
i18n.StringFormatter.__init__
__init__(global_context: dict[str, Any]) -> None
Initializes the StringFormatter with a global context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
global_context
|
dict[str, Any]
|
A dictionary containing global context variables. |
required |
Source code in src/i18n/formatter.py
9 10 11 12 13 14 15 16 |
|
i18n.StringFormatter._resolve_expr
_resolve_expr(expr: str, **kwargs: Any) -> str
Resolves an expression based on the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr
|
str
|
The expression to resolve. |
required |
**kwargs
|
Any
|
Additional context variables. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The resolved expression or an error message if the expression cannot be resolved. |
Source code in src/i18n/formatter.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
i18n.StringFormatter._call_function
_call_function(expr: str, **kwargs: Any) -> str
Calls a function based on the expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr
|
str
|
The function expression to call. |
required |
**kwargs
|
Any
|
Additional context variables. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The result of the function call or an error message if the function cannot be called. |
Source code in src/i18n/formatter.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 |
|
i18n.StringFormatter._get_object_attr
_get_object_attr(expr: str, **kwargs: Any) -> str
Gets an object's attribute based on the expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr
|
str
|
The object attribute expression to resolve. |
required |
**kwargs
|
Any
|
Additional context variables. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The value of the object's attribute or an error message if the attribute cannot be resolved. |
Source code in src/i18n/formatter.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
i18n.StringFormatter.format
format(text: str, **kwargs: Any) -> str
Formats a string by replacing placeholders with context values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text containing placeholders to format. |
required |
**kwargs
|
Any
|
Additional context variables. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The formatted string with placeholders replaced by context values. |
Source code in src/i18n/formatter.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
i18n.I18N
Internationalization class for managing translations.
Source code in src/i18n/i18n.py
11 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 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 |
|
i18n.I18N.available_locales
property
available_locales: set[str]
Get the set of available locales.
Returns:
Type | Description |
---|---|
set[str]
|
set[str]: The set of available locales. |
i18n.I18N.__init__
__init__(
default_locale: str, load_path: str = "locales/"
) -> None
Initialize the I18N class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default_locale
|
str
|
The default locale to use. |
required |
load_path
|
str
|
The path to the directory containing locale files. Defaults to "locales/". |
'locales/'
|
Source code in src/i18n/i18n.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
i18n.I18N.load
load() -> None
Load translations from locale files.
Source code in src/i18n/i18n.py
33 34 35 36 37 38 39 40 41 42 |
|
i18n.I18N._get_nested_translation
_get_nested_translation(
data: dict[str, Any], key: str
) -> Optional[str]
Retrieve a nested translation from the data dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
The dictionary containing translations. |
required |
key
|
str
|
The key for the desired translation. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The nested translation or None if not found. |
Source code in src/i18n/i18n.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
i18n.I18N.register_function
register_function(
name: str, func: Callable[..., Any]
) -> None
Register a custom function for use in translations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the function. |
required |
func
|
Callable
|
The function to register. |
required |
Source code in src/i18n/i18n.py
62 63 64 65 66 67 68 69 |
|
i18n.I18N.register_constant
register_constant(name: str, value: Any) -> None
Register a constant for use in translations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the constant. |
required |
value
|
Any
|
The value of the constant. |
required |
Source code in src/i18n/i18n.py
71 72 73 74 75 76 77 78 |
|
i18n.I18N.t
t(locale: str, key: str, **kwargs: Any) -> str
Translate a key for a given locale.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
locale
|
str
|
The locale to use for translation. |
required |
key
|
str
|
The key to translate. |
required |
**kwargs
|
Any
|
Additional keyword arguments for formatting the translation. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The translated string or the key if no translation is found. |
Source code in src/i18n/i18n.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|