![]() |
Home · Examples |
This example is a slightly more involved and introduces a key Qt Linguist concept: "contexts". It also shows how to use two or more languages.
See the Qt Linguist manual for more information about translating Qt application.Line by Line Walkthrough
In arrowpad.h we define the ArrowPad subclass which is a subclass of QWidget. In the screenshot above, the central widget with the four buttons is an ArrowPad.
class ArrowPad : public QWidget { Q_OBJECTWhen lupdate is run it not only extracts the source texts but it also groups them into contexts. A context is the name of the class in which the source text appears. Thus, in this example, "ArrowPad" is a context: it is the context of the texts in the ArrowPad class. The Q_OBJECT macro defines tr(x) in ArrowPad like this:
qApp->translate("ArrowPad", x)Knowing which class each source text appears in enables Qt Linguist to group texts that are logically related together, e.g. all the text in a dialog will have the context of the dialog's class name and will be shown together. This provides useful information for the translator since the context in which text appears may influence how it should be translated. For some translations keyboard accelerators may need to be changed and having all the source texts in a particular context (class) grouped together makes it easier for the translator to perform any accelerator changes without introducing conflicts.
In arrowpad.cpp we implement the ArrowPad class.
The following code example is written in c++.
upButton = new QPushButton(tr("&Up")); downButton = new QPushButton(tr("&Down")); leftButton = new QPushButton(tr("&Left")); rightButton = new QPushButton(tr("&Right"));We call ArrowPad::tr() for each button's label since the labels are user-visible text.
class MainWindow : public QMainWindow { Q_OBJECTIn the screenshot above, the whole window is a MainWindow. This is defined in the mainwindow.h header file. Here too, we use Q_OBJECT, so that MainWindow will become a context in Qt Linguist.
arrowPad = new ArrowPad;In the implementation of MainWindow, mainwindow.cpp, we create an instance of our ArrowPad class.
exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));We also call MainWindow::tr() twice, once for the action and once for the shortcut.
Note the use of tr() to support different keys in other languages. "Ctrl+Q" is a good choice for Quit in English, but a Dutch translator might want to use "Ctrl+A" (for Afsluiten) and a German translator "Strg+E" (for Beenden). When using tr() for Ctrl key accelerators, the two argument form should be used with the second argument describing the function that the accelerator performs.
Our main() function is defined in main.cpp as usual.
The following code example is written in c++.
QTranslator translator; translator.load(QString("arrowpad_") + locale); app.installTranslator(&translator);We choose which translation to use according to the current locale. QLocale::system() can be influenced by setting the LANG environment variable, for example. Notice that the use of a naming convention that incorporates the locale for .qm message files, (and .ts files), makes it easy to implement choosing the translation file according to locale.
If there is no .qm message file for the locale chosen the original source text will be used and no error raised.Translating to French and Dutch
We'll begin by translating the example application into French. Start Qt Linguist with arrowpad_fr.ts. You should get the seven source texts ("&Up", "&Left", etc.) grouped in two contexts ("ArrowPad" and "MainWindow").
Now, enter the following translations:
Save the file and do the same for Dutch working with arrowpad_nl.ts:
Type
lrelease arrowpad.proThis should create both arrowpad_fr.qm and arrowpad_nl.qm. Set the LANG environment variable to fr. In Unix, one of the two following commands should work
export LANG=fr setenv LANG frIn Windows, either modify autoexec.bat or run
set LANG=frWhen you run the program, you should now see the French version:
Set LANG=fr_CA (French Canada) and run the example program again. Explain why the result is the same as with LANG=fr.
Change one of the accelerators in the Dutch translation to eliminate the conflict between &Bestand and &Boven.
Copyright © 2008 Nokia | Trademarks | Qt Jambi 4.4.3_01 |