Text internationalization with async context magic
Project description
Magic-i18n
Internationalization with special contextvars magic.
Key features:
- Relies on a mechanism of implicit context passing for asynchronous functions.
- Supports template formatting via %.
- Texts are defined separately from their usage location and passed as variables.
- Supports temporary (local) language overrides.
- Can be used both standalone and within ASGI applications.
Provides:
- container for text/template variations,
- language context manager,
- middleware for ASGI-compatible frameworks.
Install
pip install magic-i18n
Declaration and basic usage
from magic_i18n import Text, set_default_language, language
# setup global default language, must be called before running application
set_default_language('ru')
# Basic init without translations, fallback only
message = Text('text')
print(message) # print `text`
# Text in different languages, with default language for non-defined languages
message = Text(en='text', ru='текст')
print(message) # print `текст` (ru - default language)
# Text in different languages, with fallback for non-defined languages
message = Text('fail', en='text', ru='текст')
print(message) # print `текст` (ru - default language)
Template in different languages
message = Text(en='hello ${name}', ru='привет ${name}')
print(message % 'Alex')
print(message % ('Alex',))
print(message % {'name': 'Alex'})
# all prints `привет Alex` (ru - default language)
Context manager for temporarily changing the current language.
with language('ru'):
print(message % name)
with language(language) as lang:
log.debug('Send with language %s', lang)
print(message % name)
LazyTemplate
LazyTemplate enables partial formatting and deferred evaluation.
Separate declaration
from magic_i18n import LazyTemplate
lazy_template = LazyTemplate('привет ${name}, открой ${target}')
Get LazyTemplate from Text container
message = Text(en='hello ${name}, open ${target}', ru='привет ${name}, открой ${target}')
lazy_template = message | 'en'
Usage
print(lazy_template)
# print `привет ${name}, открой ${target}`
lazy_template % 'Alex'
print(lazy_template)
# print `привет Alex, открой ${target}`
lazy_template % 'Telegram'
print(lazy_template)
# print `привет Alex, открой Telegram`
lazy_template % {'target': 'Site'} # set or replace
print(lazy_template)
# print `привет Alex, открой Site`
lazy_template(target='Calc') # set or replace
print(lazy_template)
# print `привет Alex, открой Calc`
ASGI middleware
The ASGI middleware retrieves the language from the Accept-Language header
and sets it as the current language if it's present in the accept_languages option.
Options:
application- wrapped ASGI application.default_language- (default: en) Used when the user's language is unknown or unavailable. This is the default only for ASGI and does not callset_default_language.accept_languages- list of available languages. Thedefault_languagemust be included in this list.
application = I18nMiddleware(
application,
default_language='en',
accept_languages=['en', 'ru'],
)
The header parser pattern r'([a-zA-Z]{2}[-a-zA-Z0-9]*)' can be modified in
header_parser class attribute.
I18nMiddleware.header_parser = re.compile(...)
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 magic_i18n-0.1.tar.gz.
File metadata
- Download URL: magic_i18n-0.1.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
518e023639e0efedb0629c9f1e58793a8613f8fd18b5097300b242def0025dcc
|
|
| MD5 |
8d1d053aa1748fde288e8648be2058cf
|
|
| BLAKE2b-256 |
c6ea86da0a37b3134be92a12ce87c19aa80fb5f07ce08acfbe5348d22b039b57
|
File details
Details for the file magic_i18n-0.1-py3-none-any.whl.
File metadata
- Download URL: magic_i18n-0.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd29a209ccb61c8ceafa0338807f9d028252a9e75a1ca084a9ef88417f891816
|
|
| MD5 |
4d62e26c1ad2962f9caea34772af87cc
|
|
| BLAKE2b-256 |
0a0c116936c7ded343c48849e2bfa76037c47cd5850c625a115d490ee6f3334c
|