Internationalization (i18n) support for ASGI applications
Project description
asgi-babel – Adds internationalization (i18n) support to ASGI applications (Asyncio / Trio / Curio)
Requirements
python >= 3.9
Installation
asgi-babel should be installed using pip:
pip install asgi-babel
Usage
Common ASGI applications:
from asgi_babel import BabelMiddleware, current_locale, gettext
async def my_app(scope, receive, send):
"""Get a current locale."""
locale = current_locale.get().language.encode()
hello_world = gettext('Hello World!').encode()
await send({"type": "http.response.start", "status": 200})
await send({"type": "http.response.body", "body": b"Current locale is %s\n" % locale})
await send({"type": "http.response.body", "body": hello_world})
app = BabelMiddleware(my_app, locales_dirs=['tests/locales'])
# http GET /
#
# Current_locale is en
# Hello World!
# http GET / "accept-language: ft-CH, fr;q-0.9"
#
# Current_locale is fr
# Bonjour le monde!
As ASGI-Tools Internal middleware
from asgi_tools import App
from asgi_babel import BabelMiddleware, gettext
app = App()
app.middleware(BabelMiddleware.setup(locales_dirs=['tests/locales']))
@app.route('/')
async def index(request):
return gettext('Hello World!')
@app.route('/locale')
async def locale(request):
return current_locale.get().language
Usage with Curio async library
The asgi-babel uses context variable to set current locale. To enable the context variables with curio you have to run Curio with contextvars support:
from curio.task import ContextTask
curio.run(main, taskcls=ContextTask)
Options
The middleware’s options with default values:
from asgi_babel import BabelMiddleware
app = BabelMiddleware(
# Your ASGI application
app,
# Default locale
default_locale='en',
# A path to find translations
locales_dirs=['locales']
# A function with type: typing.Callable[[asgi_tools.Request], t.Awaitable[t.Optional[str]]]
# which takes a request and default locale and return current locale
locale_selector=asgi_babel.select_locale_by_request,
)
How to extract & compile locales:
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/asgi-babel/issues
Contributing
Development of the project happens at: https://github.com/klen/asgi-babel
License
Licensed under a MIT license.
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
Built Distribution
File details
Details for the file asgi_babel-0.10.0.tar.gz
.
File metadata
- Download URL: asgi_babel-0.10.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b049dd34d026428209552753d8b455dd4137a07fb35065d8bf9bddddf9c7ba9 |
|
MD5 | adec8c09ed58d7c11972dad1e5048862 |
|
BLAKE2b-256 | a4cf4b3cb372393c49f5c747ec1f76be8f0fcdd26c344a94436657cd343b32ca |
File details
Details for the file asgi_babel-0.10.0-py3-none-any.whl
.
File metadata
- Download URL: asgi_babel-0.10.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bed95420df1770b99ed9b476b8b6b576d46072d5b2b3208f0882e7cf1b0d020e |
|
MD5 | 38122d0fd525e11b1918ea10e0e6f048 |
|
BLAKE2b-256 | 1d4de9556900606350dabf3d3f63b44c0d6079db721cc38832a84f799ab30b5a |