[PyQt] Translations failing when not using "self" for the instance
Hans-Peter Jansen
hpj at urpla.net
Fri Dec 3 13:51:17 GMT 2010
On Friday 03 December 2010, 12:36:44 Thorsten Kampe wrote:
> Hi,
>
> when using other names for the self idiom referring to the current
> instance (like "inst" for example - see [1]), the application is not
> correctly translated.
>
> If I open the generated ts file in Qt Linguist it shows two
> "contexts" ("MainWindow" and "inst") instead of just one
> ("MainWindow) when using "self".
>
> The translation for the text in the script ("inst.tr('Ready')" for
> example) shows still English - while the ones from the resource file
> work.
>
> Is that a problem with pyuic or with pylupdate4?
>
>
> Thorsten
>
> [1]
> from PyQt4 import QtGui, QtCore
> import resource.ui
>
> class MainWindow(QtGui.QMainWindow, resource.ui.Ui_MainWindow):
> def __init__(inst):
> QtGui.QMainWindow.__init__(inst)
> inst.setupUi(inst)
> inst.statusBar().showMessage(inst.tr('Ready'))
In short: don't.
This issue is most probably due to parsing deficits in pylupdate4 (which
is still C++ inherited from lupdate). Don't defy it too much..
Your best bet is to adhere to the pyuic4 standard (e.g. check
resource/ui.py). In order to reduce your typing needs, it's common to
add a tr method to your classes, that may look like this:
def tr(self, msg):
return QtGui.QApplication.translate("YourClass", msg, None,
QtGui.QApplication.UnicodeUTF8)
That way, you have full control, and you can be sure, that translations
work in almost all contexts, which might not always be the case
otherwise (e.g. to trust on QWidget's tr implementation (please correct
me if I'm wrong, Phil)). Again pyqt4ref.html has a paragraph about this
very topic.
Pete
More information about the PyQt
mailing list