Skip to main content

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

  1. The original error messages provided by @samuelcolvin and pydantic contributors.
  2. The Russian translation is provided by @orsinium.
  3. 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

pydantic-translations-0.2.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

pydantic_translations-0.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-translations-0.2.0.tar.gz.

File metadata

File hashes

Hashes for pydantic-translations-0.2.0.tar.gz
Algorithm Hash digest
SHA256 eea3732d5cbb3a917d0f40a0c87e66ac73f0655b457d500d3035a9dab76be78a
MD5 fd53c7f53e8842689389b32044a5d29d
BLAKE2b-256 e579ba547fc5a310f8f2ebaefbf0153621aab19c4a06a7148cbcc4062e53f90c

See more details on using hashes here.

File details

Details for the file pydantic_translations-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_translations-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d17acf20fc524f7176630468f105d69b2039c66f55a995be7bf6dc657dbe474
MD5 654f74a6fe3486d2f5640916e08ad8fd
BLAKE2b-256 8a232f3a0c22be7782c2b1163a60d6c2f607fba60143ba59ac7a2d569e925322

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page