Простой и быстрый i18n для FastAPI без внешних зависимостей
Project description
FastAPI Easy i18n
A simple, fast, and fully independent internationalization (i18n) library for FastAPI.
Add multilingual support to your project in just a few minutes. No heavy libraries — only pure Python and an intuitive API.
💡 Features
- 🔧 Minimal setup
- ⚡ Fast access to translations
- 🌍 Global and local locale setting
- 📝 String parameter support
- 📦 No external dependencies
📥 Installation
pip install fastapi-easy-i18n
🚀 Usage
Basic example
from fastapi_easy_i18n import t, set_locale
set_locale('en')
print(t('common.hello')) # → Hello!
🌐 Locale Management
Sett a global locale
The default use locale 'ru'.
set_locale('it')
print(get_locale()) # → 'it'
set_locale('fr')
print(get_locale()) # → 'fr'
Arguments:
locale: locale code ('ru','en','it','de','fr', ...)
Get the current locale
print(get_locale()) # → 'ru'
set_locale('en')
print(get_locale()) # → 'en'
Temp locale (context manager)
Used for temporarily changing the locale within a block:
with locale_context('en'):
print(t('common.hello')) # → Hello!
# after exiting, the locale is restored automatically
✨ Translation Functions
Translates a message for the selected locale.
t(key, params=None, locale=None)
# alias
_(key, params=None, locale=None)
Parameters:
- key — key in the format
"file.key"For example:"pagination.next","errors.not_found" - params — dictionary of parameters for string formatting
For example:
{'name': 'Alice'} - locale — locale for this specific call (optional)
Examples:
t('pagination.next')
# → 'вперёд'
t('common.greeting', {'name': 'Alice'})
# → 'Привет, Alice!'
_('pagination.next', locale='en')
# → 'next'
_('unknown.key')
# → 'unknown.key'
📁 Translation File Structure
Create the following directory structure in your project to store translation files:
your_fastapi_project/
└── app/
└── core/
└── i18n/
├── en/
│ ├── common.json
│ ├── errors.json
│ └── pagination.json
└── ru/
├── common.json
├── errors.json
└── pagination.json
If your application architecture is different, you can choose any directory to store translation files.
Simply inform the library about it by calling the set_patch('patch') method, for example:
set_path('backend/app/core') # add to search paths
set_path('backend/app/core', replace=True) # replace all paths
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 fastapi_easy_i18n-1.0.3.tar.gz.
File metadata
- Download URL: fastapi_easy_i18n-1.0.3.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0af9533e5cdbc17f8075d04e5bd5786d768bd540076c0cf2d42a2d187bda6b9a
|
|
| MD5 |
6d330395bf38f006c89e58a2eefbc7bb
|
|
| BLAKE2b-256 |
0e2f1e1a51c0a81fbd33480f0c576be415fc3cd790a45c79f456e32f24b94b1b
|
File details
Details for the file fastapi_easy_i18n-1.0.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_easy_i18n-1.0.3-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ccd39284220afea60aa998a195382ac48e0b0c56b2c06d41a85cdae088cd396
|
|
| MD5 |
5262ad20f6534feea46c2723c7f38122
|
|
| BLAKE2b-256 |
e0d71aaf89ff3b07d71abb8655f0f55130e68d87b08c3123af7b13f4bb0a5641
|