Skip to main content

Easy to use babel in FastAPI

Project description

FastAPI And Babel

FastAPI And Babel

FastAPIAndBabel allows you to easily use babel in your FastAPI projects and offers some features to improve and ease things.

Install

You can either download the source code of this repository or install it via pip:

pip install fastapi-and-babel

Usage

First, we create an instance of FastAPIAndBabel in our program as shown below ‍‍‍

from fastapi import FastAPI
from fastapi_and_babel.translator import FastAPIAndBabel


app = FastAPI()
translator = FastAPIAndBabel(__file__, app, "en")

We create the following mapping file babel.cfg

[python: **.py]

Save the file and then run the following command

pybabel extract -F babel.cfg -o messages.pot .

babel uses this file for mapping and stores the patterns in messages.pot

pybabel init -i messages.pot -d translations -l en

Here -d says to save translations in translations, you can change it in config

After adding your messages, use the following command to compile

pybabel compile -d translations

Now we can translate our texts using the gettext method

from fastapi_and_babel import gettext as _

@app.get("/")
def index():
    return {"text": _("Hello World!")}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fastapi_and_babel-0.0.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_and_babel-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_and_babel-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6a220fe9c922bdfaa130d8f6943775fc31fcc171d7b1f6598ed3e7da600d281e
MD5 02863fc2a343b0b6d9a79a551af55883
BLAKE2b-256 e44536f58d8f79db0dd54b831b6fd36e8fdd4236263392432a9bf524d00625a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page