All Projects → klen → asgi-babel

klen / asgi-babel

Licence: MIT license
Adds internationalization (i18n) support to ASGI applications (Asyncio/Trio)

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to asgi-babel

React Translated
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project 🌎🌍🌏
Stars: ✭ 176 (+738.1%)
Mutual labels:  i18n, localization
ad localize
ADLocalize is a simple way to manage your localization files. Supported wording sources : CSVs and Google Sheets. Localization file generation available for iOS, Android, JSON (i18next), YAML and Java properties
Stars: ✭ 22 (+4.76%)
Mutual labels:  i18n, localization
Localize Router
An implementation of routes localisation for Angular
Stars: ✭ 177 (+742.86%)
Mutual labels:  i18n, localization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: ✭ 12,869 (+61180.95%)
Mutual labels:  i18n, localization
Ttag
📙 simple approach for javascript localization
Stars: ✭ 243 (+1057.14%)
Mutual labels:  i18n, localization
Es2015 I18n Tag
ES2015 template literal tag for i18n and l10n (translation and internationalization)
Stars: ✭ 171 (+714.29%)
Mutual labels:  i18n, localization
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+12847.62%)
Mutual labels:  i18n, localization
Punic
PHP translation and localization made easy!
Stars: ✭ 133 (+533.33%)
Mutual labels:  i18n, localization
L10ns
Internationalization workflow and formatting
Stars: ✭ 234 (+1014.29%)
Mutual labels:  i18n, localization
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (+1004.76%)
Mutual labels:  i18n, localization
I18n Extract
Manage localization with static analysis. 🔍
Stars: ✭ 152 (+623.81%)
Mutual labels:  i18n, localization
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+1071.43%)
Mutual labels:  i18n, localization
Jquery.ime
jQuery based input methods library
Stars: ✭ 145 (+590.48%)
Mutual labels:  i18n, localization
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: ✭ 175 (+733.33%)
Mutual labels:  i18n, localization
Toolbelt.blazor.i18ntext
The class library that provides the ability to localize texts on your Blazor app!
Stars: ✭ 134 (+538.1%)
Mutual labels:  i18n, localization
Linguist
Easy multilingual urls and redirection support for the Laravel framework
Stars: ✭ 188 (+795.24%)
Mutual labels:  i18n, localization
React I18nify
Simple i18n translation and localization components and helpers for React.
Stars: ✭ 123 (+485.71%)
Mutual labels:  i18n, localization
Dom I18n
Provides a very basic HTML multilingual support using JavaScript
Stars: ✭ 125 (+495.24%)
Mutual labels:  i18n, localization
Serge
Continuous localization platform
Stars: ✭ 212 (+909.52%)
Mutual labels:  i18n, localization
Flutter translate
Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
Stars: ✭ 245 (+1066.67%)
Mutual labels:  i18n, localization

ASGI-Babel

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

Tests Status PYPI Version Python Versions

Requirements

  • python >= 3.7

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].