ASGI-Babel
###########
.. _description:
**asgi-babel** -- Adds internationalization (i18n) support to ASGI applications (Asyncio_ / Trio_ / Curio_)
.. _badges:
.. image:: https://github.com/klen/asgi-babel/workflows/tests/badge.svg
:target: https://github.com/klen/asgi-babel/actions
:alt: Tests Status
.. image:: https://img.shields.io/pypi/v/asgi-babel
:target: https://pypi.org/project/asgi-babel/
:alt: PYPI Version
.. image:: https://img.shields.io/pypi/pyversions/asgi-babel
:target: https://pypi.org/project/asgi-babel/
:alt: Python Versions
.. _contents:
.. contents::
.. _requirements:
Requirements
=============
- python >= 3.9
.. _installation:
Installation
=============
**asgi-babel** should be installed using pip: ::
pip install asgi-babel
Usage
=====
Common ASGI applications:
.. code:: python
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
.. code:: python
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:
.. code-block:: python
from curio.task import ContextTask
curio.run(main, taskcls=ContextTask)
Options
========
The middleware's options with default values:
.. code:: python
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
.. _bugtracker:
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:
Contributing
============
Development of the project happens at: https://github.com/klen/asgi-babel
.. _license:
License
========
Licensed under a `MIT license`_.
.. _links:
.. _ASGI-Tools: https://github.com/klen/asgi-tools
.. _Asyncio: https://docs.python.org/3/library/asyncio.html
.. _Curio: https://curio.readthedocs.io/en/latest/
.. _MIT license: http://opensource.org/licenses/MIT
.. _Trio: https://trio.readthedocs.io/en/stable/
.. _klen: https://github.com/klen
Raw data
{
"_id": null,
"home_page": null,
"name": "asgi-babel",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "locale, i18n, l10n, babel, gettext, translation, asyncio, trio, asgi",
"author": null,
"author_email": "Kirill Klenov <horneds@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/a4/cf/4b3cb372393c49f5c747ec1f76be8f0fcdd26c344a94436657cd343b32ca/asgi_babel-0.10.0.tar.gz",
"platform": null,
"description": "ASGI-Babel\n###########\n\n.. _description:\n\n**asgi-babel** -- Adds internationalization (i18n) support to ASGI applications (Asyncio_ / Trio_ / Curio_)\n\n.. _badges:\n\n.. image:: https://github.com/klen/asgi-babel/workflows/tests/badge.svg\n :target: https://github.com/klen/asgi-babel/actions\n :alt: Tests Status\n\n.. image:: https://img.shields.io/pypi/v/asgi-babel\n :target: https://pypi.org/project/asgi-babel/\n :alt: PYPI Version\n\n.. image:: https://img.shields.io/pypi/pyversions/asgi-babel\n :target: https://pypi.org/project/asgi-babel/\n :alt: Python Versions\n\n.. _contents:\n\n.. contents::\n\n.. _requirements:\n\nRequirements\n=============\n\n- python >= 3.9\n\n.. _installation:\n\nInstallation\n=============\n\n**asgi-babel** should be installed using pip: ::\n\n pip install asgi-babel\n\n\nUsage\n=====\n\nCommon ASGI applications:\n\n.. code:: python\n\n from asgi_babel import BabelMiddleware, current_locale, gettext\n\n async def my_app(scope, receive, send):\n \"\"\"Get a current locale.\"\"\"\n locale = current_locale.get().language.encode()\n hello_world = gettext('Hello World!').encode()\n\n await send({\"type\": \"http.response.start\", \"status\": 200})\n await send({\"type\": \"http.response.body\", \"body\": b\"Current locale is %s\\n\" % locale})\n await send({\"type\": \"http.response.body\", \"body\": hello_world})\n\n app = BabelMiddleware(my_app, locales_dirs=['tests/locales'])\n\n # http GET /\n #\n # Current_locale is en\n # Hello World!\n\n # http GET / \"accept-language: ft-CH, fr;q-0.9\"\n #\n # Current_locale is fr\n # Bonjour le monde!\n\nAs `ASGI-Tools`_ Internal middleware\n\n.. code:: python\n\n from asgi_tools import App\n from asgi_babel import BabelMiddleware, gettext\n\n app = App()\n app.middleware(BabelMiddleware.setup(locales_dirs=['tests/locales']))\n\n @app.route('/')\n async def index(request):\n return gettext('Hello World!')\n\n @app.route('/locale')\n async def locale(request):\n return current_locale.get().language\n\n\nUsage with Curio async library\n------------------------------\n\nThe `asgi-babel` uses context variable to set current locale. To enable the\ncontext variables with curio you have to run Curio_ with ``contextvars``\nsupport:\n\n.. code-block:: python\n\n from curio.task import ContextTask\n\n curio.run(main, taskcls=ContextTask)\n\n\nOptions\n========\n\nThe middleware's options with default values:\n\n.. code:: python\n\n from asgi_babel import BabelMiddleware\n\n app = BabelMiddleware(\n\n # Your ASGI application\n app,\n\n # Default locale\n default_locale='en',\n\n # A path to find translations\n locales_dirs=['locales']\n\n # A function with type: typing.Callable[[asgi_tools.Request], t.Awaitable[t.Optional[str]]]\n # which takes a request and default locale and return current locale\n locale_selector=asgi_babel.select_locale_by_request,\n\n )\n\n\nHow to extract & compile locales:\n=================================\n\nhttp://babel.pocoo.org/en/latest/messages.html\n\nhttp://babel.pocoo.org/en/latest/cmdline.html\n\n.. _bugtracker:\n\nBug tracker\n===========\n\nIf you have any suggestions, bug reports or\nannoyances please report them to the issue tracker\nat https://github.com/klen/asgi-babel/issues\n\n.. _contributing:\n\nContributing\n============\n\nDevelopment of the project happens at: https://github.com/klen/asgi-babel\n\n.. _license:\n\nLicense\n========\n\nLicensed under a `MIT license`_.\n\n\n.. _links:\n\n.. _ASGI-Tools: https://github.com/klen/asgi-tools\n.. _Asyncio: https://docs.python.org/3/library/asyncio.html\n.. _Curio: https://curio.readthedocs.io/en/latest/\n.. _MIT license: http://opensource.org/licenses/MIT\n.. _Trio: https://trio.readthedocs.io/en/stable/\n.. _klen: https://github.com/klen\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Internationalization (i18n) support for ASGI applications",
"version": "0.10.0",
"project_urls": {
"homepage": "https://github.com/klen/asgi-babel",
"repository": "https://github.com/klen/asgi-babel"
},
"split_keywords": [
"locale",
" i18n",
" l10n",
" babel",
" gettext",
" translation",
" asyncio",
" trio",
" asgi"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1d4de9556900606350dabf3d3f63b44c0d6079db721cc38832a84f799ab30b5a",
"md5": "38122d0fd525e11b1918ea10e0e6f048",
"sha256": "bed95420df1770b99ed9b476b8b6b576d46072d5b2b3208f0882e7cf1b0d020e"
},
"downloads": -1,
"filename": "asgi_babel-0.10.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "38122d0fd525e11b1918ea10e0e6f048",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5930,
"upload_time": "2024-07-31T13:14:37",
"upload_time_iso_8601": "2024-07-31T13:14:37.685801Z",
"url": "https://files.pythonhosted.org/packages/1d/4d/e9556900606350dabf3d3f63b44c0d6079db721cc38832a84f799ab30b5a/asgi_babel-0.10.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a4cf4b3cb372393c49f5c747ec1f76be8f0fcdd26c344a94436657cd343b32ca",
"md5": "adec8c09ed58d7c11972dad1e5048862",
"sha256": "7b049dd34d026428209552753d8b455dd4137a07fb35065d8bf9bddddf9c7ba9"
},
"downloads": -1,
"filename": "asgi_babel-0.10.0.tar.gz",
"has_sig": false,
"md5_digest": "adec8c09ed58d7c11972dad1e5048862",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 6710,
"upload_time": "2024-07-31T13:14:38",
"upload_time_iso_8601": "2024-07-31T13:14:38.782390Z",
"url": "https://files.pythonhosted.org/packages/a4/cf/4b3cb372393c49f5c747ec1f76be8f0fcdd26c344a94436657cd343b32ca/asgi_babel-0.10.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-31 13:14:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "klen",
"github_project": "asgi-babel",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "asgi-babel"
}