Python application localization

August 7, 2018

Lang: cs en de es

The result of the program implementation is not only the code and the visual part of the program, but also the textual content that needs to be localized. One Python application in particular required localization, so I was trying to figure out how to localize an application written in Python.

I also develop applications in Python and there is one application in particular that required localization into different languages. So I was looking into a standardized localization procedure, what the resources are in UNIX (primarily Linux) operating systems. All this for applications written in Python.

Default state

I wrote the application in Python and it was a complex application that processed data in the background in parallel, receiving data from different inputs. It had a graphical interface in the QT toolkit that interactively showed the current situation, and the user could also use it to modify settings.

Translate tools

There are several tools for implementing localization in a program. The basic and standard one is Gettext.

However, directly QT has a tool in it that I decided to use. In order to use these resources, we need to install qttools, which contains conversion tools and the linguist graphics program, which can be used to conveniently translate text.
In Mageia 6, these packages are: qt4-linguist, qttools5-dev-tool.

Program code

The text in the program code must be written in such a way that it is possible to subsequently use the means for convenient localization of the application:
self.gpsOff.setText(_translate("MainWindow", "NO SIGNAL", None))
self.camera1.setText(_translate("MainWindow", "Camera 1 no signal", None))
self.voipRX.setText(_translate("MainWindow", "voipRX", None))
Incorporate and use the localization in the program:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

if __name__ == "__main__":
    translator = QtCore.QTranslator()

Export and convert texts

The program pylupdate4 is used to export text strings from the program.
Using the pylupdate4 program:

pylupdate4 -ts translate/zh_TW.ts
Then you need to translate the individual text strings in Lingust.
The resulting file must then be converted into a binary form that can be read by our program. The lrelease program performs the conversion to the qm binary format for the QT toolkit:
lrelease translate/zh_TW.ts

Resources a translation of Qt

Articles on a similar topic

Python program to control Docker using the API
How to use MailCatcher to test emails
Python OpenAI API
Creating a WebSocket web application and setting up a proxy
Project management: agile software development
How to run old PHP applications
What a good programmer should know
Rust programming language
NodeJS: development, server configuration
Nette security bug CVE-2020-15227
REST API: platform API
Custom web and mail hosting with ISP Config software
Programming in SQL: PostgreSQL, MySQL/MariaDB
HTTPS: secure web
NoSQL database Mongo DB
Connecting to Microsoft SQL Server from Linux
What is the job description of a programmer
Which mail and web hosting to choose
Digispark - Program Atmel ATtiny microcontroller with Arduino IDE
Development for ARM processors with Arduino IDE
How to program the ESP8266 WiFi processor
Open smartphone with Linux - Openmoko Neo FreeRunner


If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.