htmy.i18n
TranslationResource = Mapping[str, Any]
module-attribute
Translation resource type.
I18n
Bases: ContextAware
Context-aware async internationalization utility.
Source code in htmy/i18n.py
__init__(path, fallback=None)
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to the root directory that contains the translation resources. |
required |
fallback
|
str | Path | None
|
Optional fallback path to use if |
None
|
Source code in htmy/i18n.py
get(dotted_path, key, **kwargs)
async
Returns the translation resource at the given location.
If keyword arguments are provided, it's expected that the referenced data is a format string which can be fully formatted using the given keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dotted_path
|
str
|
A package-like (dot separated) path to the file that contains
the required translation resource, relative to |
required |
key
|
str
|
The key in the translation resource whose value is requested. Use dots to reference embedded attributes. |
required |
Returns:
Type | Description |
---|---|
Any
|
The loaded value. |
Raises:
Type | Description |
---|---|
I18nError
|
If the given translation resource is not found or invalid. |
Source code in htmy/i18n.py
load_translation_resource(path)
async
Loads the translation resource from the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
The path of the translation resource to load. |
required |
Returns:
Type | Description |
---|---|
TranslationResource
|
The loaded translation resource. |
Raises:
Type | Description |
---|---|
I18nValueError
|
If the translation resource is not a JSON dict. |
Source code in htmy/i18n.py
resolve_json_path(root, dotted_subpath)
Resolves the given dotted subpath relative to root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Path
|
The root path. |
required |
dotted_subpath
|
str
|
Subpath under |
required |
Returns:
Type | Description |
---|---|
Path
|
The resolved path. |
Raises:
Type | Description |
---|---|
I18nValueError
|
If the given dotted path is invalid. |