[PyQt] installTranslator does not work in help function
Phil Thompson
phil at riverbankcomputing.com
Sun Jun 5 22:26:07 BST 2011
On Sun, 5 Jun 2011 22:36:19 +0200, Martin Altmayer
<martin.altmayer at web.de>
wrote:
> Hello,
>
> it seems that whether the translation works depends on the context where
> QApplication.installTranslator is called.
> The following code works as it should:
>
> from PyQt4 import QtCore, QtGui
>
> app = QtGui.QApplication([])
> translator = QtCore.QTranslator()
> translator.load("hello_de")
> app.installTranslator(translator)
>
> hello = QtGui.QPushButton(QtGui.QApplication.translate("Test","Hello
> World!"))
> hello.resize(100, 30)
> hello.show()
>
> app.exec_()
>
>
> In contrast, the next application does not display the translated text,
> although I simply moved the initialization into a function:
>
>
> from PyQt4 import QtCore, QtGui
>
> def init():
> app = QtGui.QApplication([])
> translator = QtCore.QTranslator()
> translator.load("hello_de")
> app.installTranslator(translator)
> return app
>
> app = init()
>
> hello = QtGui.QPushButton(QtGui.QApplication.translate("Test","Hello
> World!"))
> hello.resize(100, 30)
> hello.show()
>
> app.exec_()
>
>
> Did I make a mistake or is this a difference between PyQt and Qt? As far
as
> I understand, this is not covered by "Differences Between PyQt and Qt" (
> http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/i18n.html)
and
> seems not to be documented elsewhere.
>
> The appendix contains the ts-file that I used.
>
> Thanks,
> Martin Altmayer
You need to keep a reference to the translator to prevent it from being
garbage collected. The easiest way is to make app the parent...
translator = QtCore.QTranslator(app)
Phil
More information about the PyQt
mailing list