[PyKDE] Phil: Any chance of getting the TR (translate) method working?

Phil Thompson phil at river-bank.demon.co.uk
Fri Sep 7 13:39:05 BST 2001


Boudewijn Rempt wrote:
> 
> On Sun, 2 Sep 2001, Phil Thompson wrote:
> 
> >
> > The equivalent C++ program (attached) does exactly the same thing - so
> > (if there is a bug) I'm not convinced it is with PyQt.
> >
> 
> But what's the tr() function then supposed to do? I mean,
> it _should_ translate the text to the right language, and it
> doesn't, not even when used with forms generated with pyuic.

I've worked it out. tr() is generated by MOC. For a C++ implementation
of MyWidget, the generated code is...

QString MyWidget::tr(const char *s,const char *c)
{
	return qApp -> translate("MyWidget",s,c);
}

This gives us two problems - tr() is static, and the class name is
hard-coded. In fact I think the latter is a direct result of the former.
It tr() was not static then className() would be used instead of the
hardcoded string. Therefore what is being called is QMainWindow.tr()
(because QMainWindow is the "nearest" Qt class in MyWidget's superclass
list) which is passing the string "QMainWindow" to
QApplication.translate() instead of "MyWidget" which is what we want.

The reason my C++ version of the code failed was that I missed out the
Q_OBJECT macro which, by accident, created the same problem as we have
with the Python version.

With Qt2 I don't think there is a fix for this, only a couple of
workarounds...

1. Change the message file so that the scope of the translations is
always a Qt class, not one of your Python classes.

2. Implement tr() for each of your Python classes that you want to use
as a translation scope. In other words...

class MyWidget(QMainWindow):
	def tr(self,s,c):
		qApp.translate("MyWidget",s,c)

I don't think there is anything I can do in SIP to automate any of this
- but any ideas welcome.

With Qt3 the problems goes away because tr() is no longer static and
PyQt will be able to extract the class name from the self object and it
will work as you would expect.

Phil




More information about the PyQt mailing list