DocsPython

Quickstart - Python

Add Generic Translation to a Flask, FastAPI or Django app with the Python SDK and translate it with the generic-translation-py command.

On this page

The Python SDK is a port of @generic-translation/core and @generic-translation/node. Message syntax and content hashes are byte-compatible with the JavaScript packages, so a translation map written by the dashboard, the CDN or a JS build works unchanged in a Python app. This page gets a Flask app translated; FastAPI and Django are covered in the reference.

1. Install

The generic-translation Python package is not on PyPI yet. Install it from the repository. Once it is published, pip install generic-translation and the extras (generic-translation[flask], generic-translation[fastapi], generic-translation[django], generic-translation[async]) will work.

pip install "generic-translation[flask] @ git+https://github.com/generic-translation/generic-translation-python"

Python 3.9 or newer. The only required dependency is Babel (CLDR plural rules and number/date data). The source is in generic-translation-python.

2. Initialize and call t()

from flask import Flask
from generic_translation import initialize_generic_translation, t
from generic_translation.flask import GenericTranslation

initialize_generic_translation(
    default_locale="en",
    locales=["es", "ja"],
    translations_dir="translations",  # <locale>.json maps, written by `generic-translation-py translate`
)

app = Flask(__name__)
GenericTranslation(app)


@app.get("/")
def home():
    return t("Hello {name}", name="Ada")

The source string is the key: t() hashes it (and an optional _context), looks the hash up in the locale’s translation map, and falls back to formatting the source text. A missing or malformed translation never raises. Pass string literals so the extractor can find them.

The Flask extension negotiates the locale from ?locale=, then the generic-translation-locale cookie, then Accept-Language, then your default, and binds it for the request.

3. Configure and translate

generic-translation-py reads the same generic-translation.config.json as the JavaScript CLI:

{
  "projectId": "prj_...",
  "defaultLocale": "en",
  "locales": ["es", "ja"],
  "src": ["app.py", "templates/**/*.html"],
  "output": "translations/[locale].json"
}
GENERIC_TRANSLATION_API_KEY=gt-api-... generic-translation-py translate

This extracts messages from your Python files and Jinja/Django templates, submits them, waits for the job, downloads the results and writes translations/es.json, translations/ja.json and the source map translations/en.json. Restart the app, or wait out the cache TTL (60 seconds by default), and request /?locale=es.

Preview first with generic-translation-py extract (add --json for machine output) and generic-translation-py translate --dry-run.

What to know

  • The JavaScript generic-translation CLI cannot read .py files, which is why this package ships generic-translation-py. See the JS CLI for JavaScript sources and for whole-file translation (generic-translation-py handles messages only).
  • Numeric dates passed to {d, date} are epoch seconds, not milliseconds as in JavaScript.
  • Formatting comes from Babel’s CLDR data, not Node’s ICU, so a few outputs differ from JavaScript. They are listed in the reference.

Next: Python reference.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.