A package for easy creating and using translated messages withgettext in python projects
Project description
S-Translation
A package for easy creating and using translated messages with gettext in python projects
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
-
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
-
Load environment variables from
.env
file to use above environment variables:from dotenv import load_dotenv load_dotenv()
-
In your code add something with translation e.g.:
from s_translation import gettext as _ print(_("Hello, world"))
-
Create translations files
.po
:from s_translation import MessageCreator creator = MessageCreator() creator.create_message_files()
-
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
. -
Finally, now generate gettext message object files
mo
:from s_translation import MessageCreator creator = MessageCreator() creator.generate_message_objects()
Without .env file
-
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"))
-
Create translations files:
from s_translation import MessageCreator creator = MessageCreator(domain='myapp', languages=['en', 'pl'], locale_dir='locales') creator.create_message_files()
-
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
. -
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.
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
Hashes for s_translation-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f44fcfafea21fe2d9347414ac3c84f0a4fe9a3d5d4f2f61b2bce23beb37ddf11 |
|
MD5 | 7c6ed7384d40b6cc25952c4a161e9e0f |
|
BLAKE2b-256 | a890fc25c2059227d4f1072b185389a5b6226259cb3f174edf4ef806f9a6648e |