TL;DR¶
Install l10n:
python3 -m pip install 'l10n[cli]'
And there are all the changes you need to do in your code to support translations for a string:
from l10n import Locales
locales = Locales()
def say_hello(lang='en'):
loc = locales[lang]
msg = loc.get('Hello, world!')
print(msg)
Now, let’s translate it to Ukrainian:
Extract all strings from the code that need to be translated:
python3 -m l10n extract --lang uk
Translate all extracted strings using Google Translate:
python3 -m l10n translate
Compile all translations into the binary format:
python3 -m l10n compile
That’s all! Now, your code supports translations:
from example import say_hello
say_hello(lang='uk')
# Привіт Світ!
If you want to manually adjust the translation text, just edit the languages/en.po
file and run compile
again. You don’t even need to restart your app!
Read more in the documentation: l10n.orsinium.dev.