The internationalization of an application is the process of making the application usable by people in countries other than one's own.
这些类支持 Qt 应用程序的国际化。
| QInputContext | Abstracts the input method dependent data and composing state |
| QLocale | 在数字及其各种语言的字符串表示之间转换 |
| QSystemLocale | Can be used to finetune the system locale of the user |
| QTextCodec | 在文本编码间转换 |
| QTextDecoder | 基于状态的解码器 |
| QTextEncoder | 基于状态的编码器 |
| QTranslator | 用于文本输出的国际化支持 |
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.
Qt tries to make internationalization as painless as possible for developers. All input widgets and text drawing methods in Qt 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:
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 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's input widgets (e.g. QLineEdit , QTextEdit , and derived classes) and Qt's display widgets (e.g. QLabel ).
Support for these writing systems is transparent to the programmer and completely encapsulated in Qt 文本引擎 . 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:
The following sections give some information on the status of the internationalization (i18n) support in Qt. See also the Qt Linguist manual .
Writing cross-platform international software with Qt is a gentle, incremental process. Your software can become internationalized in the following stages:
由于
QString
uses the Unicode 5.1 encoding internally, every language in the world can be processed transparently using familiar text processing operations. Also, since all Qt functions that present text to the user take a
QString
as a parameter, there is no
char *
to
QString
conversion overhead.
Strings that are in "programmer space" (such as
QObject
names and file format texts) need not use
QString
; the traditional
char *
或
QByteArray
class will suffice.
You're unlikely to notice that you are using Unicode;
QString
,和
QChar
are just like easier versions of the crude
const char *
and char from traditional C.
Wherever your program uses "quoted text" for text that will be presented to the user, ensure that it is processed by the
QCoreApplication::translate
() function. Essentially all that is necessary to achieve this is to use
QObject::tr
(). For example, assuming the
LoginWidget
是子类化的
QWidget
:
LoginWidget::LoginWidget() { QLabel *label = new QLabel(tr("Password:")); ... }
This accounts for 99% of the user-visible strings you're likely to write.
若引号文本不在成员函数中对于 QObject subclass, use either the tr() function of an appropriate class, or the QCoreApplication::translate () 函数直接:
void some_global_function(LoginWidget *logwid) { QLabel *label = new QLabel( LoginWidget::tr("Password:"), logwid); } void same_global_function(LoginWidget *logwid) { QLabel *label = new QLabel( qApp->translate("LoginWidget", "Password:"), logwid); }
If you need to have translatable text completely outside a function, there are two macros to help:
QT_TR_NOOP
() 和
QT_TRANSLATE_NOOP
(). They merely mark the text for extraction by the
lupdate
utility described below. The macros expand to just the text (without the context).
Example of QT_TR_NOOP ():
QString FriendlyConversation::greeting(int type) { static const char *greeting_strings[] = { QT_TR_NOOP("Hello"), QT_TR_NOOP("Goodbye") }; return tr(greeting_strings[type]); }
Example of QT_TRANSLATE_NOOP ():
static const char *greeting_strings[] = { QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"), QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye") }; QString FriendlyConversation::greeting(int type) { return tr(greeting_strings[type]); } QString global_greeting(int type) { return qApp->translate("FriendlyConversation", greeting_strings[type]); }
若禁用
const char *
to
QString
automatic conversion by compiling your software with the macro
QT_NO_CAST_FROM_ASCII
defined, you'll be very likely to catch any strings you are missing. See
QString::fromLatin1
() for more information. Disabling the conversion can make programming a bit cumbersome.
If your source language uses characters outside Latin1, you might find QObject::trUtf8 () more convenient than QObject::tr (), as tr() depends on the QTextCodec::codecForTr (), which makes it more fragile than QObject::trUtf8 ().
Accelerator values such as Ctrl+Q or Alt+F need to be translated too. If you hardcode Qt::CTRL + Qt::Key_Q for "quit" in your application, translators won't be able to override it. The correct idiom is
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
The QString::arg () functions offer a simple means for substituting arguments:
void FileCopier::showProgress(int done, int total, const QString ¤tFile) { label.setText(tr("%1 of %2 files copied.\nCopying: %3") .arg(done) .arg(total) .arg(currentFile)); }
In some languages the order of arguments may need to change, and this can easily be achieved by changing the order of the % arguments. For example:
QString s1 = "%1 of %2 files copied. Copying: %3"; QString s2 = "Kopierer nu %3. Av totalt %2 filer er %1 kopiert."; qDebug() << s1.arg(5).arg(10).arg("somefile.txt"); qDebug() << s2.arg(5).arg(10).arg("somefile.txt");
produces the correct output in English and Norwegian:
5 of 10 files copied. Copying: somefile.txt Kopierer nu somefile.txt. Av totalt 10 filer er 5 kopiert.
Once you are using tr() throughout an application, you can start producing translations of the user-visible text in your program.
The
Qt Linguist manual
provides further information about Qt's translation tools,
Qt Linguist
,
lupdate
and
lrelease
.
Translation of a Qt application is a three-step process:
lupdate
to extract translatable text from the C++ source code of the Qt application, resulting in a message file for translators (a TS file). The utility recognizes the tr() construct and the
QT_TR*_NOOP()
macros described above and produces TS files (usually one per language).
lrelease
to obtain a light-weight message file (a QM file) from the TS file, suitable only for end use. Think of the TS files as "source files", and QM files as "object files". The translator edits the TS files, but the users of your application only need the QM files. Both kinds of files are platform and locale independent.
Typically, you will repeat these steps for every release of your application. The
lupdate
utility does its best to reuse the translations from previous releases.
Before you run
lupdate
, you should prepare a project file. Here's an example project file (
.pro
file):
HEADERS = funnydialog.h \ wackywidget.h SOURCES = funnydialog.cpp \ main.cpp \ wackywidget.cpp FORMS = fancybox.ui TRANSLATIONS = superapp_dk.ts \ superapp_fi.ts \ superapp_no.ts \ superapp_se.ts
When you run
lupdate
or
lrelease
, you must give the name of the project file as a command-line argument.
In this example, four exotic languages are supported: Danish, Finnish, Norwegian and Swedish. If you use
qmake
, you usually don't need an extra project file for
lupdate
; your
qmake
project file will work fine once you add the
TRANSLATIONS
entry.
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 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.)
通常,应用程序的
main()
函数看起来像这样:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); app.installTranslator(&qtTranslator); QTranslator myappTranslator; myappTranslator.load("myapp_" + QLocale::system().name()); app.installTranslator(&myappTranslator); ... return app.exec(); }
Note the use of
QLibraryInfo::location
() to locate the Qt translations. Developers should request the path to the translations at run-time by passing
QLibraryInfo::TranslationsPath
to this function instead of using the
QTDIR
environment variable in their applications.
The QTextCodec class and the facilities in QTextStream make it easy to support many input and output encodings for your users' data. When an application starts, the locale of the machine will determine the 8-bit encoding used when dealing with 8-bit data: such as for font selection, text display, 8-bit text I/O, and character input.
The application may occasionally require encodings other than the default local 8-bit encoding. For example, an application in a Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might need to output Cyrillic in the ISO 8859-5 encoding. Code for this would be:
QString string = ...; // some Unicode text QTextCodec *codec = QTextCodec::codecForName("ISO 8859-5"); QByteArray encodedString = codec->fromUnicode(string);
For converting Unicode to local 8-bit encodings, a shortcut is available: the QString::toLocal8Bit () function returns such 8-bit data. Another useful shortcut is QString::toUtf8 (), which returns text in the 8-bit UTF-8 encoding: this perfectly preserves Unicode information while looking like plain ASCII if the text is wholly ASCII.
For converting the other way, there are the QString::fromUtf8 () 和 QString::fromLocal8Bit () convenience functions, or the general code, demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode conversion:
QByteArray encodedString = ...; // some ISO 8859-5 encoded text QTextCodec *codec = QTextCodec::codecForName("ISO 8859-5"); QString string = codec->toUnicode(encodedString);
Ideally Unicode I/O should be used as this maximizes the portability of documents between users around the world, but in reality it is useful to support all the appropriate encodings that your users will need to process existing documents. In general, Unicode (UTF-16 or UTF-8) is best for information transferred between arbitrary people, while within a language or national group, a local standard is often more appropriate. The most important encoding to support is the one returned by QTextCodec::codecForLocale (), as this is the one the user is most likely to need for communicating with other people and applications (this is the codec used by local8Bit()).
Qt supports most of the more frequently used encodings natively. For a complete list of supported encodings see the QTextCodec 文档编制。
In some cases and for less frequently used encodings it may be necessary to write your own
QTextCodec
subclass. Depending on the urgency, it may be useful to contact Qt's technical support team or ask on the
qt-interest
mailing list to see if someone else is already working on supporting the encoding.
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 Clock::setTime(const QTime &time) { 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.
For localized numbers use the QLocale 类。
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.
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 MyWidget::changeEvent(QEvent *event) { if (e->type() == QEvent::LanguageChange) { titleLabel->setText(tr("Document Title")); ... okPushButton->setText(tr("&OK")); } else QWidget::changeEvent(event); }
All other change events should be passed on by calling the default implementation of the function.
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.
LanguageChange events are posted when a new translation is installed using the QCoreApplication::installTranslator () function. Additionally, other application components can also force widgets to update themselves by posting LanguageChange events to them.
It is sometimes necessary to provide internationalization support for strings used in classes that do not inherit
QObject
或使用
Q_OBJECT
macro to enable translation features. Since Qt translates strings at run-time based on the class they are associated with and
lupdate
looks for translatable strings in the source code, non-Qt classes must use mechanisms that also provide this information.
One way to do this is to add translation support to a non-Qt class using the Q_DECLARE_TR_FUNCTIONS () macro; for example:
class MyClass { Q_DECLARE_TR_FUNCTIONS(MyClass) public: MyClass(); ... };
This provides the class with
tr()
functions that can be used to translate strings associated with the class, and makes it possible for
lupdate
to find translatable strings in the source code.
另外,
QCoreApplication::translate
() function can be called with a specific context, and this will be recognized by
lupdate
and Qt Linguist.
Some of the operating systems and windowing systems that Qt runs on only have limited support for Unicode. The level of support available in the underlying system has some influence on the support that Qt can provide on those platforms, although in general Qt applications need not be too concerned with platform-specific limitations.
/usr/share/locale/ja_JP.EUC
directory, this does not necessarily mean you can display Japanese text; you also need JIS encoded fonts (or Unicode fonts), and the
/usr/share/locale/ja_JP.EUC
directory needs to be complete. For best results, use complete locales from your system vendor.
For details on Mac-specific translation, refer to the Qt/Mac Specific Issues document here .
| Internationalization Example | This example shows how to enable text translation in QML. |
| Qt Linguist 范例 | Using Qt Linguist to internationalize your Qt application. |
| Qt Linguist 手册 | |
| Qt Linguist Manual: Programmers | |
| Qt Linguist 手册:发行管理者 | |
| Qt Linguist 手册:TS 文件格式 | |
| Qt Linguist 手册:翻译者 | |
| 编写翻译源代码 | How to write source code in a way that makes it possible for user-visible text to be translated. |