Easy to use babel in FastAPI
Project description
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
Built Distribution
File details
Details for the file fastapi_and_babel-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: fastapi_and_babel-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26688df50a0b854171cbf6b0070edb43f2a94659ea920f564570a86325381f48 |
|
MD5 | 6b025922cd1ffbcda76121665efec82a |
|
BLAKE2b-256 | e9927ac9760ec56cc1e2dc6af4e5bfc446023dcc94e76d739d1f6e09238a6b84 |