Skip to main content

PyPI - Downloads

error message with i18n support in FastAPI

response example

{
  "errors": [
    {
      "loc": [
        "body",
        "string"
      ],
      "msg": "確保此值最多有 10 個字符",
      "type": "value_error.any_str.max_length",
      "ctx": {
        "limit_value": 10
      }
    },
    {
      "loc": [
        "body",
        "nested",
        "body"
      ],
      "msg": "確保此值最多有 2 個字符",
      "type": "value_error.any_str.max_length",
      "ctx": {
        "limit_value": 2
      }
    },
    {
      "loc": [
        "body",
        "nested",
        "inner",
        "inner_body"
      ],
      "msg": "確保此值最多包含 2 個項目",
      "type": "value_error.list.max_items",
      "ctx": {
        "limit_value": 2
      }
    },
    {
      "loc": [
        "body",
        "nested",
        "inner",
        "cat"
      ],
      "msg": "鑑別器 'color' 和值 'ccc' 不匹配(允許的值: 'black', 'white')",
      "type": "value_error.discriminated_union.invalid_discriminator",
      "ctx": {
        "discriminator_key": "color",
        "discriminator_value": "ccc",
        "allowed_values": "'black', 'white'"
      }
    }
  ]
}

parameters

all parameters are optional

param description default
locale_path the path of your locale files locales
locale_list support locales in your app in tuple ('zh-TW', 'ja-JP', 'en-US')
bind_to_life_span set to True if you want the translator instance be created when on app startup False

Attention

  • For FastAPI >=0.100.0 and pydantic v2, please use ^0.4.0
  • For FastAPI < 0.100.0 nad pydantic v1, please use ^0.3.0
  • built-in locales are zh-TW, en-US, ja-JP, you can change the locales by yourself

How to run

  • use setup
from fastapi_validation_i18n import setup
from fastapi import FastAPI
app = FastAPI()
setup(app, locale_path=..., locale_list=...)
  • use middleware and exception handler
from fastapi import FastAPI
from fastapi_validation_i18n import I18nMiddleware, i18n_exception_handler
from fastapi.exceptions import RequestValidationError

app = FastAPI()

app.add_middleware(I18nMiddleware, locale_path='your-publish-path')

app.add_exception_handler(
    RequestValidationError,
    i18n_exception_handler
)

Other

  • publish locales to your app path
# default to "locale" in your project path
uv run publish-locale <your-path> [--locale]
  • how to set locale

there are 3 ways to set locale

  1. set accept-language header to your request
  2. set an API with locale in path
  3. set locale query parameter to your request

you can see the example for more detail

Release files for fastapi-validation-i18n 0.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-validation-i18n 0.4.3
File Size Uploaded
fastapi_validation_i18n-0.4.3.tar.gz 25.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-validation-i18n 0.4.3
File Interpreter ABI Platform
fastapi_validation_i18n-0.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 42.4 kB

Release files / fastapi_validation_i18n-0.4.3.tar.gz

Download URL fastapi_validation_i18n-0.4.3.tar.gz
Size 25.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c1bfe69743e6ddfc8b932cdb0425f7edba53455c8727e9384645da93f4e8f97a
BLAKE2b-256 checksum
How to use checksums
d89d6211c8ff1c649ea6a8e22b362e61a8eb9545e1571eae21e07e995022fa13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / fastapi_validation_i18n-0.4.3-py3-none-any.whl

Download URL fastapi_validation_i18n-0.4.3-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d762165de5a96dd1a159a224f0a14a752a76cf6a3ac9d14f7d3c1f5f6301f12
BLAKE2b-256 checksum
How to use checksums
4c8881c4f08692027f4279c75c36a46d0b6babbe4911ca5786b276ba78e1d129
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.4.3 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.1.61

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page