Skip to main content

Multilingual support middleware for FastAPI using gettext and Tortoise ORM

Project description

FastAPI18n

Features:

✅ Middleware for handling multilingual requests in FastAPI
✅ Supports gettext for internationalization
✅ Multilingual fields for Tortoise ORM models
Aerich integration for database migrations
✅ Auto-translate missing text using Google Translate API
✅ Fully compatible with FastAPI Admin
Context-aware localization: Stores locale using contextvars, ensuring per-session isolation

Installation

pip install fastapi18n

Usage Example

from fastapi import FastAPI
from fastapi18n.middlewares import LocalizationMiddleware
from fastapi18n.wrappers import TranslationWrapper

app = FastAPI()
app.add_middleware(LocalizationMiddleware)
_ = TranslationWrapper.get_instance().gettext

TranslationWrapper.init(
    locales_dir="locales",
    languages=[("en", "English"), ("fr", "French")],
    language="en"
)


@app.get("/")
async def read_root():
    return {"message": _("Hello, world!")}

Multilingual ORM Fields with Tortoise ORM

Database Integration

When using Tortoise ORM, multilingual fields will be automatically created for each language during database migrations.

from tortoise import fields
from fastapi18n.decorators import multilangual_model
from tortoise.models import Model


@multilangual_model({"name", "description"})
class Product(Model):
    # for IDE
    name = fields.CharField(max_length=100)
    description = fields.TextField()

    id = fields.IntField(pk=True)

What happens during migration?

When running migrations (aerich migrate), the following fields will be automatically generated:

from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS "job" (
    "id" SERIAL NOT NULL PRIMARY KEY,
    "name_en" VARCHAR(100) NOT NULL,
    "name_ru" VARCHAR(100) NOT NULL,
    "description_en" TEXT NOT NULL,
    "description_ru" TEXT NOT NULL,
);"""

Fields access

FastAPI18n allows you to dynamically access multilingual fields based on the currently active locale.

Example Usage

# Access based on the current locale:
from fastapi18n.wrappers import TranslationWrapper

product = Product()
activate = TranslationWrapper.get_instance().set_locale
# Set English as the active language and assign a value
activate("en")
product.name = "Example Name"

# Switch to French and assign a value in French
activate("fr")
product.name = "Nom d'exemple"

# Switch back to English and retrieve the stored value
activate("en")
print(product.name)  # Returns 'Example Name' in the selected language

Passing Language in API Requests

FastAPI18n provides multiple ways to define the user 's language when making API requests.

1️⃣ Using Query Parameters

You can pass the language directly in the URL using the lang parameter.

Example

GET /api/resource?lang=fr

This tells the middleware to use French (fr) for this request.

2️⃣ Using Accept-Language Header

You can also define the language via the Accept-Language header.

curl -H "Accept-Language: fr" http://localhost:8000/api/resource

3️⃣Active Language in Code

In code you can activate language with

from fastapi18n.wrappers import TranslationWrapper

activate = TranslationWrapper.get_instance().set_locale

activate("en")

🎛 Command-Line Interface (CLI)

FastAPI18n provides a CLI for managing translations. You can run the following commands in your terminal:

Available Commands

Command Description
fastapi18n makemessages Extracts translatable strings and creates .po files
fastapi18n compilemessages Compiles .mo files for runtime usage
fastapi18n translate Automatically translates missing text using Google Translate

Command Arguments

Argument Description
-d, --dir Specifies the locale directory (default: locales/)
-l, --languages Defines languages as a `
-o, --only Translates only selected languages (comma-separated, e.g., "en,fr")
-e, --exclude Excludes specific languages from translation (comma-separated, e.g., "de,it")
-s, --source_lang Default messages language in code ("en")

Using .env for Default Settings**

You can store default values for makemessages and compilemessages in a .env file:

Example .env file

LOCALES_DIR=locales
LANGUAGES=en|fr
SOURCE_TRANSLATE_LANGUAGE=en # default "en"

Examples

Extract translatable messages and create .po files

fastapi18n makemessages -d locales -l en|fr|es

This extracts all translatable strings and generates .po files for English, French, and Spanish.

Auto-translate missing text using Google Translate

fastapi18n translate -o fr,de

This translates missing strings only for French and German.

Exclude a language from auto-translation

fastapi18n translate -e en

This translates all missing strings except for English.

🌍 Setting up Google Cloud Translate

FastAPI18n supports automatic translation of missing texts using Google Cloud Translate API.

Step 1: Create a Google Cloud Project

  1. Go to the Google Cloud Console.
  2. Click on Select a projectNew Project.
  3. Give your project a name and click Create.

Step 2: Enable Cloud Translation API

  1. Open the Cloud Translation API page.
  2. Click Enable.

Step 3: Generate API Key

  1. Go to Google Cloud Credentials.
  2. Click Create CredentialsAPI Key.
  3. Copy the generated API Key.

Step 4: Set Up Environment Variables

FastAPI18n requires the Google API key to be stored in an environment variable.

Method 1: Set in .env file

GOOGLE_APPLICATION_CREDENTIALS=your-google-api-key

Method 2: Export in Terminal (for Linux/macOS)

export GOOGLE_APPLICATION_CREDENTIALS="your-google-api-key"

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

fastapi18n-0.1.3.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi18n-0.1.3-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi18n-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi18n-0.1.3.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for fastapi18n-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cb7a23f3062122744bc07a00cd1f5d5bc968aad909459378cd1e701d5cc78eea
MD5 6f64131dcb840dc2a0a76cce8d25b9ad
BLAKE2b-256 7aae892841a323f07e9881f0c83de39148cc9caa7834e38e78b945b4222fa75a

See more details on using hashes here.

File details

Details for the file fastapi18n-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: fastapi18n-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for fastapi18n-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf569888a294e4e732dfc202d50fc78a9682a389fa5b774a4f23bb3674189844
MD5 65004542733cc8da4ddbb2579f16cb52
BLAKE2b-256 04c08b658a944f5017128b5b993af8bbaab5c7fa14522ec3e70be8e38693b7f9

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