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.2.tar.gz (16.8 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.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi18n-0.1.2.tar.gz
  • Upload date:
  • Size: 16.8 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.2.tar.gz
Algorithm Hash digest
SHA256 d5997f00f437c06c1a85c3e91c554fdff652a1fbea00bfc79a78ea2f17c639c6
MD5 a93a182341f198877ae2cd139aacc18a
BLAKE2b-256 363eb5c4f373734b25f4fbb5708f962af8e580a2ec0ed52c6b750b96e0a22e48

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi18n-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49210a5b80221dd657e062fbc7ab5c6de86f5308df66aebf3d65fb604c878a38
MD5 99363156f25dd5f48114985412f73e3e
BLAKE2b-256 fff83009f0c356a26dee401e9e334130248021195e959b922d21e209e083b3f5

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