Providing QGIS-Plugins in Multiple Languages
If you want to provide a QGIS-plugin in more than one language follow the steps below. The plugin will then display in the language of QGIS. Users can alter the language of QGIS (Version 3.40) in:
Settings -> Options -> General -> Override System Locale -> User interface translation
The following steps are necessary:
1. Mark text in UI and code as translatable
In Python code you mark a string by feeding it to the QGIS translator object:
my_string = QCoreApplication.translate(context: "context_of_this_string", sourceText: "the string to be translated")
For strings in the UI you need to open the according .ui file in QDesigner (or Designer, the app's name may differ), select the element to be translated, and make sure that "translatable" is check in the entry 'text' of the category QLabel, QAbstractButton, or wherever you find this entry (depends of the UI item type).
2. Create a folder "i18n"
If it doesn't exist already, create a folder "i18n" within your project folder.
3. There,Translate create.ui to .py files
If you have touched your .ui files then compile them into .py files with the command pyuic (pyuic5 or pyuic6).
4. Create a or update the .pro file listing all ui and code files that contain translatable text
Navigate into the i18n folder. There you need to create or – if it already exists – a .pro file. It is a simple text file listing all other files that contain translatable text. It also specifies the translation files with .ts ending. A .pro file may look like this:
FORMS = ui/my_dialog.ui \
ui/another_dialog.uiSOURCES = my_plugin.py \
my_module.py \
another_module.pyTRANSLATIONS = i18n/my_plugin_de.ts \
i18n/my_plugin_fr.ts \
i18n/my_plugin_it.ts \
i18n/my_plugin_en.ts
