Application Development With QT Creator: Second Edition
Application Development With QT Creator: Second Edition
Qt Creator
Second Edition
Ray Rischpater
Chapter No.10
"Localizing Your Application with Qt Linguist"
I'd like to thank Ritika Singh for shepherding this book through the
publishing process at Packt Publishing as well as for her suggestions
throughout the process. I'd also like to thank the technical reviewers who
provided their time to give suggestions and clarifications for each draft of the
book. Of course, in the end, I'm responsible for any errors that might remain!
Edit, compile, debug, and run C++ applications using Qt Creator, opening a path
to build state-of-the-art console and GUI applications with Qt and with the
Standard Template Library (STL)
Edit, compile, debug, and run Qt Quick applications using Qt Creator, giving you
access to one of the most advanced declarative GUI authoring environments
anywhere
Design GUI applications using Qt Designer to build either traditional widgetbased or Qt Quick applications
Analyze the memory and the runtime performance of your Qt applications, make
improvements, and fix defects
Provide localized versions of your application so that you can deploy it all over
the world in different languages
Use Qt Quick and Qt Widgets to write mobile applications for platforms such as
Google Android
Build multimedia- and sensor-aware applications with Qt's support for
multimedia and sensors
Chapter 12, Developing Mobile Applications with Qt Creator, gives you a glimpse of the
exciting arena of mobile software development and shows you how you can use what
you've learned in this book about Qt and Qt Creator to write applications for platforms
such as Google Android.
Chapter 13, Qt Tips and Tricks, is packed with tricks for using Qt and Qt Creator
that will help you use the Qt framework and the Qt Creator IDE efficiently.
Irelease
tr(hello world);
tr(this is a test);
qsTr(Heres a string in
QML);
Iupdate
Chapter 10
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 with & to assign a keyboard accelerator and it gets
the default system (a key combination with Alt for Windows, Command for Apple,
and Alt for Linux). The tr function uses the string you pass as the string in the user
interface if no translated version of the string appears in the application's current
translation table, or it uses the string in the current translation table if one exists.
The tr function can take a second argument, a disambiguation context that tr uses
for the same string that might require different translations:
tr("&Copy", "Menu");
Depending on the value of count and the locale, a different string is returned. So, a
native English translation could return "0 items replaced", "1 item replaced",
"2 items replaced", and so on, while a French translation could return "0 item
remplac", "1 item remplac", "2 items remplacs", and so on.
The qsTr function in QML works similarly but does not have the flexibility that the
tr method has for disambiguation or handling plurals.
[ 195 ]
Don't forget that the % character is the command prompt, which might differ from
system to system.
Here, the pro file indicates the .pro file that contains the list of sources to scan for
strings to translate, and the ts argument indicates the name of the translation file to
be written. You'll need lupdate in your path, of course. How you set your path will
depend on whether you're working on Windows, Mac OS X, or Linux, and where
you've installed Qt. Some installations of Qt might update your path automatically,
while others might not do so. On my Windows machine, for example, I can find
lupdate at C:\qt\5.1.0\msvc2012_64\bin\lupdate.exe.
The .ts file is an XML file with tags to indicate the strings to be translated, their
context in your application's source code, and so forth. Qt Linguist will save the
translations to its output file, which is named with a QM suffix as well, but don't
worry: lupdate is smart enough to not overwrite the existing translations if you run
it again after providing some translations.
Qt Linguist is a GUI application; on starting this application, you'll see a screen very
similar to the next screenshot:
[ 196 ]
Chapter 10
To begin, you need to open a .ts file you generated, by navigating to File | Open, and
choosing a translation file. You'll be prompted for the destination language, and then
you're given a list of the strings found. Youor your translatorsonly need to walk
through each string and enter the corresponding string in the translated language.
As you do so, you can see the context of the string in the source code in the right-most
pane; the line of the source from which the string was captured is highlighted.
Qt Linguist lets you track which strings you've translated and also those which still
need translation. The icon to the left-hand side of each of the strings can be one of
the following:
A yellow question mark, indicating that the string doesn't pass all of
Qt Linguist's validation tests, but you're ignoring the failures
A green checkbox, indicating that the string has been translated and is
ready to go
Qt Linguist provides some simple validation tests, such as ensuring that strings with
printf-style arguments have the same number of arguments in each translation.
Qt Linguist also supports phrase books; you might be able to download a phrase
book with common strings already localized to the language you're targeting.
At any point, you can generate a translation file for inclusion in your application by
running lrelease. For example, to create one for our Esperanto strings, we'd use
lrelease as follows:
% lrelease .\QtLinguistExample-epo.ts .\QtLinguistExample-epo.qm
This takes the incoming .ts file and generates a .qm file with the strings.
The .qm files are highly compressed binary files used by Qt directly in the process
of rendering the application.
This code allocates a QTranslator object and loads the indicated translation file into
the translator before installing it into the QApplication object. In this example, we're
hardcoding the language in order to localize to Esperanto.
[ 198 ]
Chapter 10
Note that if you want to support the locale as picked by the system, you might
choose to do it this way:
QString locale = QLocale::system().name();
QTranslator translator;
translator.load(QString("QtLinguistExample-") + locale);
The QLocale class here is a class for managing the system's locale. Here, we use it to
determine the system's locale, and then we attempt to load the localized string file
for the system's current locale.
For this to work, the .qm files for the application need to be locatable by the
application. They should be in the output directory; one way to do this during
development is to turn off shadow builds in Qt Creator, under Build Settings
in Project Pane. As you build your application's installera platform-specific
task outside the scope of this bookyou need to include your .qm files with the
application binary.
For more information on Qt Linguist, see its manual at
http://qt-project.org/doc/qt-5/qtlinguistindex.html.
Then, s contains the string "a b". Second, you need to know about the toString
method of QLocale which formats its argument in a locale-specific way.
So, we could write:
QString currencyValue = QString("%1 %2")
.arg(tr("$")).arg(QLocale::toString(value, 'g', 2)
This uses tr to localize the currency symbol and the QLocale class's static method,
toString, to convert the value of the currency to a string with the locale-specific
decimal separator (a period in the US and Canada, and a comma in Europe).
[ 199 ]
Date formatting is similar; the toString method of QLocale has overloads for the
QDateTime, QDate, and QTime arguments, so you can simply write:
QDateTime whenDateTime = QDateTime::currentDateTime();
QString when = QLocale::toString(whenDate);
This gets the current date and time and stores it in whenDateTime and then makes
a string out of it using the locale's default formatting. The toString method can take
a second argument that determines the output format; it's one of the following:
QLocale::LongFormat: This uses the long version of month and day names
QLocale::ShortFormat: This uses the short version of day and
month names
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 the next chapter, we will take a look at another important aspect of software
development that Qt Creator supports: performance analysis with the QML
Profiler and Valgrind.
[ 200 ]
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and
most internet book retailers.
www.PacktPub.com