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. 82/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.4.1.tar.gz (36.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_translations-0.4.1-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_translations-0.4.1.tar.gz.

File metadata

  • Download URL: pydantic_translations-0.4.1.tar.gz
  • Upload date:
  • Size: 36.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.4

File hashes

Hashes for pydantic_translations-0.4.1.tar.gz
Algorithm Hash digest
SHA256 49d30123baf4ca736bed89ecf1e9dfc9e9d7cf2d8c1aeaf99ff828d98574c1f9
MD5 c623cbdf83e103b0892ab55f03d9ec77
BLAKE2b-256 f86b30ca4cc0a82d500af29670a344332c50401b8b1eefdc5df12fa4f15fdfba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_translations-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8793c3d04b5697c73f1c8e0893a2ddfbebeff6d430c942d3ae66b8c38f94424d
MD5 330ed8eb2fbfaa5fcd1babd529214d06
BLAKE2b-256 93e634b204e1164b9f4cc1d2d45089caad6fd6071e381cafd7fd37c3f11f97ce

See more details on using hashes here.

Supported by

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