Skip to main content

Localization support for the Muffin Framework

Project description

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

Tests Status PYPI Version Python Versions

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muffin-babel-1.2.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

muffin_babel-1.2.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file muffin-babel-1.2.1.tar.gz.

File metadata

  • Download URL: muffin-babel-1.2.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for muffin-babel-1.2.1.tar.gz
Algorithm Hash digest
SHA256 5d189b3175d2f8606b58f97626cda07b4912acc3f9d61daffdf1a8e99aa62c49
MD5 cd34970d0e46f685480c9a6e11472826
BLAKE2b-256 fb05aa8bdab59b6a8b14d08f9349295fd4639c9d35583ed05c90e8ffacca3bc2

See more details on using hashes here.

File details

Details for the file muffin_babel-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for muffin_babel-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52b6e797fc4b05211f20420f18390db2e698424424ffa15752c2c840e721ba1b
MD5 98627d6e57ef22cac4f4875c54fc3530
BLAKE2b-256 8a0eefa3e978da606835e5d6b25808d6beb87c64f297321ef3600a2007ae631b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page