473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.transla tion('i18n_test ', './translations',
languages=['fr'])
fr.install()

help = _("Help me!")

class Test_i18n(objec t):
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.hel p
'Aidez-moi!'

# all of the above are as expected; now for the first surprise
>>i18n_test.tes t.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 2017
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.transla tion('i18n_test ', './translations',
languages=['fr'])
fr.install()

help = _("Help me!")

class Test_i18n(objec t):
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.he lp

'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.hel p
"_" 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.transla tion('i18n_test ', './translations',
languages=['fr'])
_ = fr.gettext # untested
help = _("Help me!")

class Test_i18n(objec t):
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.he lp
'Aidez-moi!'

# all of the above are as expected; now for the first surprise
>>>i18n_test.te st.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.transla tion('i18n_test ', './translations',
languages=['fr'])

_ = fr.gettext # untested
help = _("Help me!")
class Test_i18n(objec t):
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.hel p
'Aidez-moi!'
# all of the above are as expected; now for the first surprise
>>i18n_test.tes t.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.transla tion('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.transla tion('i18n_test ', './translations',
languages=['fr'])

_ = fr.gettext # untested
help = _("Help me!")
class Test_i18n(objec t):
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.he lp
'Aidez-moi!'
# all of the above are as expected; now for the first surprise
>>>i18n_test.te st.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.transla tion('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.transla tion('i18n_test ', './translations',
languages=['fr'])
_ = fr.gettext # untested
help = _("Help me!")
class Test_i18n(objec t):
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.he lp
'Aidez-moi!'
# all of the above are as expected; now for the first surprise
>>>i18n_test.te st.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.transla tion('i18n_test ', './translations',
languages=['fr'])
_ = fr.gettext # untested
help = _("Help me!")
class Test_i18n(objec t):
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.he lp
'Aidez-moi!'
# all of the above are as expected; now for the first surprise
>>>i18n_test.te st.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.hel p
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
3622
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 0.14.1, will using PHP's gettext support along with the GNU gettext utilities work properly? For example, I've seen references to the translated...
6
3020
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 NOT want it too be replaced with two single quotes. Any clues? Joe
14
2604
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 gettext.install() to install the special _ function inside Python builtin. Other modules, taken from a general purpose collection of Python modules,...
4
1148
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
3252
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 QtGui, QtCore import sys
3
1343
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........ import java.applet.*; import java.awt.*; import java.sql.*; import java.awt.event.*; import sun.jdbc.odbc.*; public class Customerdetails...
4
2203
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 point to where I try to create a new instance of 'ConvertToOunces' inside the main method. Any help would be greatly appreciated. Here is my source...
7
2220
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 it to the JTabbedPane. I get a 'cannot find symbol' error, and it points to the pane.addTab method. The program works fine when I remove the nested...
10
4212
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 active again and displays the messages. Here is my server code: import java.io.*; import java.net.*; public class serverForm extends...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.