Localization support for the Muffin Framework
Project description
Muffin-Babel – an extension to Muffin that adds localization support with help of Babel.
Requirements
python >= 3.7
Installation
Muffin-Babel should be installed using pip:
pip install muffin-babel
Usage
Initialize and setup the plugin:
import muffin
import muffin_babel
# Create Muffin Application
app = muffin.Application('example')
# Initialize the plugin
# As alternative: babel = muffin_babel.Plugin(app, **options)
babel = muffin_babel.Plugin()
babel.setup(app, template_folders=['src/templates'])
# Use it inside your handlers
@app.route('/')
async def index(request):
# Get current locale
assert babel.current_locale
# Translate a text
return babel.gettext('Hello World!')
Setup a locale selector function (by default the plugin is parsing accept-language header):
@babel.locale_selector
async def get_locale(request):
""" Return locale either from request.query or from request headers. """
locale = request.query.get('lang')
if not locale:
return await muffin_babel.select_locale_by_request(request, default)
return locale
Use babel.gettext, babel.pgettext callables in your code:
@app.route('/')
def index(request):
return babel.gettext('Hello!')
Jinja2
The Muffin-Babel has integration with Muffin-Jinja2, so if you have muffin_jinja2 plugin enabled, the plugin provides gettext and ngettext function inside the Jinja2 templates’ context.
Options
Name |
Default Value |
Description |
---|---|---|
AUTO_DETECT_LOCALE |
True |
Installs a middleware to automatically detect users locales |
CONFIGURE_JINJA2 |
True |
Installs i18n support for jinja2 templates (through muffin-jinja) |
DEFAULT_LOCALE |
"en" |
Default locale |
DOMAIN |
"messages" |
Default localization domain |
SOURCES_MAP |
Babel sources map |
|
OPTIONS_MAP |
Babel options map |
You are able to provide the options when you are initiliazing the plugin:
babel.setup(app, default_locale='fr')
Or setup it inside Muffin.Application config using the BABEL_ prefix:
BABEL_DEFAULT_LOCALE = 'fr'
Muffin.Application configuration options are case insensitive
Commands
The plugin adds two commands to your Muffin application.
Extract messages
Extract strings from your application to locales:
$ muffin app_package babel_extract_messages [OPTIONS] appdir
Translate .po files and compile translations:
$ muffin app_package babel_compile_messages [OPTIONS]
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-babel/issues
Contributing
Development of Muffin-Babel happens at: https://github.com/klen/muffin-babel
Contributors
klen (Kirill Klenov)
License
Licensed under a MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file muffin-babel-0.7.9.tar.gz
.
File metadata
- Download URL: muffin-babel-0.7.9.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56edfe7d32424d77725231978aa7c13968dc9afef1d393954c2be962ce0d481d |
|
MD5 | d9810c9d7e9871332436671da303e276 |
|
BLAKE2b-256 | 1a4a7439ce4e1e8f2b28478bb2fd0d610b7707bff837e037cb22352f17790630 |
File details
Details for the file muffin_babel-0.7.9-py3-none-any.whl
.
File metadata
- Download URL: muffin_babel-0.7.9-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d765d6ca616adcfe21686d82b91ea5894a90d1a12ccaf2a2a18babf1cc87e76 |
|
MD5 | 4a2e84b421463a9ab310ec6de486bba8 |
|
BLAKE2b-256 | 27450df6cf76ca368927caba41b88079fcd1d1647256d479cea85b84b05f1b51 |