Skip to main content

Language localization fastapi

Project description

fastapi_localization

fastapi_localization - provides a simple language localization from Accept-Language header in your application.

Installation

$ pip install fastapi-localization

Example with small fastapi application

from typing import List

from pydantic import BaseModel
from fastapi_localization import TranslateJsonResponse
from fastapi_localization import TranslatableStringField

class LanguageTranslatableSchema(BaseModel):
    code: str
    title: TranslatableStringField


@app.get(
    '/language',
    response_class=TranslateJsonResponse,
    response_model=List[LanguageTranslatableSchema])
async def languages():
    return [{'code': 'ru', 'title': 'Russia'},
            {'code': 'en', 'title': 'English'}]
# Russia
$ curl --location --request GET 'http://127.0.0.1:8000/language' \
--header 'Accept-Language: ru'

[{"code":"ru","title":"Русский"},{"code":"en","title":"Английский"}]


# English
$ curl --location --request GET 'http://127.0.0.1:8000/language' \
--header 'Accept-Language: en'

[{"code":"ru","title":"Russia"},{"code":"en","title":"English"}]

manual partial translation

from fastapi_localization import TranslateJsonResponse
from fastapi_localization import lazy_gettext as _

@app.get(
    '/country',
    response_class=TranslateJsonResponse)
async def countries():
    return [{'code': 'ru', 'title': _('Russia')},
            {'code': 'us', 'title': 'USA'}]
# Russian
$ curl --location --request GET 'http://127.0.0.1:8000/country' \
--header 'Accept-Language: ru'

[{"code":"ru","title":"Русский"},{"code":"us","title":"USA"}]


# English
$ curl --location --request GET 'http://127.0.0.1:8000/country' \
--header 'Accept-Language: en'

[{"code":"ru","title":"Russia"},{"code":"us","title":"USA"}]

error validation

from pydantic import BaseModel, EmailStr
from fastapi_localization import TranslateJsonResponse

class InputSchema(BaseModel):
    email = EmailStr()


@app.post(
    '/input',
    response_class=TranslateJsonResponse)
async def countries(value: InputSchema):
    return value
# Russia
$ curl --location --request POST 'http://127.0.0.1:8000/input' \
--header 'Accept-Language: ru' \
--header 'Content-Type: application/json' \
--data-raw '{"email": "invalid_email"}'

{
    "detail": [
        {
            "loc": [
                "body",
                "email"
            ],
            "msg": "значение не является действительным адресом электронной почты",
            "type": "value_error.email"
        }
    ]
}


# English
$ curl --location --request POST 'http://127.0.0.1:8000/input' \
--header 'Accept-Language: en' \
--header 'Content-Type: application/json' \
--data-raw '{"email": "invalid_email"}'

{
    "detail": [
        {
            "loc": [
                "body",
                "email"
            ],
            "msg": "value is not a valid email address",
            "type": "value_error.email"
        }
    ]
}

Application source code

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

fastapi-localization-0.0a2.dev3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_localization-0.0a2.dev3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-localization-0.0a2.dev3.tar.gz.

File metadata

  • Download URL: fastapi-localization-0.0a2.dev3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.5

File hashes

Hashes for fastapi-localization-0.0a2.dev3.tar.gz
Algorithm Hash digest
SHA256 ade508d602083d948a90a5fdf259c24f7b55609437c2180cc26da23df41e05c3
MD5 79a2d57b59fafad256c49d671794b91e
BLAKE2b-256 1ffa6dd859ec41cd413b0a913ca75b82a6284c1e40b7ab2e2b7e2b8fcdbd0ff3

See more details on using hashes here.

File details

Details for the file fastapi_localization-0.0a2.dev3-py3-none-any.whl.

File metadata

  • Download URL: fastapi_localization-0.0a2.dev3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.5

File hashes

Hashes for fastapi_localization-0.0a2.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 512160db955a2cdc077c11e682f09f113c2a4cca3f8d570627a5d8ed1f341417
MD5 bc4d2528518f06716a485b960708c090
BLAKE2b-256 a978a0f3144138c1ba20c1bec19ad46247f7aec1638d0a9f1751cf94baeea702

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