Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Qt and Windows CE


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.