Make-your-application-Multilingual

Abstract

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.

Introduction

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.

Make-your-application-multilingual-1

Implementation

Let’s try to make the following user interface multi-lingual. We need to translate the following strings:

  1. TranslationApp
  2. Choose Language
  3. English
  4. Hindi
  5. French
  6. Welcome

Make-your-application-multilingual7

Pre-Requirements:

  1. Qt Creator installation
  2. Qt Linguist installation
  3. User should have basic understanding of multilingual applications.
  4. Create a widget project with name translationExample and save to disk.

Step 1- Add TRANLATIONS to application .pro file:
TRANSLATIONS += english.ts \
hindi.ts \
french.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:

Command:

3
Output:

4

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.

Make-your-application-multilingual3

            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.
Make-your-application-multilingual4
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.

Make-your-application-multilingual5

Step 6 – Coding

    1. Add the Qtranslator header.
    2. Create an object for the Qtranslator class.
    3. To enable the translations, all the strings in the application, need to be inside tr() function or QcoreApplication::translate().
    4. Use the load function of the qtranslator class to load the language file.

translator->load(“/home/French.qm”);

    1. Appropriate fonts have to be set to the application for the proper display. Using QFont.
    2. Use the installTranslator() of the QCoreApplication class to install the translator.

qApp->installTranslator(translator);

Sample Source code:

Please download the source from the below link:
https://drive.google.com/file/d/0B4LkGhFjozX3bHM1eXV2SFVieHc/edit?usp=sharing

Output Screenshots:
Make-your-application-multilingual6
Make-your-application-multilingual7

Further reading recommendations:

Along with QTranslator, Qt provides QLocale which can be used to handle currencies, date and time representations.

References

1. http://doc.qt.io/qt-4.8/qtranslator.html
2. http://doc.qt.io/qt-4.8/linguist-manual.html

For any technical support or queries feel free to write to us at sales@e-consystems.com