Metadata-Version: 2.4
Name: marshmallow-i18n-messages
Version: 0.2.0
Summary: A patch to marshmallow to allow to translate marshmallow messages via GNU gettext
Project-URL: Homepage, https://github.com/oarepo/marshmallow-i18n-messages
Project-URL: Bug Tracker, https://github.com/oarepo/marshmallow-i18n-messages/issues
Author-email: Miroslav Simek <miroslav.simek@cesnet.cz>
License: MIT License
        
        Copyright (C) 2024 CESNET z.s.p.o.
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
        of the Software, and to permit persons to whom the Software is furnished to do
        so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.12
Requires-Dist: babel
Requires-Dist: marshmallow
Requires-Dist: marshmallow-utils
Requires-Dist: speaklater
Provides-Extra: dev
Requires-Dist: polib; extra == 'dev'
Provides-Extra: tests
Requires-Dist: pytest; extra == 'tests'
Description-Content-Type: text/markdown

# marshmallow-i18n-messages

A simple library to provide i18n messages for marshmallow validation errors.

## Installation

```shell
pip install marshmallow-i18n-messages
```

## Usage

Call `add_i18n_to_marshmallow` once at application startup, **before** any
marshmallow schemas are imported or instantiated:

```python
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`:

```python
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 the `lazy_gettext` from `invenio_i18n`
* Registers an `app_loaded` signal. In this signal it processes marshmallow schemas from
  all registered invenio services which might have been already cached before the 
  `add_i18n_to_marshmallow` was 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`:

```python
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:

1. Fork the repository and create a feature branch.
2. Add your language to `build.sh` and create an empty `messages.po` file at
   `marshmallow_i18n_messages/translations/<language>/LC_MESSAGES/messages.po`.
3. Run `build.sh` to regenerate the POT template and merge it into your PO
   file.
4. Translate the messages in the generated `.po` file.
5. Run `build.sh` again to compile the translations.
6. Commit the changes, push to your fork, and open a pull request.
