ASGI-Babel
asgi-babel – Adds internationalization (i18n) support to ASGI applications (Asyncio / Trio / Curio)
Requirements
python >= 3.10
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.
Metadata
Release files for asgi-babel 0.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asgi_babel-0.11.0.tar.gz | 6.7 kB | Without provenance | Details for asgi_babel-0.11.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asgi_babel-0.11.0-py3-none-any.whl | Python 3 | none | any | Details for asgi_babel-0.11.0-py3-none-any.whl |
Total release size: 12.6 kB
Release files / asgi_babel-0.11.0.tar.gz
| Download URL | asgi_babel-0.11.0.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
330f7e6a489476160d092ee3bf9ac24780206e582558c8dc8a503d3894406092
|
|
BLAKE2b-256 checksum How to use checksums |
d81bde2423e5e08f331c95d480dd7582705585d96625e9a3e043381384a9bdea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / asgi_babel-0.11.0-py3-none-any.whl
| Download URL | asgi_babel-0.11.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c713b99093bfdc9a0fe8ee4d54bbbf3e7408328a98d2b9946a6b82d07ad372d
|
|
BLAKE2b-256 checksum How to use checksums |
7f29076d4751c4757ed9bd47ec1bb80c73ed04f97a974c88731a6f130698d458
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for asgi-babel 0.11.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.