Qt localization using .ts files
Lingobit Localizer allows you to localize Qt files with minimum effort. Here are the basic steps to make Qt application multilingual:
- Prepare all localizable string in your source code
- Run lupdate utility for your project. It will create .ts files with extracted strings.
- Localize .ts file with Lingobit Localizer. It will create translated .ts file for each language.
- Compile all localized .ts files using lrelease utility. It will create .qm files that should be distributed with your application.
- Add code to load appropriate language at startup of your application
Now we will discuss it in more details.
Step 1: Prepare source code
Wherever your program uses "quoted text" for text that will be presented to the user, ensure that it is processed by the QApplication::translate() function. Essentially all that is necessary to achieve this is to use QObject::tr(). For example, assuming the LoginWidget is a subclass of QWidget:
actionExit = new QAction(tr("Exit"), this);
This accounts for 99% of the user-visible strings you're likely to write.
If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). The macros expand to just the text (without the context).
If you disable the const char* to QString automatic conversion by compiling your software with the macro QT_NO_CAST_ASCII defined, you'll be very likely to catch any strings you are missing. See QString::fromLatin1() for more information. Disabling the conversion makes programming cumbersome.
If your source language uses characters outside Latin-1, you might find QObject::trUtf8() more convenient than QObject::tr(), as tr() depends on the QApplication::defaultCodec(), which makes it more fragile than QObject::trUtf8().
For accelerators you need to use QKeySequence ().
You can find more information about Qt localization at http://doc.qt.nokia.com/3.1/i18n.html.
Step 2: Create .ts files using lupdate utility
Run lupdate to extract translatable text from the C++ source code of the Qt application, resulting in a message file for translators (a .ts file). The utility recognizes the tr() construct and the QT_*_NOOP macros described above and produces .ts files.
Step 3: Localize .ts files using Lingobit Localizer
Create new project in Lingobit Localizer and add .ts file into it. Translate it and create localized files. If application changed, use Scan for Changes and translate only new and modified strings.
Lingobit Localizer is much more powerful than Qt Linguist and provides a lot of productivity tools like automatic translation, automatic validation, translation reuse and much more. More about Lingobit Localizer.
Step 4: Compile localized .ts files into .qm files
Run lrelease to obtain a light-weight message file (a .qm file) from the .ts file, suitable only for end use. Think of the .ts files as "source files" for localization, and .qm files as "object files". Users of your application only need the .qm files.
Step 5: Modify startup code to load translation
In your application you need to create QTranslator, then load appropriate language and install it.
qt_localization.load( QString( "Qt_Localization_" ) +
QLocale::system().name() ); //Load a translation files appropriate
// for the user's language
a.installTranslator( &qt_localization );
You can find a sample of localized Qt application at Lingobit Localizer startup page. It is called "Qt localization sample". You can also find it at Shared Documents\Lingobit Localizer ..\Samples\Qt.
"Qt_Localization.ts" file contains all localizable strings from Qt_localization.pro project. .TS file was created using make_ts.cmd located in Sources folder.
Localized application uses .QM files. To get translated .QM files you should:
- Open "Qt localization.loc" with Lingobit Localizer and click "Create localized". Translation source files (.ts files) will be created in Release directory.
- Run compile_ts.cmd located in Release directory to create message files (a .qm files) from translated .ts files.
- Then run application in Release directory. Application requires installation of Qt runtime files.