pydantic-translations


Namepydantic-translations JSON
Version 0.2.2 PyPI version JSON
download
home_pageNone
SummaryTranslations for pydantic errors.
upload_time2023-10-27 13:59:10
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords mypy typing annotations type annotations
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
python3 -m pip install pydantic-translations
```

## Usage

Let's say you have a pydantic model `User`:

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

```python
from pydantic_translations import Translator

tr = Translator('ru')
```

You can use translator as a context manager to translate pydantic exceptions raised from the context:

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

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

```python
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](https://github.com/orsinium-labs/l10n) locale with your translations:

```python
from l10n import Locales

locales = Locales()
locale = locales['ua']
tr = Translator(locale)
```

## Contributors

1. The original error messages provided by [@samuelcolvin](https://github.com/samuelcolvin) and [pydantic contributors](https://github.com/pydantic/pydantic/graphs/contributors).
1. The Russian translation is provided by [@orsinium](https://github.com/orsinium).
1. The German, Spanish, French, Italian, and Dutch translations are provided by [Andovar](https://andovar.com/) translation agency.

Minor corrections and improvements are provided by [the project contributors](https://github.com/orsinium-labs/pydantic-translations/graphs/contributors).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pydantic-translations",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "mypy,typing,annotations,type annotations",
    "author": null,
    "author_email": "Gram <git@orsinium.dev>",
    "download_url": "https://files.pythonhosted.org/packages/98/f5/291154e6598a2b1b05815d88992d8330e11ed777d2cab602b9f9a71bbe2e/pydantic_translations-0.2.2.tar.gz",
    "platform": null,
    "description": "# pydantic-translations\n\nTranslations for pydantic errors.\n\n## Languages\n\nCurrently, we have translated pydantic v1.10.2 errors to the following languages:\n\n* `de`: German. 87/87.\n* `es`: Spanish. 87/87.\n* `fr`: French. 87/87.\n* `it`: Italian. 87/87.\n* `nl`: Dutch. 87/87.\n* `ru`: Russian. 82/87.\n\nNeed more languages? Contributions are welcome!\n\n## Installation\n\n```bash\npython3 -m pip install pydantic-translations\n```\n\n## Usage\n\nLet's say you have a pydantic model `User`:\n\n```python\nfrom pydantic import BaseModel\n\nclass User(BaseModel):\n    age: int\n```\n\nThe translations are managed by the `Translator` class that is instantiated with the locale (language) you want the messages to be translated to:\n\n```python\nfrom pydantic_translations import Translator\n\ntr = Translator('ru')\n```\n\nYou can use translator as a context manager to translate pydantic exceptions raised from the context:\n\n```python\nwith tr:\n    User.parse_obj({'age': 'idk'})\n# ValidationError: 1 validation error for User\n# age\n#   \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c (type=type_error.integer)\n```\n\nOr use the `translate_exception` method to directly translate an exception instance:\n\n```python\nfrom pydantic import ValidationError\n\ntry:\n    User.parse_obj({'age': 'idk'})\nexcept ValidationError as exc:\n    exc = tr.translate_exception(exc)\n    raise exc\n```\n\nOr use the `translate_error` method to translate a specific error:\n\n```python\ntry:\n    User.parse_obj({'age': 'idk'})\nexcept ValidationError as exc:\n    err = exc.errors()[0]\n    err = tr.translate_error(err)\n    print(err)\n# {'loc': ('age',), 'msg': '\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c', 'type': 'type_error.integer'}\n```\n\n## Custom translations\n\nIf 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](https://github.com/orsinium-labs/l10n) locale with your translations:\n\n```python\nfrom l10n import Locales\n\nlocales = Locales()\nlocale = locales['ua']\ntr = Translator(locale)\n```\n\n## Contributors\n\n1. The original error messages provided by [@samuelcolvin](https://github.com/samuelcolvin) and [pydantic contributors](https://github.com/pydantic/pydantic/graphs/contributors).\n1. The Russian translation is provided by [@orsinium](https://github.com/orsinium).\n1. The German, Spanish, French, Italian, and Dutch translations are provided by [Andovar](https://andovar.com/) translation agency.\n\nMinor corrections and improvements are provided by [the project contributors](https://github.com/orsinium-labs/pydantic-translations/graphs/contributors).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Translations for pydantic errors.",
    "version": "0.2.2",
    "project_urls": {
        "Source": "https://github.com/orsinium-labs/pydantic-translations"
    },
    "split_keywords": [
        "mypy",
        "typing",
        "annotations",
        "type annotations"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "641f8d5bb5791350d5e11bf8925383aa588e92f4a1cbc5e62b783494bd0d214c",
                "md5": "f260b7a3320d6e380ddef8504d5f9c5c",
                "sha256": "1b5445d9c7d113b810d42ca0876148f4a1372d91759c98507c7c434a1f958037"
            },
            "downloads": -1,
            "filename": "pydantic_translations-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f260b7a3320d6e380ddef8504d5f9c5c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 29569,
            "upload_time": "2023-10-27T13:59:08",
            "upload_time_iso_8601": "2023-10-27T13:59:08.729094Z",
            "url": "https://files.pythonhosted.org/packages/64/1f/8d5bb5791350d5e11bf8925383aa588e92f4a1cbc5e62b783494bd0d214c/pydantic_translations-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "98f5291154e6598a2b1b05815d88992d8330e11ed777d2cab602b9f9a71bbe2e",
                "md5": "ea5c9079c1a86829c33f637a8da4055f",
                "sha256": "d9000242d640f8d4e4b2e4b263df39bce9afb6cf023185f65271053e28907c56"
            },
            "downloads": -1,
            "filename": "pydantic_translations-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "ea5c9079c1a86829c33f637a8da4055f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 32755,
            "upload_time": "2023-10-27T13:59:10",
            "upload_time_iso_8601": "2023-10-27T13:59:10.784826Z",
            "url": "https://files.pythonhosted.org/packages/98/f5/291154e6598a2b1b05815d88992d8330e11ed777d2cab602b9f9a71bbe2e/pydantic_translations-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-27 13:59:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "orsinium-labs",
    "github_project": "pydantic-translations",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-translations"
}
        
Elapsed time: 0.15352s