fastapi-i18n


Namefastapi-i18n JSON
Version 0.3.0 PyPI version JSON
download
home_pageNone
SummaryFastAPI Internationalization (i18n)
upload_time2025-10-27 13:07:38
maintainerNone
docs_urlNone
authorHeiGIT ohsome team
requires_python<3.14,>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastAPI Internationalization (i18n)

[![Build Status](https://jenkins.heigit.org/buildStatus/icon?job=fastapi-i18n/main)](https://jenkins.heigit.org/job/fastapi-i18n/job/main/)
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=fastapi-i18n&metric=alert_status)](https://sonarcloud.io/dashboard?id=fastapi-i18n)
[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-i18n)](https://pypi.org/project/fastapi-i18n/)
[![LICENSE](https://img.shields.io/github/license/GIScience/fastapi-i18n)](https://github.com/GIScience/fastapi-i18n/blob/main/COPYING)
[![status: active](https://github.com/GIScience/badges/raw/master/status/active.svg)](https://github.com/GIScience/badges#active)

This package is implemented as a [FastAPI dependency](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/?h=depende) which initializes translations using the [`gettext`](https://docs.python.org/3/library/gettext.html) module and makes them available throughout the request lifecycle using a [Conext Variable](https://docs.python.org/3/library/contextvars.html).

## Installation

```bash
uv add fastapi-i18n
```

## Prerequisites

A locale directory adhering to the GNU gettext message catalog API containing translated messages. See [chapter on Babel](#Babel) for more details.

## Configuration

```bash
export FASTAPI_I18N_LOCALE_DIR="paht/to/locale/dir"
export FASTAPI_I18N_LOCALE_DEFAULT="de"
```

## Usage

```python
from fastapi import FastAPI, Depends

from fastapi_i18n import i18n, _

app = FastAPI(dependencies=[Depends(i18n)])


@app.get("/")
def root():
    return _("Hello from fastapi-i18n!")
```

Set `Accept-Language` header for requests to get a translated version of the response.

For a complete example see [tests](https://github.com/GIScience/fastapi-i18n/blob/main/tests).

### Babel

Babel is not a dependency of FastAPI i18n, but it is useful for [working with GNU gettext message catalogs](https://babel.pocoo.org/en/latest/messages.html).

To add new locale and use babel to extract messages from Python files run:
```bash
echo "[python: **.py]" > babel.cfg

pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d locale -l de

# Now translate messages in locale/de/LC_MESSAGES/messages.po

# Then compile locale:
pybabel compile -d locale
```

To update existing locale run `update` instead of `init` run:
```bash
pybabel extract -F babel.cfg -o messages.pot .
pybabel update -i messages.pot -d locale
```


## Roadmap

- [x] Move to GitHub
- [ ] Support configuration via `pyproject.toml`
- [ ] Validate locale string
- [ ] Support setting locale using query parameter
- [ ] Support configuration of domain (currently defaults to "messages")

## Alternatives

- [FastAPI babel](https://github.com/Anbarryprojects/fastapi-babel)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-i18n",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.14,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "HeiGIT ohsome team",
    "author_email": "HeiGIT ohsome team <ohsome@heigit.org>",
    "download_url": "https://files.pythonhosted.org/packages/b6/7f/ff9aae088c08e50841e806b7ae371af8c309c127ff83ef3e05bc7bf7f4f7/fastapi_i18n-0.3.0.tar.gz",
    "platform": null,
    "description": "# FastAPI Internationalization (i18n)\n\n[![Build Status](https://jenkins.heigit.org/buildStatus/icon?job=fastapi-i18n/main)](https://jenkins.heigit.org/job/fastapi-i18n/job/main/)\n[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=fastapi-i18n&metric=alert_status)](https://sonarcloud.io/dashboard?id=fastapi-i18n)\n[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-i18n)](https://pypi.org/project/fastapi-i18n/)\n[![LICENSE](https://img.shields.io/github/license/GIScience/fastapi-i18n)](https://github.com/GIScience/fastapi-i18n/blob/main/COPYING)\n[![status: active](https://github.com/GIScience/badges/raw/master/status/active.svg)](https://github.com/GIScience/badges#active)\n\nThis package is implemented as a [FastAPI dependency](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/?h=depende) which initializes translations using the [`gettext`](https://docs.python.org/3/library/gettext.html) module and makes them available throughout the request lifecycle using a [Conext Variable](https://docs.python.org/3/library/contextvars.html).\n\n## Installation\n\n```bash\nuv add fastapi-i18n\n```\n\n## Prerequisites\n\nA locale directory adhering to the GNU gettext message catalog API containing translated messages. See [chapter on Babel](#Babel) for more details.\n\n## Configuration\n\n```bash\nexport FASTAPI_I18N_LOCALE_DIR=\"paht/to/locale/dir\"\nexport FASTAPI_I18N_LOCALE_DEFAULT=\"de\"\n```\n\n## Usage\n\n```python\nfrom fastapi import FastAPI, Depends\n\nfrom fastapi_i18n import i18n, _\n\napp = FastAPI(dependencies=[Depends(i18n)])\n\n\n@app.get(\"/\")\ndef root():\n    return _(\"Hello from fastapi-i18n!\")\n```\n\nSet `Accept-Language` header for requests to get a translated version of the response.\n\nFor a complete example see [tests](https://github.com/GIScience/fastapi-i18n/blob/main/tests).\n\n### Babel\n\nBabel is not a dependency of FastAPI i18n, but it is useful for [working with GNU gettext message catalogs](https://babel.pocoo.org/en/latest/messages.html).\n\nTo add new locale and use babel to extract messages from Python files run:\n```bash\necho \"[python: **.py]\" > babel.cfg\n\npybabel extract -F babel.cfg -o messages.pot .\npybabel init -i messages.pot -d locale -l de\n\n# Now translate messages in locale/de/LC_MESSAGES/messages.po\n\n# Then compile locale:\npybabel compile -d locale\n```\n\nTo update existing locale run `update` instead of `init` run:\n```bash\npybabel extract -F babel.cfg -o messages.pot .\npybabel update -i messages.pot -d locale\n```\n\n\n## Roadmap\n\n- [x] Move to GitHub\n- [ ] Support configuration via `pyproject.toml`\n- [ ] Validate locale string\n- [ ] Support setting locale using query parameter\n- [ ] Support configuration of domain (currently defaults to \"messages\")\n\n## Alternatives\n\n- [FastAPI babel](https://github.com/Anbarryprojects/fastapi-babel)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "FastAPI Internationalization (i18n)",
    "version": "0.3.0",
    "project_urls": {
        "Documentation": "https://github.com/GIScience/fastapi-i18n/blob/main/README.md",
        "Homepage": "https://ohsome.org/",
        "Issues": "https://github.com/GIScience/fastapi-i18n/issues",
        "Repository": "https://github.com/GIScience/fastapi-i18n"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1328dd49ef8f7f8c4300b90610cca5e7167d5e8c3c6461a6bf2ea9d825f8da80",
                "md5": "8785f5de9840b109c7237760dfecaac3",
                "sha256": "8357e01fe18f88de15ffb5cb8a8ef693030ee6b70458c13272094f1449283dcf"
            },
            "downloads": -1,
            "filename": "fastapi_i18n-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8785f5de9840b109c7237760dfecaac3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.14,>=3.10",
            "size": 18986,
            "upload_time": "2025-10-27T13:07:37",
            "upload_time_iso_8601": "2025-10-27T13:07:37.488370Z",
            "url": "https://files.pythonhosted.org/packages/13/28/dd49ef8f7f8c4300b90610cca5e7167d5e8c3c6461a6bf2ea9d825f8da80/fastapi_i18n-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b67fff9aae088c08e50841e806b7ae371af8c309c127ff83ef3e05bc7bf7f4f7",
                "md5": "2653c87f36c5ea1540fd9af255242853",
                "sha256": "ef11014893aa2d8a039268abbbe32220772537c01e46ef80be8b2dda55d5fe43"
            },
            "downloads": -1,
            "filename": "fastapi_i18n-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2653c87f36c5ea1540fd9af255242853",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.14,>=3.10",
            "size": 18605,
            "upload_time": "2025-10-27T13:07:38",
            "upload_time_iso_8601": "2025-10-27T13:07:38.677312Z",
            "url": "https://files.pythonhosted.org/packages/b6/7f/ff9aae088c08e50841e806b7ae371af8c309c127ff83ef3e05bc7bf7f4f7/fastapi_i18n-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-27 13:07:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "GIScience",
    "github_project": "fastapi-i18n",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "fastapi-i18n"
}
        
Elapsed time: 8.99127s