Skip to main content

FastAPI Internationalization (i18n)

Project description

FastAPI Internationalization (i18n)

Build Status Sonarcloud Status PyPI - Version LICENSE status: active

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"

# To ignore the Accept-Language header for certain referes set:
export FASTAPI_I18N__IGNORE_REFERES="https://api.quality.ohsome.org/v1/docs"

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

Roadmap

  • Support configuration via pyproject.toml
  • Validate locale string
  • Support setting locale using query parameter
  • Support configuration of domain (currently defaults to "messages")

Alternatives

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_i18n-0.9.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

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

fastapi_i18n-0.9.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_i18n-0.9.0.tar.gz.

File metadata

  • Download URL: fastapi_i18n-0.9.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for fastapi_i18n-0.9.0.tar.gz
Algorithm Hash digest
SHA256 abc2d58cf46ac73c7d4b1d37a413e61bf0f2d93584b94e52c5b9ca538c500c40
MD5 3e8697fef7b2114b5a14fbd9210e57b1
BLAKE2b-256 c0bbda93ac13a585f0e15a5f0bc3cc8aece9a2dd425b7e40d79f6e86b442205b

See more details on using hashes here.

File details

Details for the file fastapi_i18n-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_i18n-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffcf792598b2ebbb6e3a54d3031c6b65a00d96697920224e670042566f1af0e5
MD5 03b4a2125f830464234dbbd56b850308
BLAKE2b-256 d9bb39f7b7b45188687dca512333214dfe574e6ab6c452bda22780646be989f8

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