problem mixing gettext and properties

Matimus mccredie at gmail.com
Tue Jun 26 20:49:59 CEST 2007


On Jun 26, 10:52 am, André <andre.robe... at gmail.com> wrote:
> I've encountered a problem using gettext with properties while using a
> Python interpreter.
>
> Here's a simple program that illustrate the problem.
> ==============
> # i18n_test.py: test of gettext & properties
>
> import gettext
>
> fr = gettext.translation('i18n_test', './translations',
> languages=['fr'])
> fr.install()
>
> help = _("Help me!")
>
> class Test_i18n(object):
>     def get(self):
>         __help = _("HELP!")
>         return __help
>     help_prop = property(get, None, None, 'help')
>
> test = Test_i18n()
>
> print help
> print test.help_prop
> #### end of file
>
> To run the above program, you need to have the strings translated and
> the proper ".po" and ".mo" files created.  (for those interested, I
> can send the whole lot in a zip file)
>
> If I run the program as is, the output is:
> Aidez-moi!
> AIDE!!!
>
> Ok, let's try with the Python interpreter:
>
> ActivePython 2.4.2 Build 248 (ActiveState Corp.) based on
> Python 2.4.2 (#67, Oct 30 2005, 16:11:18) [MSC v.1310 32 bit (Intel)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.>>> import i18n_test
>
> Aidez-moi!
> AIDE!!!
>
> #  No surprise there so far.
>
> >>> print i18n_test.help
> Aidez-moi!
> >>> print i18n_test.test.help_prop
> AIDE!!!
> >>> i18n_test.help
>
> 'Aidez-moi!'
>
> # all of the above are as expected; now for the first surprise
>
> >>> i18n_test.test.help_prop
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "i18n_test.py", line 12, in get
>     __help = _("HELP!")
> TypeError: 'str' object is not callable
>
> # and a second surprise where we try to repeat something that used to
> work
>
> >>> print i18n_test.test.help_prop
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "i18n_test.py", line 12, in get
>     __help = _("HELP!")
> TypeError: 'str' object is not callable
>
> #=============
>
> Dare I say: "Help!"   I really need to use the above at the
> interpreter prompt.
>
> André

In the interpreter "_" is used to store the return value of the last
statement executed. So when this line is run:

>>> i18n_test.help

"_" is set to  'Aidez-moi!'.

A possible fix, which I have not tested in your context, would be to
explicitly set _ to _ after your module is loaded.

So, try this:

>>> import i18n_test
>>> _ = i18n_test._
... The rest of your code here

I don't know for sure that this will work. From what I have tried
though, assigning to _ seems to disable that feature of the
interpreter.

Matt




More information about the Python-list mailing list