Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASGI-Babel #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add ASGI-Babel #60

wants to merge 1 commit into from

Conversation

klen
Copy link

@klen klen commented Feb 9, 2021

Checklist

  • This project is explicitly related to ASGI.
  • The new list entry contains a project name, URL and description.

What is this project?

ASGI-Babel - Adds internationalization (i18n) support to ASGI applications (asyncio / trio)

Do you know about other similar projects?

Yes
If so, how is this one different?

No one internationalization tool is here


Anyone who agrees with this pull request can add a 👍.

@klen klen changed the title Add asgi-babel Add ASGI-Babel Feb 9, 2021
@florimondmanca florimondmanca added the please vote Vote to confirm interest in adding this project to the list label Feb 9, 2021
@florimondmanca
Copy link
Owner

Thanks, this one is quite exciting! I don't think there are equivalent tools like this one yet. I was personally eager to look into ASGI i18n some time ago to power French/English articles on my blog. :-)

Are there some examples of using this package in the wild yet?

@klen
Copy link
Author

klen commented Feb 9, 2021

@florimondmanca you able to find a little reference in the ASGI-Babel readme.

First, you need to extract messages from your python/templates/etc files using pybabel (http://babel.pocoo.org/en/latest/messages.html, http://babel.pocoo.org/en/latest/cmdline.html) and compile them. I'm not including any code for extracting/compiling because babel can do it and I don't think we need it inside the middleware.

After you have to add the middleware into your ASGI application (Im using asgi_tools.ResponseHTML here for simplicity, you are able to use starlette.HTMLResponse, etc or return ASGI messages manually):

from asgi_babel import BabelMiddleware, gettext
from asgi_tools import ResponseHTML

async def app(scope, send, receive):
    message = gettext('A string to translate')
    response = ResponseHTML(message)
    await response(scope, send, receive)

app = BabelMIddleware(app, locales_dirs=['path/to/your/locales'])

That's it. By default, BabelMiddleware parses a request's accept-language header and load the current user locale babel.Locale into asgi_babel.current_locale context variable. You are able to customize the method to get the current locale (from lang cookie for example, or from query string/path, etc). The middleware provides the methods to translate text: gettext, ngettext, pgettext, npgettext which are bound to the current locale. Also you can use the current locale to get user's timezones, first day of week, number formats, currencies and etc.

@klen
Copy link
Author

klen commented Feb 9, 2021

@florimondmanca also you are able to check a simple example here: https://github.com/klen/asgi-babel/blob/develop/example/__init__.py

Makefile contains instructions on how I extract the messages: https://github.com/klen/asgi-babel/blob/develop/Makefile#L67

In real life, you can use templates (jinja/etc) and extract locale messages from them. You will need to install asgi-babel callables to your jinja2 env and use i18n inside the templates after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
please vote Vote to confirm interest in adding this project to the list
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants