Skip to main content

Internationalization (i18n) support for ASGI applications

Project description

asgi-babel – Adds internationalization (i18n) support to ASGI applications (Asyncio / Trio / Curio)

Tests Status PYPI Version Python Versions

Requirements

  • python >= 3.8

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:

http://babel.pocoo.org/en/latest/messages.html

http://babel.pocoo.org/en/latest/cmdline.html

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi-babel-0.9.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

asgi_babel-0.9.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file asgi-babel-0.9.3.tar.gz.

File metadata

  • Download URL: asgi-babel-0.9.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for asgi-babel-0.9.3.tar.gz
Algorithm Hash digest
SHA256 f3d449250a20151ca65a32f8eeef43e6ad928bba80220554aca50a55a3da0b96
MD5 f6ae63edd507e6a2e0b8a5c4534c2338
BLAKE2b-256 9fa9b85549a4af536504f221fe3fd4b222e02a66e766a24347cc308fbaf6b04f

See more details on using hashes here.

File details

Details for the file asgi_babel-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: asgi_babel-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for asgi_babel-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1c65c30d9a182d4bc91bea05a5d61ecff256ed01f8070cfdc230947222ecc085
MD5 0c5a3d7b38015b3ea5ca852a71b35025
BLAKE2b-256 befd462f28cc2c850d55b8b53968c366652761873fa4b43d6a08b0444fb7dbbb

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