Skip to content

Translatable-Enums is an i18n python library which uses built-in Enums as a convenient way to store translation keys.

License

Notifications You must be signed in to change notification settings

CrazyProger1/Translatable-Enums

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Translatable-Enums

Lib logo

GitHub GitHub release (latest by date) PyPI - Downloads Coverage Code Style

Translatable-Enums is an i18n tool which uses built-in Enums as a convenient way to store translation keys.

Key-Features

  • No dependencies except the Python's standard library. Based on built-in enums & gettext
  • Powerful utility for extracting translation-keys
  • Easy-to-Use

Installation

You can use PIP:

pip install translatable-enums

Or Poetry:

poetry add translatable-enums

Getting-Started

from i18n import (
    TranslatableEnum,
    set_domain,
    set_language,
    language
)


class Messages(TranslatableEnum):
    HELLO = 'Hello,'
    WORLD = 'World!'


set_domain('app', './resources/languages')
set_language('en_US')

print(Messages.HELLO, Messages.WORLD)  # Hello, World!

set_language('uk_UA')

print(Messages.HELLO, Messages.WORLD)  # Привіт, Світ!

set_language('fr_FR')

print(Messages.HELLO, Messages.WORLD)  # Bonjour le monde!

print(Messages.HELLO.language('uk'), Messages.WORLD.language('en'))  # Привіт, World!

with language('uk_UA'):
    print(Messages.HELLO, Messages.WORLD)  # Привіт, Світ!

print(Messages.HELLO, Messages.WORLD)  # Bonjour le monde!

Extraction-Tools

To extract the translation-keys from application:

python -m i18n main.py application.pot

You will obtain a .pot file like this:

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: \n"
"MIME-Version: \n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

# Messages.WORLD
msgid "World!"
msgstr ""

# Messages.HELLO
msgid "Hello,"
msgstr ""

Key-Format

python -m i18n main.py application.pot --key-format "{enum}.{name}.{value}"

Default: "{value}"

  • enum - enum name
  • name - attribute name
  • value - attribute value

Examples:

{enum}.{name}.{value}

# Messages.WORLD
msgid "Messages.WORLD.World!"
msgstr ""

{enum}.{name}

# Messages.WORLD
msgid "Messages.WORLD"
msgstr ""

{value}

# Messages.WORLD
msgid "World!"
msgstr ""

Examples

See /examples for more examples.

Status

0.0.6 - RELEASED

Licence

Translatable-Enums is released under the MIT License. See the bundled LICENSE file for details.