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. 0/87.es
: Spanish. 0/87.fr
: French. 0/87.it
: Italian. 0/87.nl
: Dutch. 0/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.1.0.tar.gz
.
File metadata
- Download URL: pydantic-translations-0.1.0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6966dd592fa985e3b8297e4ef9e925abd7501dcb6cb67e59dd8f94c52abbf5c8 |
|
MD5 | 534b6d1321fc3f8aa33c5567d0dc97ef |
|
BLAKE2b-256 | 0e672e895891f8abd6f0f1e5fb3237bc8411e06116429b28262a93437615caf0 |
File details
Details for the file pydantic_translations-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_translations-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75a911af22dcd19f84ac91f497fefe22c58374d02ca44a84c88134798d589673 |
|
MD5 | eaae05af4ef1b8fc0e87829031bc4a82 |
|
BLAKE2b-256 | aabf398d8d3a98b06f33fc54aa4bd69bd2c97a7c4f317ec00627b48d5fb1418b |