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:
First, it enables Qt to find every string that needs localization
Second, 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 with &
to assign a keyboard accelerator and it gets the default system (a key combination with Alt for Windows, Command for Apple...