A patch to marshmallow to allow to translate marshmallow messages via GNU gettext
Project description
marshmallow-i18n-messages
A simple library to provide i18n messages for marshmallow validation errors.
Installation
pip install marshmallow-i18n-messages
Usage
Call add_i18n_to_marshmallow once at application startup, before any
marshmallow schemas are imported or instantiated:
from marshmallow_i18n_messages import add_i18n_to_marshmallow
add_i18n_to_marshmallow()
After this call all marshmallow validation errors will be translated to the active GNU gettext locale at the moment the error is rendered.
Enabling i18n for schemas loaded before add_i18n_to_marshmallow
If a schema class was imported and cached before add_i18n_to_marshmallow
was called, its error messages will not have been wrapped yet. Enable i18n on
it explicitly with enable_i18n:
from marshmallow_i18n_messages import enable_i18n
enable_i18n(MyAlreadyLoadedSchema)
enable_i18n is idempotent — calling it on a schema that is already
patched is safe and has no effect. It also recurses into parent classes and
any nested schemas automatically.
Invenio / InvenioRDM usage
This package ships with an integrated support for InvenioRDM repositories and in most cases, no extra configuration is necessary to activate the translations.
This package adds an invenio_config.module entrypoint that is loaded
when InvenioRDM configuration is loaded. This entrypoint:
- Calls
add_i18n_to_marshmallow(lazy_gettext)with thelazy_gettextfrominvenio_i18n - Registers an
app_loadedsignal. In this signal it processes marshmallow schemas from all registered invenio services which might have been already cached before theadd_i18n_to_marshmallowwas called.
If you encounter a schema that is neither covered by the global patch above
nor by the built-in service-schema finalizer, patch it explicitly in
invenio.cfg:
from mypackage import MyUncoveredSchema
from marshmallow_i18n_messages import enable_i18n
from invenio_i18n import lazy_gettext
enable_i18n(MyUncoveredSchema, lazy_gettext)
If you find such a gap in the built-in coverage, please open an issue so it can be addressed in a future release.
Contributing new translations
To contribute new translations or fix translation issues:
- Fork the repository and create a feature branch.
- Add your language to
build.shand create an emptymessages.pofile atmarshmallow_i18n_messages/translations/<language>/LC_MESSAGES/messages.po. - Run
build.shto regenerate the POT template and merge it into your PO file. - Translate the messages in the generated
.pofile. - Run
build.shagain to compile the translations. - Commit the changes, push to your fork, and open a pull request.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file marshmallow_i18n_messages-0.2.0.tar.gz.
File metadata
- Download URL: marshmallow_i18n_messages-0.2.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdaf4bd3c4d8460599570b7366d75402aa59fdac70e3c05c3905798c386ce7af
|
|
| MD5 |
a91c7488e7c21334d09b9b7b89271bf2
|
|
| BLAKE2b-256 |
e7be07ebe5b9309d6ed5289a47be2b2831ceab0755952efbc0fdb20e67f31430
|
File details
Details for the file marshmallow_i18n_messages-0.2.0-py3-none-any.whl.
File metadata
- Download URL: marshmallow_i18n_messages-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3412cea8376fc4d98d88525d9375f2addfd127740b16b3ecfee9939bb7821b8
|
|
| MD5 |
8f3917e17a5a8f3e57a222ff2b5d2316
|
|
| BLAKE2b-256 |
4ca7f666a527ea8dd516e1dddddfa142fe50f0e66c3392d4623119403b56ef4a
|