Skip to content

The qt translate function

Anderas2 edited this page Apr 6, 2019 · 3 revisions

If you set up a dialog or a main window inside pyqt5designer.exe and translate it to py code by using pyuic5.exe, then all the texts you put there are automatically in an extra function that loads something called "translate". I was curious about how to use it, so I searched a little bit in my book and the net.

among other things, I found this: https://wiki.qt.io/How_to_create_a_multi_language_application

so, for python it is not "lupdate" but pylupdate5.exe. It takes your ui file and makes an xml language file with ts extension from it. It needs a config file with .pro extension. It's a text file that contains nearly nothing, except this:

FORMS += monstereditor.ui
SOURCES += monster_editor_wrapper.py
TRANSLATIONS += monstereditor_fr.ts
TRANSLATIONS += monstereditor_de.ts

So, filenames of the ui and the self written code, plus the language files you want to generate for it.

Starting linguist.exe, one can translate all the texts;

then lrelease.exe transforms it into a ".qm" file.

that one is understood by pyqt5 and loaded automatically with those lines:

locale = QtCore.QLocale.system().name()
qtTranslator = QTranslator()
if qtTranslator.load("monstereditor_"+locale):
    app.installTranslator(qtTranslator)

These lines come directly after

app = QApplication(sys.argv)

Works like a charm! I love it. :-) The workflow sounds strange, but it really works and is easy. So I don't care that the workflow is a bit strange.

Clone this wiki locally