Home · Examples 


Internationalization with Qt

Information about Qt jambi's support for internationalization and multiple languages. The internationalization of an application is the process of making the application usable by people in countries other than one's own.

In some cases internationalization is simple, for example, making a US application accessible to Australian or British users may require little more than a few spelling corrections. But to make a US application usable by Japanese users, or a Korean application usable by German users, will require that the software operate not only in different languages, but use different input techniques, character encodings and presentation conventions.

Qt Jambi tries to make internationalization as painless as possible for developers. All input widgets and text drawing methods in Qt Jambi offer built-in support for all supported languages. The built-in font engine is capable of correctly and attractively rendering text that contains characters from a variety of different writing systems at the same time.

Qt supports most languages in use today, in particular:

On Windows, Unix/X11 with FontConfig (client side font support) and Qt for Embedded Linux the following languages are also supported: Many of these writing systems exhibit special features: Qt Jambi tries to take care of all the special features listed above. You usually don't have to worry about these features so long as you use Qt Jambi's input widgets (e.g. QLineEdit, QTextEdit, and derived classes) and Qt Jambi's display widgets (e.g. QLabel).

Support for these writing systems is transparent to the programmer and completely encapsulated in Qt Jambi's text engine. This means that you don't need to have any knowledge about the writing system used in a particular language, except for the following small points:

The following sections give some information on the status of the internationalization (i18n) support in Qt Jambi. See also the Qt Linguist manual.

Step by Step

Writing cross-platform international software with Qt Jambi is a gentle, incremental process. Your software can become internationalized in the following stages:

Use tr() for All Literal Text

Wherever your program uses "quoted text" for text that will be presented to the user, ensure that it is processed by the
QCoreApplication::translate() function. Essentially all that is necessary to achieve this is to use QtJambiObject::tr(). For example:
        public LoginWidget()
        {
            QLabel abel = new QLabel(tr("Password:"));
            ...
        }
This accounts for 99% of the user-visible strings you're likely to write.

If the quoted text is not in a member function of a QtJambiObject subclass, use either the QCoreApplication::translate() function directly:

        void some_static_function()
        {
            QLabel abel = new QLabel(
                        QApplication.instance().translate("LoginWidget", "Password:"));
        }

Use QKeySequence() for Accelerator Values

Accelerator values such as Ctrl+Q or Alt+F need to be translated too. If you hardcode
Qt::CTRL + Qt::Key_Q for "quit" in your application, translators won't be able to override it. The correct idiom is
     exitAct = new QAction(tr("E&xit"), this);
     exitAct.setShortcut(tr("Ctrl+Q", "Quit"));

Produce Translations

Once you are using tr() throughout an application, you can start producing translations of the user-visible text in your program.

The Qt Linguist manual provides further information about Qt Jambi's translation tools, Qt Linguist, lupdate and lrelease.

Translation of a Qt Jambi application is a three-step process:

  1. Run lupdate to extract translatable text from the Java source code of the Qt Jambi application, resulting in a message file for translators (a .ts file). The utility recognizes the tr() construct described above and produces .ts files (usually one per language).
  2. Provide translations for the source texts in the .ts file, using Qt Linguist. Since .ts files are in XML format, you can also edit them by hand.
  3. 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", and .qm files as "object files". The translator edits the .ts files, but the users of your application only need the .qm files. Both kinds of files are platform and locale independent.
Typically, you will repeat these steps for every release of your application. The lupdate utility does its best to reuse the translations from previous releases.

In your application, you must QTranslator::load() the translation files appropriate for the user's language, and install them using QCoreApplication::installTranslator().

linguist, lupdate and lrelease are installed in the bin subdirectory of the base directory Qt Jambi is installed into. Click Help|Manual in Qt Linguist to access the user's manual; it contains a tutorial to get you started. Qt itself contains over 400 strings that will also need to be translated into the languages that you are targeting. You will find translation files for French, German and Simplified Chinese in $QTDIR/translations, as well as a template for translating to other languages. (This directory also contains some additional unsupported translations which may be useful.)

Typically, your application's main() function will look like this:

        public static void main(String args[])
        {
            QApplication.initialize(args);

            QTranslator qtTranslator = new QTranslator();
            qtTranslator.load("classpath:/translations_directory/qt_" + QLocale.system().name() + ".qm");
            QApplication.instance().installTranslator(tTranslator);


            QTranslator myappTranslator = new QTranslator();
            myappTranslator.load("classpath:/translations_directory/myapp_" + QLocale.system().name() + ".qm");
            QApplication.instance().installTranslator(myappTranslator);

            ...
            QApplication.exec();
        }

Localize

Localization is the process of adapting to local conventions, for example presenting dates and times using the locally preferred formats. Such localizations can be accomplished using appropriate tr() strings.
        void setTime(QTime ime)
        {
            if (tr("AMPM") == "AMPM") {
                // 12-hour clock
            } else {
                // 24-hour clock
            }
        }
In the example, for the US we would leave the translation of "AMPM" as it is and thereby use the 12-hour clock branch; but in Europe we would translate it as something else and this will make the code use the 24-hour clock branch.

For localized numbers use the QLocale class.

Localizing images is not recommended. Choose clear icons that are appropriate for all localities, rather than relying on local puns or stretched metaphors. The exception is for images of left and right pointing arrows which may need to be reversed for Arabic and Hebrew locales.

Dynamic Translation

Some applications, such as Qt Linguist, must be able to support changes to the user's language settings while they are still running. To make widgets aware of changes to the installed QTranslators, reimplement the widget's changeEvent() function to check whether the event is a LanguageChange event, and update the text displayed by widgets using the tr() function in the usual way. For example:
    void changeEvent(QEvent vent)
    {
        if (e.type() == QEvent.Type.LanguageChange) {
            titleLabel.setText(tr("Document Title"));
            ...
            okPushButton.setText(tr("K"));
        } else
            QWidget.changeEvent(event);
    }
All other change events should be passed on by calling the default implementation of the function.

The list of installed translators might change in reaction to a LocaleChange event, or the application might provide a user interface that allows the user to change the current application language.

The default event handler for QWidget subclasses responds to the QEvent::LanguageChange event, and will call this function when necessary; other application components can also force widgets to update themselves by posting the LanguageChange event to them.


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01