When developing an application that supports multiple language localizations, I use a file in which I store all the string data of the program. I used to get by with simple -

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

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

Now exec somehow do not want to use. I have 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 current application localization):

 [LOCALE] language = english 

english.ini
(file with string data):

 [ENGLISH] program_title = Clean Master 

Now, let's say, if I need to change the language to Russian, I add the file russian.ini with the lines of Russian localization, and in the file locale.ini I set the value "language" to "russian".

It turns out the following:

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

 [LOCALE] language = russian 

english.ini (file with string data):

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

Maybe in Python there are other options for the above bike?

    1 answer 1

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

    The principle of operation is approximately as follows: all strings to be translated are wrapped in a call to the special function _() .

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

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

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

    A couple of googled manuals:

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

    • I saw these articles. It just seemed to me that gettext was an even more cycling bike than the one I use :) - Xyanight
    • one
      @Xyanight, well, this is a rather bearded bike, around which has already developed its own infrastructure. The presence of specials. editors and support by online translation services are already talking about something. Plus, he, for example, supports the possibility of specifying a different translation depending on the numeral (1 - "bicycle", 2 - "bicycle", 5 - "bicycle"), the ability to automatically merge an existing translation with an updated template, and other buns - insolor