Methods for multilingual programs in Python?

Question:

When developing an application with support for several language localizations, I use a file in which I store all the string data of the program. I used to get by with a simple one –

exec(open("файл/языковой/локолизации").read())

thus initializing all variables with string values ​​in the main application code.

Now I don’t want to use exec. I adapted the ConfigParser module for this purpose. Now it looks like this:

from ConfigParser import ConfigParser


config = ConfigParser()
config.read("locale.ini")
language = config.get("LOCALE", "language")  # устанавливаем локаль
section = language.upper()
config.read("{}.ini".format(language))  # читаем файл строковых данных

# Таким образом я получаю строковые значения для приложеничя.
program_title = config.get(section, "program_title")

locale.ini
(file of the current localization of the application):

[LOCALE]
language = english

english.ini
(file with string data):

[ENGLISH]
program_title = Clean Master

Now, for example, if I need to change the language to Russian, I add the russian.ini file with the Russian localization strings, and in the locale.ini file I set the "language" value to "russian".

It turns out the following:

locale.ini (file of current application localization):

[LOCALE]
language = russian

english.ini (file with string data):

[RUSSIAN]
program_title = Мастер Очистки

Maybe there are other variants of the above bike in Python?

Answer:

In general, there is a fairly standard option – to use the gettext library, for which there are bindings, including for Python (moreover, in the standard library , it is possible that this is a Python implementation of gettext ).

The principle of operation is something like this: all strings to be translated are wrapped in a call to the special function _() .

Next, the program is processed by the pygettext utility, which pulls out all translatable strings, resulting in a .pot file (translation template), into which you can add the translation later. This is a text file with a relatively simple internal structure, it can be edited with something like a notepad, but there are also specialized utilities, for example, poedit , it can also be uploaded to any of the online translation services like crowdin.com or transifex.com , and edit there together with other translators.

From the .pot file, a .po file is obtained (essentially the same .pot , but with the completed translation of the lines into the desired language), which is then compiled into a .mo file (a binary file, although also with a rather simple internal structure) using the msgfmt utility from the gettext package or the msgfmt.py utility from the Python package.

Further, the translation is imported into the program to be translated using the command gettext.install('mary', './locale', unicode=True) (example from here , real parameters may be different).

A couple of google tutorials:

If you need to edit the translation online together with other translators, I can say that the above crowdin and transifex are free for Opensource products, but crowdin is quite strict with this, while in transifex it is enough for the translation to be available publicly, and in the settings the project had a link to the source code. At the same time, subjectively, crowdin has a more user-friendly interface.

Scroll to Top