Localisation helper for starlette
Project description
starlette-i18n
starlette-i18n is a localisation helper for starlette.
Installation
starlette-i18n is available on PyPI. Use pip to install:
$ pip install starlette-i18n
Basic Usage
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette_i18n import (
DEFAULT_LOCALE,
LANGUAGE_HEADER,
LocaleFromHeaderMiddleware,
load_gettext_translations,
)
from starlette_i18n import gettext_lazy as _
BABEL_DOMAIN = "messages"
BABEL_LOCALES_PATH = "locales"
def init_app():
load_gettext_translations(directory=BABEL_LOCALES_PATH, domain=BABEL_DOMAIN)
app_ = Starlette()
app_.add_middleware(
LocaleFromHeaderMiddleware,
language_header=LANGUAGE_HEADER,
default_code=DEFAULT_LOCALE
)
@app_.route("/")
def success(request):
return PlainTextResponse(_("OK"), status_code=200)
return app_
app = init_app()
if __name__ == "__main__":
uvicorn.run(app=app)
License
starlette-i18n is developed and distributed under the Apache 2.0 license.
Reporting a Security Vulnerability
See our security policy.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
starlette-i18n-2.0.0.tar.gz
(10.5 kB
view hashes)
Built Distribution
Close
Hashes for starlette_i18n-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 774a23621f1356a598d5a5c9b61cdbde4054964d8035c6bcc9b45a872aae9b29 |
|
MD5 | 3b86a7c043b9dd1a8936b0837fb12730 |
|
BLAKE2b-256 | 68595a2ea4fee1842c2a20665efa78e272ba21b981b87d70e4bedfe179e18d93 |