Translations for pydantic errors.
Project description
pydantic-translations
Translations for pydantic errors.
Languages
Currently, we have translated pydantic v1.10.2 errors to the following languages:
de
: German. 87/87.es
: Spanish. 87/87.fr
: French. 87/87.it
: Italian. 87/87.nl
: Dutch. 87/87.ru
: Russian. 70/87.
Need more languages? Contributions are welcome!
Installation
python3 -m pip install pydantic-translations
Usage
Let's say you have a pydantic model User
:
from pydantic import BaseModel
class User(BaseModel):
age: int
The translations are managed by the Translator
class that is instantiated with the locale (language) you want the messages to be translated to:
from pydantic_translations import Translator
tr = Translator('ru')
You can use translator as a context manager to translate pydantic exceptions raised from the context:
with tr:
User.parse_obj({'age': 'idk'})
# ValidationError: 1 validation error for User
# age
# значение должно быть целым числом (type=type_error.integer)
Or use the translate_exception
method to directly translate an exception instance:
from pydantic import ValidationError
try:
User.parse_obj({'age': 'idk'})
except ValidationError as exc:
exc = tr.translate_exception(exc)
raise exc
Or use the translate_error
method to translate a specific error:
try:
User.parse_obj({'age': 'idk'})
except ValidationError as exc:
err = exc.errors()[0]
err = tr.translate_error(err)
print(err)
# {'loc': ('age',), 'msg': 'значение должно быть целым числом', 'type': 'type_error.integer'}
Custom translations
If you have translated the errors to a new language, the best you can do is contribute it back here. If, for some (legal?) reason, you can't, you may pass into the Translated as a locale a l10n locale with your translations:
from l10n import Locales
locales = Locales()
locale = locales['ua']
tr = Translator(locale)
Contributors
- The original error messages provided by @samuelcolvin and pydantic contributors.
- The Russian translation is provided by @orsinium.
- The German, Spanish, French, Italian, and Dutch translations are provided by Andovar translation agency.
Minor corrections and improvements are provided by the project contributors.
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
File details
Details for the file pydantic-translations-0.2.1.tar.gz
.
File metadata
- Download URL: pydantic-translations-0.2.1.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d538ba7c385d83b311b29c9f7f066cca39fa6d44f74106a11af5f9021b9c79fa |
|
MD5 | 5fd18cbec3a7a4cb2c96a064c4f92528 |
|
BLAKE2b-256 | 50f5c67403579c13d0b3132053352830fa5cde63656d29f07afee62f38b63fcb |
File details
Details for the file pydantic_translations-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_translations-0.2.1-py3-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8fec69da406edd2dcc410cab91f1eae935d9d84d95c5a5bd5496895637fdaac |
|
MD5 | 6ad21dd698fc978162ab3ee2fa04d6e5 |
|
BLAKE2b-256 | cb5ff3ee33807ae0fe26864fa44051635bece50ce7b5b21490bb3762db3a31b8 |