Skip to content

Extension to Muffin that adds localization support with help of babel.

Notifications You must be signed in to change notification settings

klen/muffin-babel

Repository files navigation

Muffin-Babel

Muffin-Babel -- an extension to Muffin that adds localization support with help of Babel.

Tests Status PYPI Version Python Versions
  • python >= 3.9

Muffin-Babel should be installed using pip:

pip install muffin-babel

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!')

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.

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

The plugin adds two commands to your Muffin application.

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]

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-babel/issues

Development of Muffin-Babel happens at: https://github.com/klen/muffin-babel

  • klen (Kirill Klenov)

Licensed under a MIT license.

About

Extension to Muffin that adds localization support with help of babel.

Topics

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published