[PyQt] i18n

Giovanni Bajo rasky at develer.com
Wed Feb 11 17:55:28 GMT 2009


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.

> 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.

In those cases (which, in my exprience, are not so common to call for 
the adoption of a different convention everywhere in the code), I simply 
change:

    def hello(self):
        return self.tr("Hello")

into this:

    def hello(self):
        return qApp.translate("A", "Hello")

Oh, yeah, throw in some 5-6 useless characters somewhere if you object 
to star imports :), but my point stands: I suggest using self.tr() as a 
default, and turn to something else only where required.
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


More information about the PyQt mailing list