Methods for multilingual programs in Python?


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 –


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()"locale.ini")
language = config.get("LOCALE", "language")  # устанавливаем локаль
section = language.upper()"{}.ini".format(language))  # читаем файл строковых данных

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

(file of the current localization of the application):

language = english

(file with string data):

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):

language = russian

english.ini (file with string data):

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

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


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 or , 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 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