FastAPI Internationalization (i18n)
This package is implemented as a FastAPI dependency which initializes translations using the gettext module and makes them available throughout the request lifecycle using a Conext Variable.
Installation
uv add fastapi-i18n
Prerequisites
A locale directory adhering to the GNU gettext message catalog API containing translated messages. See chapter on Babel for more details.
Configuration
export FASTAPI_I18N__LOCALE_DIR="paht/to/locale/dir" # required
export FASTAPI_I18N__LOCALE_DEFAULT="de" # defaults to "en"
Usage
from fastapi import FastAPI, Depends
from fastapi_i18n import i18n, _
app = FastAPI(dependencies=[Depends(i18n)])
@app.get("/")
def root():
return _("Hello from fastapi-i18n!")
Set Accept-Language header for requests to get a translated version of the response.
For a complete example see tests.
Babel
Babel is useful for working with GNU gettext message catalogs.
To add new locale and use babel to extract messages from Python files run:
echo "[python: **.py]" > babel.cfg
pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d locale -l de
# Now translate messages in locale/de/LC_MESSAGES/messages.po
# Then compile locale:
pybabel compile -d locale
To update existing locale run update instead of init run:
pybabel extract -F babel.cfg -o messages.pot .
pybabel update -i messages.pot -d locale
Development Setup
uv run pre-commit install
uv run pytest
uv run pybabel compile -d tests/locale
FASTAPI_I18N__LOCALE_DIR=tests/locale uv run --with fastapi[standard] fastapi dev tests/main.py
Roadmap
- Support configuration via
pyproject.toml - Validate locale string
- Support setting locale using query parameter
- Support configuration of domain (currently defaults to "messages")
Alternatives
Metadata
Release files for fastapi-i18n 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_i18n-0.12.0.tar.gz | 19.1 kB | Without provenance | Details for fastapi_i18n-0.12.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_i18n-0.12.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_i18n-0.12.0-py3-none-any.whl |
Total release size: 38.6 kB
Release files / fastapi_i18n-0.12.0.tar.gz
| Download URL | fastapi_i18n-0.12.0.tar.gz |
|---|---|
| Size | 19.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e3b3a0e29c842ef4bf40a7b60f9e002c3e63578897c5fae2f7e2fe54bc8e3bc
|
|
BLAKE2b-256 checksum How to use checksums |
80d615c5a77130c6bde5485d376b07d4a9c9c1b20f96433dc411dd83d1e6b25b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Release files / fastapi_i18n-0.12.0-py3-none-any.whl
| Download URL | fastapi_i18n-0.12.0-py3-none-any.whl |
|---|---|
| Size | 19.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20f0fcc2d8b5d66cf1dd562253927ebd8e0e1979d01c3fe73fad5412d1e572b5
|
|
BLAKE2b-256 checksum How to use checksums |
9dc0b5eb0a56fccf2e478e220f18d8e66e7305eb82a5f66ca4ec872dd23e0b7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Security for fastapi-i18n 0.12.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.