Qt 国际化

The 国际化 and 本地化 of an application are the processes of adapting the application to different languages, regional differences and technical requirements of a target market. Internationalization means designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization means adapting internationalized software for a specific region or language by adding locale-specific components (such as date, time, and number formats) and translating text.

相关 Qt 类和 API

这些类支持 Qt 应用程序的国际化。

QCollator

根据本地整理算法比较字符串

QCollatorSortKey

可以用于加速字符串整理

QLocale

在数字及其各种语言的字符串表示之间转换

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 controls 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:

  • All East Asian languages (Chinese, Japanese and Korean)
  • All Western languages (using Latin script)
  • 阿拉伯语
  • Cyrillic languages (Russian, Ukrainian, etc.)
  • 希腊语
  • 希伯来语
  • Thai and Lao
  • All scripts in Unicode 6.2 that do not require special processing
  • Bengali
  • Burmese (Myanmar)
  • Devanagari
  • Gujarati
  • Gurmukhi
  • Kannada
  • Khmer
  • Malayalam
  • Tamil
  • Telugu
  • Tibetan

The list above is supported and will work on all platforms as long as the system has fonts to render these writing systems installed.

On Windows, Linux and Unix with FontConfig (client side font support) the following languages are also supported:

  • Dhivehi (Thaana)
  • Syriac
  • N'Ko

On macOS, the following languages are also supported:

  • Oriya
  • Sinhala

Many of these writing systems exhibit special features:

  • Special line breaking behavior. Some of the Asian languages are written without spaces between words. Line breaking can occur either after every character (with exceptions) as in Chinese, Japanese and Korean, or after logical word boundaries as in Thai.
  • Bidirectional writing. Arabic and Hebrew are written from right to left, except for numbers and embedded English text which is written left to right. The exact behavior is defined in the Unicode Technical Annex #9 .
  • Non-spacing or diacritical marks (accents or umlauts in European languages). Some languages such as Vietnamese make extensive use of these marks and some characters can have more than one mark at the same time to clarify pronunciation.
  • Ligatures. In special contexts, some pairs of characters get replaced by a combined glyph forming a ligature. Common examples are the fl and fi ligatures used in typesetting US and European books.

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 controls (e.g. QLineEdit , QTextEdit , and derived classes or the Quick TextInput item) and Qt's display controls (e.g. QLabel and Qt Quick's Text item).

Support for these writing systems is transparent to the programmer and completely encapsulated in Qt 文本引擎 . This means that you don't usually need to have any knowledge of the writing system used in a particular language, unless you want to write your own text input controls. In some languages, such as Arabic or languages from the Indian subcontinent, the width and shape of a glyph changes depending on the surrounding characters. To take this into account, use QTextLayout . Writing input controls also requires some knowledge of the scripts they are going to be used in. Usually, the easiest way is to subclass QLineEdit or QTextEdit .

For more information about how to internationalize source code, see 编写翻译源代码 and Qt Quick 的国际化和本地化 .

产生翻译

Qt provides excellent support for translating Qt C++ and Qt Quick applications into local languages. Release managers, translators, and developers can use the Qt translation tools to accomplish their tasks.

The Qt translation tools, Qt Linguist, lupdate ,和 lrelease are installed in the bin subdirectory of the base directory Qt is installed into. For more information about using them, see the Qt Linguist 手册 .

Qt itself contains several thousands of strings that will also need to be translated into the languages that you are targeting. You will find a number of translation files in the qttranslations repository. Before you start translating Qt, read the wiki page 将 Qt 翻译成其它语言 .

启用翻译

通常,应用程序的 main() 函数看起来像这样:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTranslator myappTranslator;
    myappTranslator.load(QLocale(), QLatin1String("myapp"), QLatin1String("_"), QLatin1String(":/i18n"));
    app.installTranslator(&myappTranslator);
    return app.exec();
}
					

For a translation-aware application, a QTranslator object is created, then a translation is loaded according to the user's preferred UI display locale at runtime, and finally, the translator object is installed into the application.

定位 Qt 翻译

还可以使用 QLibraryInfo::location () to locate the translations for the Qt modules used. You can request the path to the translations at run-time by passing QLibraryInfo::TranslationsPath to this function.

可用分类

Qt 翻译分类位于 qttranslations 存储库。

警告: Qt translations are contributed by the Qt community, and provided without any guarantees. Translations migh be missing, outdated, or entirely incorrect, up to the point of being malicious. It is recommended that you audit any translations you ship.

The qt_ meta catalog contains the still-existing Qt translations that were included in the qt_ catalog in Qt 4. It was created to make porting applications from Qt 4 to Qt 5 easier. The meta catalog depends on translations that might be absent, because they belong to unnecessary or deprecated modules, which might cause the loading of the translations to fail. If you use modules that are new in Qt 5 in your application, you must specify the names of the catalogs for those modules even if you use the meta catalog.

The following table lists the translation catalogs available for the Qt modules and tools in Qt.

Qt 模块或工具 分类
Qt Assistant assistant_
Qt Bluetooth qtconnectivity_
Qt Concurrent qtbase_
Qt Core qtbase_
Qt D-Bus qtbase_
Qt Designer designer_
Qt GUI qtbase_
Qt Help qt_help_
Qt Linguist linguist_
Qt Location qtlocation_
Qt Multimedia qtmultimedia_
Qt Multimedia Widgets qtmultimedia_
Qt NFC qtconnectivity_
Qt Network qtbase_
Qt Positioning qtlocation_
Qt Print Support qtbase_
Qt QML qtdeclarative_
Qt Quick qtdeclarative_
Qt Quick Controls qtquickcontrols2_
Qt Quick Widgets qtdeclarative_
Qt Serial Port qtserialport_
Qt SQL qtbase_
Qt WebEngine qtwebengine_
Qt WebSockets qtwebsockets_
Qt Widgets qtbase_
范例:基本 Qt 模块

For example, to locate translations for basic Qt modules, such as Qt Core, Qt GUI, Qt Network, and Qt Widgets, add the following code to the main() 函数:

    QTranslator qtTranslator;
    qtTranslator.load(QLocale::system(), QStringLiteral("qtbase_"));
    app.installTranslator(&qtTranslator);
					
					

支持编码

The QTextCodec class and the facilities in QTextStream make it easy to support many legacy 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 external 8-bit data. QTextCodec::codecForLocale () returns a codec that can be used to convert between this locale encoding and Unicode.

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);
					

Unicode I/O should be used as this maximizes the portability of documents between users around the world. In many cases it is however still necessary to support other encodings as your users will need to process existing documents. The most important additional 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. For localized numbers, dates, times and currency strings, 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 QTranslator objects, 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 (event->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.

系统支持

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.

Unix/X11
  • Locale-oriented fonts and input methods. Qt hides these and provides Unicode input and output.
  • Filesystem conventions such as UTF-8 are today used by default in most Unix variants. All Qt file functions allow Unicode, but convert filenames to the local 8-bit encoding, as this is the Unix convention (see QFile::setEncodingFunction() to explore alternative encodings).
  • File I/O defaults to the local 8-bit encoding, with Unicode options in QTextStream .
  • Some older Unix distributions contain only partial support for some locales. For example, if you have a /usr/share/locale/ja_JP.EUC directory, this does not necessarily mean you can display Japanese text; you also need to have Japanese fonts installed, and the /usr/share/locale/ja_JP.EUC directory needs to be complete. For best results, use complete locales from your system vendor.
Linux
  • Qt provides full Unicode support, including input methods, fonts, clipboard, drag-and-drop.
  • The file system is usually encoded in UTF-8 on all modern Linux distributions and should not pose a problem. File I/O defaults to UTF-8.
Windows
  • Qt provides full Unicode support, including input methods, fonts, clipboard, drag-and-drop and file names.
  • File I/O defaults to Latin1, with Unicode options in QTextStream . Note that some Windows programs do not understand big-endian Unicode text files even though that is the order prescribed by the Unicode Standard in the absence of higher-level protocols.
macOS

For details on macOS-specific translation, refer to the Qt for macOS issues document here .

翻译过程概述

Qt Linguist 范例

使用 Qt Linguist 国际化 Qt 应用程序

Qt Linguist 手册

Qt Linguist 手册:开发者

Qt Linguist 手册:发行管理者

Qt Linguist 手册:TS 文件格式

Qt Linguist 手册:基于文本 ID 的翻译

Text ID based internationalization provides support for large scale projects with many target locales and many texts to translate

Qt Linguist 手册:翻译者

复数翻译规则

A summary of the translation rules for plurals produced by Qt's i18n tools.

编写翻译源代码

How to write source code in a way that makes it possible for user-visible text to be translated.

编写翻译源代码

内容