Skip to main content

S-Translation

A package for easy creating and using translated messages with gettext in python projects

Pypi Codecov Coverage downloads

Requirements

  • gettext installed in your system (gettext)
  • python: tested for 3.10, 3.11
  • python package python-dotenv (pip install python-dotenv)

Getting started

With .env file

  1. First, create a new .env file in your project, eg:

    GETTEXT_DOMAIN=myapp
    LOCALE_DIR=locales  # => directory inside your project base dir to store the gettext translations
    GETTEXT_LANGUAGES='pl,en'
    GETTEXT_LANGUAGE=pl
    
  2. Load environment variables from .env file to use above environment variables:

    from dotenv import load_dotenv
    
    load_dotenv()
    
  3. In your code add something with translation e.g.:

    from s_translation import gettext as _
    
    print(_("Hello, world"))
    
  4. Create translations files .po:

    from s_translation import MessageCreator
    
    creator = MessageCreator()
    creator.create_message_files()
    
  5. Next, put your translations to the created files in the previous step ./locales/{language}/LC_MESSAGES/myapp.po e.g. ./locales/pl/LC_MESSAGES/myapp.po.

  6. Finally, now generate gettext message object files mo:

    from s_translation import MessageCreator
    
    creator = MessageCreator()
    creator.generate_message_objects()
    

Without .env file

  1. In your code add something with translation e.g.:

    from s_translation import Translation
    
    _ = Translation(domain='myapp', language_code='pl', locale_dir='locales').gettext
    
    print(_("Hello, world"))
    
  2. Create translations files:

    from s_translation import MessageCreator
    
    creator = MessageCreator(domain='myapp', languages=['en', 'pl'], locale_dir='locales')
    creator.create_message_files()
    
  3. Next, put your translations to the created files in the previous step ./locales/{language}/LC_MESSAGES/myapp.po e.g. ./locales/pl/LC_MESSAGES/myapp.po.

  4. So, now generate gettext message object files mo:

    from s_translation import MessageCreator
    
    creator = MessageCreator(domain='myapp', languages=['en', 'pl'], locale_dir='locales')
    creator.generate_message_objects()
    

Now, your gettext translation is ready to use.

Metadata

Release files for s-translation 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for s-translation 1.0.0
File Size Uploaded
s_translation-1.0.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for s-translation 1.0.0
File Interpreter ABI Platform
s_translation-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / s_translation-1.0.0.tar.gz

Download URL s_translation-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
89c2ebaffca507212ac8bab65096007e2c05f879553ea324f55dd321513e3cca
BLAKE2b-256 checksum
How to use checksums
b8f0317cf05ff2b0b6f478d2fa96dd8874c752cf765daa7ec1f10d67124b62d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / s_translation-1.0.0-py3-none-any.whl

Download URL s_translation-1.0.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f44fcfafea21fe2d9347414ac3c84f0a4fe9a3d5d4f2f61b2bce23beb37ddf11
BLAKE2b-256 checksum
How to use checksums
a890fc25c2059227d4f1072b185389a5b6226259cb3f174edf4ef806f9a6648e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page