Skip to main content

Localisation helper for starlette

Project description

starlette-i18n

CI codecov pypi downloads versions license

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


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.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

starlette_i18n-2.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file starlette-i18n-2.1.0.tar.gz.

File metadata

  • Download URL: starlette-i18n-2.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for starlette-i18n-2.1.0.tar.gz
Algorithm Hash digest
SHA256 4e76c438999407c723dba0329ec16dace57f79dcd453503f126f5545a7af0f6b
MD5 8172e4943b284b6e6eb0935ed9c55914
BLAKE2b-256 920c7cf9fc854293a6e9886865b0923820c06cc881569a6b542a7664d9ac9232

See more details on using hashes here.

File details

Details for the file starlette_i18n-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_i18n-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c83876c95a6659a1b8768bab18ed3bb60290170c5ccb822011722bf34ab162b6
MD5 c88ff9e27cb57c151ee82cab1f752e71
BLAKE2b-256 3ebbdcbf0065c559a81c90c2772a0546297e78a334473685d0fb7107bf13fcd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page