[PyQt] Internationalization and PyQt

Thorsten Kampe thorsten at thorstenkampe.de
Sat Oct 4 19:31:10 BST 2008


Hi,

I've "upgraded" a PyQt demo application of mine so it includes not only 
a translation of the user interface in German but also in French.

My problem is that it simply works but I think it shouldn't (no don't 
laugh). This is the code in my demo application (called my_application) 
that does the internationalization:

# Internationalization
qtTranslator  = QtCore.QTranslator()
qtTranslator.load(':/qt.qm')
app.installTranslator(qtTranslator)

appTranslator = QtCore.QTranslator()
appTranslator.load(':/my_application.qm')
app.installTranslator(appTranslator)
#

This is the relevant snippet from my_application.qrc:

<qresource lang="de">
<file alias="my_application.qm">translation/my_application_de.qm</file>
<file alias="qt.qm">translation/qt_de.qm</file>
</qresource>
<qresource lang="fr">
<file alias="my_application.qm">translation/my_application_fr.qm</file>
<file alias="qt.qm">translation/qt_fr.qm</file>

Please note that I'm using the same alias ("my_application.qm" and 
"qt.qm") for the German /and/ English tranlation resources.

Mark Summerfield suggests in his excellent book about Rapid GUI 
programming to use this approach:

# Internationalization
locale = QLocale.system().name()
qtTranslator = QTranslator()
if qtTranslator.load("qt_" + locale, ":/"):
    app.installTranslator(qtTranslator)

appTranslator = QTranslator()
if appTranslator.load("imagechanger_" + locale, ":/"):
    app.installTranslator(appTranslator)
#

So why does my code (that does not query the locale and choose the right 
tranlation file according to the locale) work?


Thorsten



More information about the PyQt mailing list