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?

5
  • a stab in the dark, maybe it's case sensitive? Commented Feb 26, 2011 at 14:10
  • capitalisation difference was a typo here not present in the actual program - I'll edit the question to fix that Commented Feb 26, 2011 at 14:12
  • 4
    Why are you using .install() (twice) and binding _? Commented Feb 26, 2011 at 14:19
  • ah that was it - binding _() was the problem. Remove that and it works. Commented Feb 26, 2011 at 14:23
  • Answer and close if it's resolved, guys... Commented Jul 1, 2011 at 5:57

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.