![]() |
Home · Examples |
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. 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:
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.
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:
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:")); }
exitAct = new QAction(tr("E&xit"), this); exitAct.setShortcut(tr("Ctrl+Q", "Quit"));
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:
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: 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. 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.
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. 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.
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies)
Trademarks