Reader small image

You're reading from  Cross-Platform Development with Qt 6 and Modern C++

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781800204584
Edition1st Edition
Tools
Right arrow
Author (1)
Nibedit Dey
Nibedit Dey
author image
Nibedit Dey

Nibedit Dey is a software engineer turned serial entrepreneur with over a decade of experience in building complex software-based products with amazing user interfaces. Before starting his entrepreneurial journey, he worked for Larsen and Toubro and Tektronix in different R&D roles. He holds a bachelor's degree in biomedical engineering and a master's degree in digital design and embedded systems. Specializing in Qt and embedded technologies, his current role involves end-to-end ownership of products right from architecture to delivery. Currently, he manages two technology-driven product startups named ibrum technologies and AIDIA Health. He is a tech-savvy developer who is passionate about embracing new technologies.
Read more about Nibedit Dey

Right arrow

Chapter 11: Internationalization

In earlier chapters, we learned how to create GUI applications with Qt Widgets or Qt Quick. To make our applications usable across the world, we need to add translations to the application.

The process of making your application translation-aware is known as internationalization. It makes it easy to localize content for viewers from different cultures, regions, or languages. Translating Qt Widgets and Qt Quick apps into local languages is very easy with Qt. These processes of adapting an application to different languages with the geographical and technical standards of a target market are known as internationalization.

You will learn how to make an application with multilingual support. Throughout the chapter, we will explore different tools and processes to make a translation-aware application. In this chapter, we will discuss the following:

  • Basics of internationalization
  • Writing source code for translation
  • Loading translation...

Technical requirements

The technical requirements for this chapter include minimum versions of Qt 6.0.0 and Qt Creator 4.14.0 installed on the latest desktop platform such as Windows 10, Ubuntu 20.04, or macOS 10.14.

All the code used in this chapter can be downloaded from the following GitHub link: https://github.com/PacktPublishing/Cross-Platform-Development-with-Qt-6-and-Modern-Cpp/tree/master/Chapter11.

Important note

The screenshots used in this chapter are taken on the Windows platform. You will see similar screens based on the underlying platform on your machine.

Understanding internationalization and Qt Linguist

The processes of adjusting an application to different languages, geographical variations, and technological specifications of a target market are known as internationalization and localization. Internationalization refers to the process of creating a software application that can be translated into a variety of languages and for different regions without requiring significant technical changes. Internationalization is often abbreviated to i18n, with 18 being the number of letters between the letters i and n in the English word. The ease with which a product can be localized is greatly influenced by its internationalization. Creating a linguistically and culturally focused application for a global market is a much more complex and time-consuming process. Hence, companies focus on creating i18n-aware applications for global markets from the beginning of product development.

For internationalization, you should design your application...

Writing source code for translation

In this section, we will discuss how to mark strings as translatable strings and how to use the tools provided by Qt. Wherever your application uses a quoted string that is visible to the user, make sure the QCoreApplication::translate() method processes it. To do this, simply use the tr() method to mark the strings as translatable that are meant for display purposes. This feature is used to show which text strings are translatable inside your C++ source files.

For example, if you want to use a QLabel to show text on a user interface, then embed the text inside the tr() method as follows:

QLabel *label = new QLabel(tr("Welcome"));

The class name is the translation context for the QObject and its derived classes. To override the context, QObject-derived classes must use the Q_OBJECT macro in their class definition. This macro sets the context for the derived classes.

Qt provides several convenience macros and methods for internationalization...

Loading translations in a Qt application

In the previous section, we created translation files and understood the uses of the tools. To look up translations in a TS file, QTranslator functions are used. The translator must be instantiated before the application's GUI objects.

Let's have a look at how to load these translation files using QTranslator in the following code snippet:

QTranslator translator;
if(translator.load(QLocale(),QLatin1String("MyApplication") 
            , QLatin1String("_"), QLatin1String(":/i18n"))) 
    {
         application.installTranslator(&translator);
    } 
    else 
    {
        qDebug() << "Failed to load. " 
         ...

Switching languages dynamically

So far, you have learned how to use the system language or a default language for your Qt application. In most applications, you can just detect the language in main() and load an appropriate .qm file. Sometimes, your application must be able to support changes to the user's language settings while still running. An application that is used by multiple people in shifts may need to switch languages without requiring a restart.

To achieve this in a Qt Widgets-based application, you can override QWidget::changeEvent(). Then, you have to check whether the event is of the QEvent::LanguageChange type. You can retranslate the user interface accordingly.

The following code snippet explains how to achieve dynamic translation in a Qt Widgets-based GUI:

void CustomWidget::changeEvent(QEvent *event)
{
    if (QEvent::LanguageChange == event->type()) 
    {
        ui...

Internationalization with Qt Widgets

In the previous sections, we discussed how to create translation files and how to use QTranslator to load a translation file. Let's create a simple example using Qt Widgets and implement our learning.

Follow the subsequent steps to create the sample application:

  1. Create a Qt Widgets-based application using Qt Creator's new project creation wizard and follow through the screens as discussed in earlier chapters.
  2. On the Translation File screen, choose German (Germany) as the language option, or any preferred language.
  3. Finish the project creation. You will see that Simplei18nDemo_de_DE.ts is created in your project structure.
  4. Next, you add a QLabel to the .ui file and add Welcome text.
  5. Next, run lupdate. You can run lupdate from the command line as well as from the Qt Creator interface, as shown in Figure 11.3:

    Figure 11.3 – Qt Linguist options in Qt Creator

  6. When you run lupdate, you will see the following...

Internationalization with Qt Quick

In the previous section, we discussed internationalization in Qt Widgets. In this section, we will discuss different aspects of internationalizing your Qt Quick application. The underlying localization scheme in Qt Quick applications is similar to Qt Widgets applications. The same set of tools described in the Qt Linguist Manual are also used in Qt Quick. You can translate an application that uses both C++ and QML.

In a Qt project file, the SOURCES variable is used for C++ source files. If you list QML or JavaScript files under this variable, the compiler will attempt to use the files considering them as C++ files. As a workaround, you can use a lupdate_only {...} conditional declaration to make the QML files visible to the lupdate tool but invisible to the C++ compiler.

Consider the following example. The application's .pro file snippet lists two QML files:

lupdate_only {
SOURCES = main.qml \
       ...

Deploying translations

In previous sections, we learned how to create translation-aware applications using both Qt Widgets and QML. You don't have to ship the .ts files with your application. To deploy translations, your release team must use the updated .qm files and ship them with the application package. The .qm files required for the application should be placed in a location where QTranslator can locate them. Typically, this is done by embedding qm files in a resource (.qrc) file or specifying a path that contain the .qm files relative to QCoreApplication::applicationDirPath(). The rcc tool is used to embed the translation files into a Qt application during the build process. It works by producing a corresponding C++ file containing specified data.

You can automate the generation of .qm files by adding a script to your .pro file. You do it by following these steps:

  1. To begin, use the language codes to declare the languages under the LANGUAGES variable in your Qt...

Summary

In this chapter, we took a look at the core concepts of internationalization and localization in Qt. We discussed different tools provided by Qt for internationalization. We learned how to use Qt Linguist. We also looked at how to translate a Qt Widgets application into a different language. Then, we learned how to translate dynamically.

In the latter part of the chapter, we discussed translating a Qt Quick application. Afterward, we learned how to switch languages dynamically in a Qt Quick application. Now you can create an application with multiple languages and share it with your clients or friends in a different geographical region.

In Chapter 12, Performance Considerations, we will learn about tools and tricks to optimize performance in a Qt application.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Cross-Platform Development with Qt 6 and Modern C++
Published in: Jun 2021Publisher: PacktISBN-13: 9781800204584
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Nibedit Dey

Nibedit Dey is a software engineer turned serial entrepreneur with over a decade of experience in building complex software-based products with amazing user interfaces. Before starting his entrepreneurial journey, he worked for Larsen and Toubro and Tektronix in different R&D roles. He holds a bachelor's degree in biomedical engineering and a master's degree in digital design and embedded systems. Specializing in Qt and embedded technologies, his current role involves end-to-end ownership of products right from architecture to delivery. Currently, he manages two technology-driven product startups named ibrum technologies and AIDIA Health. He is a tech-savvy developer who is passionate about embracing new technologies.
Read more about Nibedit Dey