473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem mixing gettext and properties

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é

Jun 26 '07 #1
6 2001
On Jun 26, 10:52 am, André <andre.robe...@gmail.comwrote:
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

Jun 26 '07 #2
André 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.gettext # untested
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é
The _ builtin is set to the result of the last expression evaluated by the
interpreter:
>>for i in range(3):
.... i
....
0
1
2
>>_
2
>>import __builtin__
__builtin__._
2

Therefore you get a name clash with _() as an alias for gettext(). Use
module-global aliases instead, e. g.

_ = fr.gettext

in the above code.

Peter

Jun 26 '07 #3
On Jun 26, 3:56 pm, Peter Otten <__pete...@web.dewrote:
André 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.gettext # untested
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é

The _ builtin is set to the result of the last expression evaluated by the
interpreter:
>for i in range(3):

... i
...
0
1
2>>_
2
>import __builtin__
__builtin__._

2

Therefore you get a name clash with _() as an alias for gettext(). Use
module-global aliases instead, e. g.

_ = fr.gettext

in the above code.

Peter
Thanks, that works ... but, it brings many other "complications". I
have multiple modules, and I want to be able to switch languages
easily. Unless I am mistaken, if I do it with module-global aliases
instead, I will need to have something like

lang = {}
for code in ['en', 'fr', ...]:
lang[code] = gettext.translation('i18n_test', './translations',
languages=[code])

def switch_language(code):
...
import module1
import module2
...
module1._ = lang[code].gettext
module2._ = lang[code].gettext
...

And I will need to make sure to keep track of all the modules that
require translation... Is there an easier, less tedious way to do
this?

Am I missing something?

André

Jun 26 '07 #4
André wrote:
On Jun 26, 3:56 pm, Peter Otten <__pete...@web.dewrote:
>André 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.gettext # untested
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é

The _ builtin is set to the result of the last expression evaluated by
the interpreter:
>>for i in range(3):

... i
...
0
1
2>>_
2
>>import __builtin__
__builtin__._

2

Therefore you get a name clash with _() as an alias for gettext(). Use
module-global aliases instead, e. g.

_ = fr.gettext

in the above code.

Peter

Thanks, that works ... but, it brings many other "complications". I
have multiple modules, and I want to be able to switch languages
easily. Unless I am mistaken, if I do it with module-global aliases
instead, I will need to have something like

lang = {}
for code in ['en', 'fr', ...]:
lang[code] = gettext.translation('i18n_test', './translations',
languages=[code])

def switch_language(code):
...
import module1
import module2
...
module1._ = lang[code].gettext
module2._ = lang[code].gettext
...
If you need to change the language while the program is running you can put

def _(s):
return gettext(s)

into one "master" module and have the other modules import that:

from master import _

This comes at the cost of one extra indirection.
And I will need to make sure to keep track of all the modules that
require translation... Is there an easier, less tedious way to do
this?
If your users don't rely on _ in the interpreter, you can write a custom
sys.displayhook that doesn't set __builtin__._

Peter
Jun 26 '07 #5
On Jun 26, 4:17 pm, André <andre.robe...@gmail.comwrote:
On Jun 26, 3:56 pm, Peter Otten <__pete...@web.dewrote:
André 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.gettext # untested
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é
The _ builtin is set to the result of the last expression evaluated by the
interpreter:
>>for i in range(3):
... i
...
0
1
2>>_
2
>>import __builtin__
>>__builtin__._
2
Therefore you get a name clash with _() as an alias for gettext(). Use
module-global aliases instead, e. g.
_ = fr.gettext
in the above code.
Peter

Thanks, that works ... but,
[snip...]
One more question (back to the original) post.
Why does
i18n_test.help
does the right thing
but
i18n_test.test.help_prop
does not? Both use _() ...

Is it because help = _(...) is defined at the module level but
test.help_prop is defined locally, and that the lookup for _() is done
first locally (finds nothing) and then globally (outside the module
scope) ?

André

Jun 26 '07 #6
André wrote:
On Jun 26, 4:17 pm, André <andre.robe...@gmail.comwrote:
>On Jun 26, 3:56 pm, Peter Otten <__pete...@web.dewrote:
André 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.gettext # untested
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!!!_("HELP!")
# 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é
The _ builtin is set to the result of the last expression evaluated by
the interpreter:
>>for i in range(3):
... i
...
0
1
2>>_
2
import __builtin__
__builtin__._
2
Therefore you get a name clash with _() as an alias for gettext(). Use
module-global aliases instead, e. g.
_ = fr.gettext
in the above code.
Peter

Thanks, that works ... but,
[snip...]
One more question (back to the original) post.
Why does
i18n_test.help
does the right thing
but
i18n_test.test.help_prop
does not? Both use _() ...

Is it because help = _(...) is defined at the module level but
test.help_prop is defined locally, and that the lookup for _() is done
first locally (finds nothing) and then globally (outside the module
scope) ?
No, it has nothing to do with the scope. It is just that (assuming a
single-threaded environment)

help = _("Help me!")

is evaluated once immediately after the install() call which sets
__builtin__._ to gettext().

i18n_test.test.help_prop on the other hand invokes __builtin__._() every
time, and because you implicitly set it to a string with
>>i18n_test.help
you saw the exception.

Peter

Jun 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Patton | last post by:
Can someone point me to a definitive source for the answers to the following two questions: 1) In an environment with Redhat Linux 2.4.21-27.0.1, Apache 1.3.31, PHP 4.3.10, and GNU gettext...
6
by: Joe Ray | last post by:
I am trying to come up with a regular expression that searches and replaces all occurences of the string "'" and replaces it with "''" . However if a backslash comes before the single quote I do...
14
by: Pierre Rouleau | last post by:
I have a problem writing self-testable modules using doctest when these modules have internationalized strings using gettext _('...'). - The main module of an application (say app.py) calls...
4
by: André | last post by:
Hi, I wrote this module in file "module.vb" and put it in App_Code of my asp.net application. Imports Microsoft.VisualBasic Public Module Mymodule Sub Main() MyFunction() End Sub
2
by: jiang.haiyun | last post by:
Hi, I am having some serious problems with PyQT4, when i run pyqt script, I always get 'Segmentation fault'. the script is simple: ====================== %less qttest.py from PyQt4 import...
3
by: pradeep84 | last post by:
hi.. to all.. i executed the following program.. i entered the data in the output screen..but the data is not updated in the database.... Is there any problem with the program.. plz help me...........
4
by: HxRLxY | last post by:
I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error...
7
by: HxRLxY | last post by:
I posted a different question (Help with non-static/static problem) which was answered. I changed my inner class to a static nested class, but now I cannot create an object using that class and add...
10
by: Elaine121 | last post by:
Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.