6

Sample python program: [CGI script, so it needs to select its own language rather than using whatever the host OS is set to]

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 binary file, generated by running msgfmt on a .po file).

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

OJW
  • 4,514
  • 6
  • 40
  • 48

1 Answers1

6

Maybe 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 programm, I did it this way:

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 on it. For every language a directory is made in lang : *XX_xx* (en_US) Inside the directory en_US there is LC_MESSAGES, and inside there is TOOLNAME.mo

But that's my way for cross-language.

gpoo
  • 8,408
  • 3
  • 38
  • 53
mDroidd
  • 1,213
  • 4
  • 17
  • 25