|  | Home · Overviews · Examples

# QTranslator Class Reference
[com.trolltech.qt.core module]

The `QTranslator` class provides internationalization support for text output. More...

Inherits `QObject`.

## Detailed Description

The `QTranslator` class provides internationalization support for text output.

An object of this class contains a set of translations from a source language to a target language. `QTranslator` provides functions to look up translations in a translation file. Translation files are created using Qt Linguist.

The most common use of `QTranslator` is to: load a translation file, install it using QApplication::installTranslator(), and use it via QObject::tr(). Here's the `main()` function from the Hello tr() example:

        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);

            QTranslator translator;
            translator.load("hellotr_la");
            app.installTranslator(&translator);

            QPushButton hello(QPushButton::tr("Hello world!"));
            hello.resize(100, 30);

            hello.show();
            return app.exec();
        }

Note that the translator must be created before the application's widgets.

Most applications will never need to do anything else with this class. The other functions provided by this class are useful for applications that work on translator files.

It is possible to lookup a translation using translate() (as tr() and QApplication::translate() do). The translate() function takes up to three parameters:

- The context - usually the class name for the tr() caller.
- The source text - usually the argument to tr().
- The comment - an optional comment that helps disambiguate different uses of the same text in the same context.

For example, the "Cancel" in a dialog might have "Anuluj" when the program runs in Polish (in this case the source text would be "Cancel"). The context would (normally) be the dialog's class name; there would normally be no comment, and the translated text would be "Anuluj".

But it's not always so simple. The Spanish version of a printer dialog with settings for two-sided printing and binding would probably require both "Activado" and "Activada" as translations for "Enabled". In this case the source text would be "Enabled" in both cases, and the context would be the dialog's class name, but the two items would have disambiguating comments such as "two-sided printing" for one and "binding" for the other. The comment enables the translator to choose the appropriate gender for the Spanish version, and enables Qt to distinguish between translations.

See also `QApplication::installTranslator`, `QApplication::removeTranslator`, `QObject::tr`, `QApplication::translate`, I18N Example, Hello tr() Example, Arrow Pad Example, and Troll Print Example.