Skip to main content

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)

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 call set_default_language.
  • accept_languages - list of available languages. The default_language must 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magic_i18n-0.1b0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

magic_i18n-0.1b0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file magic_i18n-0.1b0.tar.gz.

File metadata

  • Download URL: magic_i18n-0.1b0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for magic_i18n-0.1b0.tar.gz
Algorithm Hash digest
SHA256 97f3f61caf96889f97fc241b9895ade63cf914f08f8256a6605b3391e2a2d5e8
MD5 faa22db7be629fce6340bde177a45410
BLAKE2b-256 54afa7de5597288333f86941919f01da3cd5c551a138be5b3fbbbf05c06982bf

See more details on using hashes here.

File details

Details for the file magic_i18n-0.1b0-py3-none-any.whl.

File metadata

  • Download URL: magic_i18n-0.1b0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for magic_i18n-0.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 f242dd316f27175143d51ff70e504e92f715e134acbe70609bb33ad5fe8db564
MD5 515a7bd9431b02ade2811c9a08c65a01
BLAKE2b-256 f2f627b82191dc5953b1f2b64dc38a385e0662c3db76849f7f5c83366f9d503a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page