Adding Multilingual (Internationalization) Support to PyQt Apps
PyQt ships with everything you need to provide multiple UI languages.
All examples here use PyQt5.
Flag strings that need translation
Wrap every user-facing string with QObject.tr()
:
1 | class Mwindow(QtWidgets.QDialog): |
Because most widgets inherit from QObject
, you can call self.tr()
directly.
PyQt5 discourages tr()
in favor of translate()
:
1 | class A(QObject): |
If you rely on Qt Designer and all of your text lives in the .ui
file, life is even easier. Make sure the widgets keep the default “translatable” flag turned on in the text
property. When you run pyuic
, it wraps the strings with translate()
automatically.
Generate and edit the translation file
Create a translation file from the Python module that contains your strings:
pylupdate5 main_ui.py -ts zh_CN.ts
(PyQt4 uses pylupdate4
.)
Edit with Linguist
Open the generated file with linguist
, which comes with PyQt or pyqt5-tools. Translate each entry and choose “File → Release” to produce the .qm
file.
Edit manually
No Linguist? A .ts
file is just XML, so you can edit it by hand.
Before translation:
1 | <message encoding="UTF-8"> |
After translation:
1 | <message utf8="true"> |
Once you finish editing, convert the .ts
file into .qm
with lrelease
:
lrelease en.ts
lrelease
lives in the PyQt4 directory or ships with pyqt5-tools.
Load the .qm
and switch languages
Finally, load the compiled translation file.
Switch at startup
If you detect the system locale first, load the translator before showing the UI:
1 | if __name__ == '__main__': |
Call installTranslator
before show()
runs.
Switch on the fly
To change languages at runtime—say, when english_action
triggers—reload the translator and retranslate the UI:
1 | def to_english(self): |
retranslateUi
is generated by pyuic
and looks like this:
1 | def retranslateUi(self, MainUI): |
If you do not use Qt Designer, create a similar helper that updates every label manually.
References: