[PyQt] i18n
David Boddie
dboddie at trolltech.com
Wed Feb 11 18:21:04 GMT 2009
On Wednesday 11 February 2009, Giovanni Bajo wrote:
> On 2/11/2009 6:49 PM, David Boddie wrote:
> > On Wednesday 11 February 2009, Giovanni Bajo wrote:
> >> On 2/11/2009 6:24 PM, David Boddie wrote:
> >
> >>> To avoid problems, I have previously defined a helper method in
subclasses
> >>> where I thought it was necessary, like this:
> >>>
> >>> class ActionEditorWidget(QLabel):
> >>>
> >>> def tr(self, text):
> >>> return qApp.translate("ActionEditorWidget", text)
> >>>
> >> I don't see what this code is supposed to help.
> >>
> >> The problem at hand occurs when you use self.tr() in a class that you
> >> later inherit. I don't see how redefining tr() in the base class is
> >> going to help.
> >
> > Good point. Usually I don't use it at all, but I seem to remember that I
> > wanted to enforce the C++ behaviour referred to in the Reference Guide:
> >
> >
http://www.riverbankcomputing.com/static/Docs/PyQt4/pyqt4ref.html#differences-between-pyqt-and-qt
>
> I see, but the enforcement does not happen with the code you showed us.
Some modified code from the guide:
from PyQt4.QtCore import QObject
from PyQt4.QtGui import qApp
class A(QObject):
def tr(self, text):
print "A", text
return qApp.translate("A", text)
def hello(self):
return self.tr("hello")
class B(A):
pass
a = A()
a.hello()
b = B()
b.hello()
Running this yields:
A hello
A hello
The "A" context is being used, in contrast to the example in the guide to
which this comment is attached:
"In the above the message is translated by a.hello() using a context of A,
and by b.hello() using a context of B. In the equivalent C++ version the
context would be A in both cases."
Which is what I wanted.
> > If you want to use a different set of translations in the subclass, you'll
> > get the behaviour you want by just calling self.tr() without creating any
> > helper methods. You won't inherit any translations, though, or will you?
>
> They are not, and that's the whole point. The bug is that shown in the
> link above: the *derived* class won't display the translated strings
> because those strings are bound to the base classe context.
The derived class won't display the translated strings for the base class
in the original example (because the context is wrong), but it will for the
one with the helper method because that will invoke the translate() method
with the correct context.
Was it my use of the context the "ActionEditorWidget" context that you were
objecting to in the code that I posted? Were you suggesting that I should
have used "QLabel" instead? Are we arguing about completely different things?
David
More information about the PyQt
mailing list