Reader small image

You're reading from  Application Development with Qt Creator - Third Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
Publisher
ISBN-139781789951752
Edition3rd Edition
Languages
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Localizing Your Application with Qt Linguist

Localization is an important yet commonly neglected part of software development today. Most authors of applications, irrespective of whether those applications are commercial or open source, hope to capture a large number of users for their applications. Increasingly, this means supporting multiple languages in multiple locales, and often needing support for multiple languages in one locale (think of it as French and English coexisting in Canada).

For a long time, Qt has had a framework for making applications easy to localize with tools that help you to avoid hardcoding strings in your application and a GUI named Qt Linguist to help manage translation. In this chapter, we will take a look at Qt's strategy for localization, discussing the three tools (lupdate, lrelease, and Qt Linguist) that Qt provides and how to use them, along...

Technical requirements

The technical requirements for this chapter include Qt 5.12.3, MinGW 64-bit, Qt Creator 4.9.0, and Windows 10.

Understanding the task of localization

Localization is an important feature for your product if you want to sell it worldwide. When a user sees that their native language is being displayed and used in a piece of software, they will more likely stay attached to it and become one of your loyal customers. However, localization wasn't always an easy feature to implement, at least not until Qt introduced an easier way to do it.

Localizing your application has several phases that typically overlap throughout a project's life cycle.

The following diagram shows how these phases interact:

These phases are as follows:

  1. As you write your application, you place strings that need to be localized into a specific function (see step 5) so that Qt can identify the strings as needing localization.
  2. Periodically, you extract all the strings in your application and give them to translators...

Marking strings for localization

All the way back in Chapter 1, Getting Started with Qt Creator, I told you to always mark your strings for localization using the tr and qsTr functions: tr for C++ and qsTr for QML strings. Doing so has two key advantages:

  • It enables Qt to find every string that needs localization.
  • If you install a Qt translator object in your application and provide a translation file, the strings you wrap with these functions are automatically replaced by their localized equivalent.

Let's examine the use of tr in more detail. All Qt objects that include the Q_OBJECT macro in their declaration include the tr function. You've seen it with one argument, as follows:

button = new QPushButton(tr("&Quit"), this); 

The leading & in the string isn't for the tr function, but it is for the keyboard accelerators; you can prefix a letter...

Localizing your application with QLinguist

Once you've marked your strings using tr or qsTr, you need to generate a table of those strings for Qt Linguist to localize. You can do this using the lupdate command, which takes your .pro file and walks your sources to look for strings to localize and creates an XML file of the strings you need to translate for Qt Linguist. You need to do this once for each language you want to support. When doing this, it's best to name the resulting files systematically; one way to do this is to use the name of the project file, followed by a dash, followed by the ISO-639-2 language code for the language.

A concrete example is in order. This chapter makes use of QtLinguistExample; we can run lupdate using a command such as this to create a list of strings that we'll translate to Esperanto (ISO-639-2 language code, EPO):

% lupdate -pro...

Localizing special parameters – currencies and dates with QLocale

A common thing you might need to do is localize currencies and dates. Qt makes this easy, although the solution isn't obvious until you've thought about it a bit. Let's try it out.

  1. You need to know about the QString arg method. This replaces an escaped number with the formatted version of its argument. For example, if we write the following:
QString s = QString("%1 %2").arg("a").arg("b"); 

Then, s will contain the string "a b".

  1. You need to know about the toString method of QLocale, which formats its argument in a locale-specific way. So, we could write the following:
QString currencyValue = QString("%1 %2") 
    .arg(tr("$")).arg(QLocale::toString(value, 'g', 2) 

This uses tr to localize the currency symbol and the QLocale...

Summary

Localizing applications with Qt is easy with Qt Linguist and the localization framework in Qt. To use the framework, though, you must mark your strings to localize with tr or qsTr in your source code wherever they appear. Once you do this, you can create a source file of strings to translate with QLinguist using Qt's lupdate command and then provide translations for each string. Once you've provided the translations, you compile them using lrelease and then include them in your application by installing a QTranslator object in your application's main function and by loading the translation table generated by lrelease.

In this chapter, we have learned how to mark translatable texts so that Qt knows which text needs to be localized. Next, we also learned how to export these texts using Qt Linguist into a list that can be easily edited by you and your translator...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Application Development with Qt Creator - Third Edition
Published in: Jan 2020Publisher: ISBN-13: 9781789951752
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 €14.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng