muffin-babel


Namemuffin-babel JSON
Version 1.6.0 PyPI version JSON
download
home_pagehttps://github.com/klen/muffin-babel
SummaryLocalization support for the Muffin Framework
upload_time2025-07-17 10:51:28
maintainerNone
docs_urlNone
authorKirill Klenov
requires_python<4.0,>=3.10
licenseMIT
keywords localization internationalization muffin babel asyncio trio asgi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Muffin-Babel

[![Tests](https://github.com/klen/muffin-babel/workflows/tests/badge.svg)](https://github.com/klen/muffin-babel/actions)
[![PyPI Version](https://img.shields.io/pypi/v/muffin-babel)](https://pypi.org/project/muffin-babel/)
[![Python Versions](https://img.shields.io/pypi/pyversions/muffin-babel)](https://pypi.org/project/muffin-babel/)

**Muffin-Babel** is an extension for the [Muffin](https://github.com/klen/muffin) web framework that adds internationalization (i18n) support using [Babel](http://babel.edgewall.org/).

---

## Requirements

- Python >= 3.10

---

## Installation

Install via pip:

```bash
pip install muffin-babel
```

---

## Usage

### Basic Setup

```python
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

```python
@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:

```python
@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

```python
@app.route("/")
def index(request):
    return babel.gettext("Hello!")
```

---

## Jinja2 Integration

If you're using the [`muffin-jinja2`](https://github.com/klen/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:

```python
babel.setup(app, default_locale="fr")
```

Or set via Muffin application config using the `BABEL_` prefix:

```python
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:

```bash
$ muffin app_package babel_extract_messages [OPTIONS] appdir
```

### Compile Messages

Compile `.po` files into `.mo` binaries:

```bash
$ muffin app_package babel_compile_messages [OPTIONS]
```

---

## Export as CSV

You can also export your `.po` files to CSV:

```bash
$ 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](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](https://github.com/klen/muffin-babel/issues)

---

## License

Licensed under the [MIT license](http://opensource.org/licenses/MIT)

---

## Author

**[klen](https://github.com/klen)** (Kirill Klenov)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klen/muffin-babel",
    "name": "muffin-babel",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "localization, internationalization, muffin, babel, asyncio, trio, asgi",
    "author": "Kirill Klenov",
    "author_email": "horneds@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/df/51/6b273b34e23959b491cc77adceb0aec06fe98651f68ee56408d2ffd92ebe/muffin_babel-1.6.0.tar.gz",
    "platform": null,
    "description": "# Muffin-Babel\n\n[![Tests](https://github.com/klen/muffin-babel/workflows/tests/badge.svg)](https://github.com/klen/muffin-babel/actions)\n[![PyPI Version](https://img.shields.io/pypi/v/muffin-babel)](https://pypi.org/project/muffin-babel/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/muffin-babel)](https://pypi.org/project/muffin-babel/)\n\n**Muffin-Babel** is an extension for the [Muffin](https://github.com/klen/muffin) web framework that adds internationalization (i18n) support using [Babel](http://babel.edgewall.org/).\n\n---\n\n## Requirements\n\n- Python >= 3.10\n\n---\n\n## Installation\n\nInstall via pip:\n\n```bash\npip install muffin-babel\n```\n\n---\n\n## Usage\n\n### Basic Setup\n\n```python\nimport muffin\nimport muffin_babel\n\napp = muffin.Application(\"example\")\n\n# Initialize the plugin\nbabel = muffin_babel.Plugin()\nbabel.setup(app, local_folders=[\"src/locale\"])\n```\n\n### Inside a Route\n\n```python\n@app.route(\"/\")\nasync def index(request):\n    assert babel.current_locale\n    return babel.gettext(\"Hello World!\")\n```\n\n### Locale Selector\n\nBy default, locale is detected via the `Accept-Language` header. You can override it:\n\n```python\n@babel.locale_selector\nasync def get_locale(request):\n    return request.query.get(\"lang\") or await muffin_babel.select_locale_by_request(request, default=\"en\")\n```\n\n### Direct Use\n\n```python\n@app.route(\"/\")\ndef index(request):\n    return babel.gettext(\"Hello!\")\n```\n\n---\n\n## Jinja2 Integration\n\nIf you're using the [`muffin-jinja2`](https://github.com/klen/muffin-jinja2) plugin, `Muffin-Babel` automatically injects `gettext` and `ngettext` functions into your Jinja2 templates.\n\n---\n\n## Plugin Options\n\n| Option               | Default       | Description                               |\n| -------------------- | ------------- | ----------------------------------------- |\n| `AUTO_DETECT_LOCALE` | `True`        | Middleware for automatic locale detection |\n| `CONFIGURE_JINJA2`   | `True`        | Enable i18n support in Jinja2 templates   |\n| `DEFAULT_LOCALE`     | `\"en\"`        | Default fallback locale                   |\n| `DOMAIN`             | `\"messages\"`  | Default domain name for translation files |\n| `SOURCES_MAP`        | \u2014             | File pattern to extractor method mapping  |\n| `OPTIONS_MAP`        | \u2014             | Options for extractor (e.g., encoding)    |\n| `LOCAL_FOLDERS`      | `[\"locales\"]` | Folders to search for translation files   |\n\nOptions can be passed directly during setup:\n\n```python\nbabel.setup(app, default_locale=\"fr\")\n```\n\nOr set via Muffin application config using the `BABEL_` prefix:\n\n```python\nBABEL_DEFAULT_LOCALE = \"fr\"\n```\n\n> Note: Muffin config keys are case-insensitive.\n\n---\n\n## Commands\n\nThe plugin adds commands to your Muffin app for message management.\n\n### Extract Messages\n\nExtract localizable strings from your app source:\n\n```bash\n$ muffin app_package babel_extract_messages [OPTIONS] appdir\n```\n\n### Compile Messages\n\nCompile `.po` files into `.mo` binaries:\n\n```bash\n$ muffin app_package babel_compile_messages [OPTIONS]\n```\n\n---\n\n## Export as CSV\n\nYou can also export your `.po` files to CSV:\n\n```bash\n$ muffin app_package babel_export_csv\n```\n\nThis helps with sending strings to translators or spreadsheets.\n\n---\n\n## Contributing\n\nDevelopment happens at: [https://github.com/klen/muffin-babel](https://github.com/klen/muffin-babel)\n\nFeel free to open issues or pull requests.\n\n---\n\n## Bug Tracker\n\nFound a bug? Have a suggestion? Report it here:\n\ud83d\udc49 [https://github.com/klen/muffin-babel/issues](https://github.com/klen/muffin-babel/issues)\n\n---\n\n## License\n\nLicensed under the [MIT license](http://opensource.org/licenses/MIT)\n\n---\n\n## Author\n\n**[klen](https://github.com/klen)** (Kirill Klenov)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Localization support for the Muffin Framework",
    "version": "1.6.0",
    "project_urls": {
        "Homepage": "https://github.com/klen/muffin-babel",
        "Repository": "https://github.com/klen/muffin-babel"
    },
    "split_keywords": [
        "localization",
        " internationalization",
        " muffin",
        " babel",
        " asyncio",
        " trio",
        " asgi"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "47461a7b482e29dab8d8d851a3ef5ff94a73659c5871d6e895b8fa20a46912ba",
                "md5": "97aa437b8f5397a7e8a240296766e309",
                "sha256": "7611301aea8033bf00b4a6a93ad899b39250192b8d191a94e9b566171d76f903"
            },
            "downloads": -1,
            "filename": "muffin_babel-1.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "97aa437b8f5397a7e8a240296766e309",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 5927,
            "upload_time": "2025-07-17T10:51:27",
            "upload_time_iso_8601": "2025-07-17T10:51:27.551417Z",
            "url": "https://files.pythonhosted.org/packages/47/46/1a7b482e29dab8d8d851a3ef5ff94a73659c5871d6e895b8fa20a46912ba/muffin_babel-1.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "df516b273b34e23959b491cc77adceb0aec06fe98651f68ee56408d2ffd92ebe",
                "md5": "1b008b9e8bc9aa8385a50ffdd6e19846",
                "sha256": "1e0d14038f2d80734b5c0ac42b9c53dd90eb59abf3decce7adfb90002371da01"
            },
            "downloads": -1,
            "filename": "muffin_babel-1.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1b008b9e8bc9aa8385a50ffdd6e19846",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 5766,
            "upload_time": "2025-07-17T10:51:28",
            "upload_time_iso_8601": "2025-07-17T10:51:28.830426Z",
            "url": "https://files.pythonhosted.org/packages/df/51/6b273b34e23959b491cc77adceb0aec06fe98651f68ee56408d2ffd92ebe/muffin_babel-1.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-17 10:51:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "klen",
    "github_project": "muffin-babel",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "muffin-babel"
}
        
Elapsed time: 0.86317s