FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
TABLE OF CONTENTS
May 23, 2008

Qt and Windows CE

(Page 4 of 5)

Internationalizing Applications

Notice that the languages in Figure 3 are also different. Figure 3(a) is Norwegian, while 3(b) is German (Swiss).

To internationalize applications in Qt:

  1. Use QString for all user-visible text. Since QString uses Unicode 4.0 encoding internally, every language in the world can be processed transparently using familiar text-processing operations.
  2. Wherever your program uses "quoted text" for user-visible text, ensure that it is processed by the tr() function.
  3. Run the application lupdate to extract translatable text from the C++ source code of the Qt application.
  4. Use Qt Linguist and translate your extracted text to the languages you want to support.
  5. Run the application lrelease to obtain a lightweight message file suitable for end use.

Step 4 is usually done by a translator (like QT Linguist), while the other steps are done by you.

[Click image to view at full size]

Figure 3: (a) Image Viewer running in Windows Mobile 6.0 with default native style; (b) Image Viewer running in Windows Mobile 6.0 with custom style sheet enabled.

To switch the language you simply call the QCoreApplication::installTranslator() function. When called during the construction of an application, nothing else is needed for the app to run in the selected language. However, to change the language at runtime, you need to reimplement changeEvent() of the PreviewWindow class to handle LanguageChange:

void changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) { ui.retranslateUi(this); ui.imageName->setTitle (imageCount() ? currentImageName() : tr("no images found")); ui.imageDimension->setText (QString(tr("Dimensions: %1X%2")) .arg(ui.currentImage->pixmap() ? ui.currentImage-> pixmap()->width() : 0) .arg(ui.currentImage->pixmap() ? ui.currentImage->pixmap()-> height() : 0)); } }

You call ui.retranslateUi(this) to translate all the user-visible strings that entered when creating the GUI in Qt Designer. However, you also need to update the widgets that have user-visible strings updated by code. That is why I call setTitle() and setText() for the imageName and imageDimension. Notice how the strings are wrapped inside a tr() function. With this reimplementation of changeEvent(), the application supports changing the language at runtime.

Previous Page | 1 Qt | 2 An Image Viewer Example | 3 Customized Look | 4 Internationalizing Applications | 5 It's Small, Powerful, and Cross Platform Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK