The Qt translation system is very simple: use the
QObject::tr() method to define the texts to be translated, create a .ts file for each language, execute the
lupdate to update those files with the new phrases of the project, translate them and execute
lrelease to generate the file that Qt uses with the translations. The system is very powerful, for example, nothing special is needed to change to Eastern languages, it simply works, in addition to providing translation aids such as phrase books to have “pre-made” translations, or to detect inconsistencies in parameters or to show different punctuation signs between translations.
As always, I don’t want to give a course, just collect some tips born from day to day.
The same is one of the most important and “non-optional” tips. Let’s see.
“Pepe’s car”, “Pepe’s car”. A quick example that each language has different constructions. And that? Let’s imagine that we want our application to ask the name of the user and show the previous message. Our first code could be something like:
QString msg = tr("The car of") + strName;
The problem comes when translating it: the string to be translated is only “The car of”, while in our code we will concatenate the name below. It is not possible to translate the phrase into English using this method.
Qt offers a solution based on parameters: we build a single phrase indicating the place of the elements. At run time we substitute those parameters with the
QString::arg method. Like this:
QString msg = tr("The car of %1").arg(strName);
The string to be translated in English will have the form “% 1’s car”, and will work perfectly.
Changes in the original texts
A project is a living being, and it is normal for the texts shown to change. Every time that happens, you must execute
lupdate to update the .ts file. Now, when changing texts or removing phrases, the .ts file saves the old entries (very good on the one hand to re-translate, especially if they are minor changes such as a punctuation mark or a lack of spelling in the original text). As the project grows, old entries can annoy more than help. To remove them, just run
lupdate -noobsolete (if you use the Visual Studio plugin you can specify this parameter in the Qt project options).
It is possible to define custom properties to
QWidget in the Qt Designer (for example, to distinguish groups of objects easily). I will not go into detail now of this, I promise a post another day. What does it have to do with translations? That if the assigned value is a text string, by default Qt Designer marks it as “translatable”, so our .ts file could be overwhelmed with properties that we don’t want to translate. To avoid this, simply uncheck the “Translatable” box of the property.
One more thing…
The last tip is not directly from the translations but from the
QString class and the Visual C++. The code editor uses a different encoding than the one used by Qt by default, which can lead to some characters showing badly when running the application. A typical case is the degree symbol (º). The problem is magnified if that symbol is in a translation, since then Qt will not associate the texts ‘origin’ and ‘translation’ correctly. The most comfortable solution I have found is to use a parameter for this and insert the corresponding Unicode character:
QString degreeMsg = tr("Right now they make 25%1").arg(QChar(0260));