Python gettext - not translation

Example python program: [CGI script, so it needs to choose its own language, and not use what is installed for the host OS]

import gettext gettext.install('test', "./locale") _ = gettext.gettext t = gettext.translation('test', "./locale", languages=['fr']) t.install() print _("Hello world") 

./locale/fr/LC_messages/test.mo contains the translation (as a binary file generated when msgfmt was run in the .po file).

The program prints "Hello world" instead of the translated version. What could be the problem?

+4
source share
1 answer

Perhaps this answer is WAY too late, but I just found this, and I think it can help you.

 import gettext t = gettext.translation('test', "./locale", languages=['fr']) _ = t.gettext print _("Hello world") 

In my own program, I did it as follows:

 import gettext DIR = "lang" APP = "ToolName" gettext.textdomain(APP) gettext.bindtextdomain(APP, DIR) #gettext.bind_textdomain_codeset("default", 'UTF-8') # Not necessary locale.setlocale(locale.LC_ALL, "") LANG = "FR_fr" lang = gettext.translation(APP, DIR, languages=[LANG], fallback = True) _ = lang.gettext 

Note

My program has a lang directory. For each language, a directory is created in lang: * XX_xx * ( en_US ) Inside the en_US directory there is LC_MESSAGES , and inside there is TOOLNAME.mo

But this is my way of cross-language.

+4
source

Source: https://habr.com/ru/post/1341428/


All Articles