Localization support for the Muffin Framework
Project description
Muffin-Babel
Muffin-Babel is an extension for the Muffin web framework that adds internationalization (i18n) support using Babel.
Requirements
- Python >= 3.10
Installation
Install via pip:
pip install muffin-babel
Usage
Basic Setup
import muffin
import muffin_babel
app = muffin.Application("example")
# Initialize the plugin
babel = muffin_babel.Plugin()
babel.setup(app, local_folders=["src/locale"])
Inside a Route
@app.route("/")
async def index(request):
assert babel.current_locale
return babel.gettext("Hello World!")
Locale Selector
By default, locale is detected via the Accept-Language header. You can override it:
@babel.locale_selector
async def get_locale(request):
return request.query.get("lang") or await muffin_babel.select_locale_by_request(request, default="en")
Direct Use
@app.route("/")
def index(request):
return babel.gettext("Hello!")
Jinja2 Integration
If you're using the muffin-jinja2 plugin, Muffin-Babel automatically injects gettext and ngettext functions into your Jinja2 templates.
Plugin Options
| Option | Default | Description |
|---|---|---|
AUTO_DETECT_LOCALE |
True |
Middleware for automatic locale detection |
CONFIGURE_JINJA2 |
True |
Enable i18n support in Jinja2 templates |
DEFAULT_LOCALE |
"en" |
Default fallback locale |
DOMAIN |
"messages" |
Default domain name for translation files |
SOURCES_MAP |
— | File pattern to extractor method mapping |
OPTIONS_MAP |
— | Options for extractor (e.g., encoding) |
LOCAL_FOLDERS |
["locales"] |
Folders to search for translation files |
Options can be passed directly during setup:
babel.setup(app, default_locale="fr")
Or set via Muffin application config using the BABEL_ prefix:
BABEL_DEFAULT_LOCALE = "fr"
Note: Muffin config keys are case-insensitive.
Commands
The plugin adds commands to your Muffin app for message management.
Extract Messages
Extract localizable strings from your app source:
$ muffin app_package babel_extract_messages [OPTIONS] appdir
Compile Messages
Compile .po files into .mo binaries:
$ muffin app_package babel_compile_messages [OPTIONS]
Export as CSV
You can also export your .po files to CSV:
$ muffin app_package babel_export_csv
This helps with sending strings to translators or spreadsheets.
Contributing
Development happens at: https://github.com/klen/muffin-babel
Feel free to open issues or pull requests.
Bug Tracker
Found a bug? Have a suggestion? Report it here: 👉 https://github.com/klen/muffin-babel/issues
License
Licensed under the MIT license
Author
klen (Kirill Klenov)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file muffin_babel-1.7.0.tar.gz.
File metadata
- Download URL: muffin_babel-1.7.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c18ebff7479959c25a5d5629b9e682889a53e1bc48175a32c75c83cb7baec13
|
|
| MD5 |
5ff5e6f00b8c9e9b0b68e4f2f7949855
|
|
| BLAKE2b-256 |
185c2f731029da1119f6898f4ae1d9da70f353e9994339f024764df3cfba2a19
|
File details
Details for the file muffin_babel-1.7.0-py3-none-any.whl.
File metadata
- Download URL: muffin_babel-1.7.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b70835d514e209fab1a4498bff5f1e86219698b5d54a7c998b6dce4667cc6b39
|
|
| MD5 |
c183e1a93db9d9842308d5724bf9331a
|
|
| BLAKE2b-256 |
0a655f0118fce60493359bcae5003d38d88f6a172dbeb79a51e33b756ae419e5
|