This paper describes how to build a multi-lingual application using Qt framework. Qt provides a linguist tool chain for building and supporting applications for various regions of the world. The tool chain includes tools starting from Qt Creator, lupdate, lrelease. This article provides a detailed treatment on how to use these tools to build a multi-lingual dialog box.
Qt provides QTranslator for developers to switch between language choices. QTranslator takes .qm files as input. Developers need to provide .qm files for each language that they plan to support.
The following diagram details the steps for developers to generate .qm files. The .qm files could be packaged with the application as external or internal resources. The QTranslate load function loads a specific language resource for the application.
Let’s try to make the following user interface multi-lingual. We need to translate the following strings:
- Choose Language
- Qt Creator installation
- Qt Linguist installation
- User should have basic understanding of multilingual applications.
- Create a widget project with name translationExample and save to disk.
Step 1- Add TRANLATIONS to application .pro file:
TRANSLATIONS += english.ts \
Step 2- Use lupdate command
Run lupdate tool on your project translationExample.pro.
Once this command is executed successfully all three .ts files (english, hindi, french) mentioned in the project file will be created on disk. Below is the sample output:
Step 3 – QT Linguist:
Open Qt linguist tool and choose the .ts file(s) from your disk, which you would like to translate. In the below image each entry is a string. The columns the three languages we choose to translate.
Above image shows the untranslated strings after opening the .ts files in the qt linguist tool.
Step 4 – Start Translating strings:
Once each string is translated to the corresponding language the corresponding language column needs to be checked as shown below.
Step 5 – Once all the translations are complete, choose the Release All menu item under file menu. Release All command will create the .qm files as shown below.
Step 6 – Coding
- Add the Qtranslator header.
- Create an object for the Qtranslator class.
- To enable the translations, all the strings in the application, need to be inside tr() function or QcoreApplication::translate().
- Use the load function of the qtranslator class to load the language file.
- Appropriate fonts have to be set to the application for the proper display. Using QFont.
- Use the installTranslator() of the QCoreApplication class to install the translator.
Sample Source code:
Please download the source from the below link:
Further reading recommendations:
Along with QTranslator, Qt provides QLocale which can be used to handle currencies, date and time representations.
For any technical support or queries feel free to write to us at firstname.lastname@example.org