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.

  • 國際化 means designing an application so that it can be adapted to various languages and regions without engineering changes.
  • 本地化 means adapting internationalized applications for a specific region or language by adding locale-specific components (such as date, time, and number formats) and translating text.

The need for internationalization ranges from spelling changes to enabling the application to operate in different languages and to use different input techniques, character encoding, and presentation conventions.

All input controls and text drawing methods in Qt offer built-in support for all supported languages. The built-in font engine correctly renders text that contains characters from a variety of different writing systems at the same time.

For more information about
Internationalizing source code 編寫翻譯源代碼
Configuring and deploying translations, as well as using existing Qt module translations 本地化應用程序
Using the Qt translation tools Qt Linguist 手冊

The following video shows how to internationalize and localize a simple example application:

用於國際化的 Qt 類

The following classes support internationalizing of Qt applications.

QCollator 根據本地整理算法比較字符串
QCollatorSortKey 可以用於加速字符串整理
QLocale 在數字及其各種語言的字符串錶示之間轉換
QStringConverter Base class for encoding and decoding text
QStringDecoder State-based decoder for text
QStringEncoder State-based encoder for text
QTextCodec 在文本編碼間轉換
QTextDecoder 基於狀態的解碼器
QTextEncoder 基於狀態的編碼器
QTranslator 用於文本輸齣的國際化支持

編寫翻譯源代碼 for more information about how to use the classes in applications.

語言和書寫係統

Qt supports most 語言 in use today.

Input controls, such as the Qt Quick TextInput 類型和 QLineEdit , QTextEdit , and derived classes, as well as display controls, such as the Text 類型和 QLabel class handle the following special features of the different writing systems:

  • Line breaks

    Some of the Asian languages are written without spaces between words. Line breaking can occur either after any character (with exceptions) as in Chinese, Japanese and Korean, or after logical word boundaries as in Thai.

  • Bidirectional writing

    阿拉伯語和希伯來語從右嚮左書寫,除數字和嵌入的英文文本是從左嚮右書寫外。準確行為的定義是在 Unicode 技術附錄 #9 .

  • Non-spacing or diacritical marks, such as 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 文本引擎 supports different writing systems that work on all platforms if the fonts for rendering them are installed.

You do not need to know about 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 in C++ code, use QTextLayout 。編寫輸入控件還要求一些腳本知識,將要使用它們。通常,最輕鬆方式是子類化 QLineEdit or QTextEdit .

編碼

Encoding is relevant both for application source files and the text files that the application reads or writes.

Encoding Source Code

QML documents are always encoded in UTF-8 format. Since Qt 6, 8-bit UTF-8 is the predominant encoding also in Qt C++.

The lupdate tool extracts UI strings from your application. It expects all source code to be encoded in UTF-8 by default.

However, some editors, such as Visual Studio, use a different encoding by default. One way to avoid encoding issues is to limit any source code to ASCII, and use escape sequences for translatable strings with other characters, for example:

label->setText(tr("F\374r \310lise"));
					

QString::toUtf8 () returns the text in UTF-8 encoding, which preserves Unicode information while looking like plain ASCII if the text is wholly ASCII. To convert Unicode to local 8-bit encoding, use QString::toLocal8Bit (). On Unix systems, this is equivalent to toUtf8() . On Windows, the system's current code page is used.

For converting from UTF-8 and local 8-bit encoding to QString ,使用 QString::fromUtf8 () 和 QString::fromLocal8Bit () convenience functions.

Encoding Text Input/Output

使用 QTextStream::setEncoding () to set common encoding for text streams.

If you need some other legacy encoding, use the QTextCodec class from the Qt5Compat module.

When an application starts, the locale of the machine determines the 8-bit encoding used for external 8-bit data. QTextCodec::codecForLocale () returns a codec that you can use to convert between this locale encoding and Unicode.

The application may occasionally require encoding 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);
					

The following code demonstrates the conversion from ISO 8859-5 Cyrillic to Unicode:

QByteArray encodedString = ...; // some ISO 8859-5 encoded text
QTextCodec *codec = QTextCodec::codecForName("ISO 8859-5");
QString string = codec->toUnicode(encodedString);
					

For a complete list of supported encodings see the QTextCodec 文檔編製。

Operating and Windowing Systems

Qt 運行的某些 OS (操作係統) 和窗口係統,僅有限支持 Unicode。可用於底層係統的支持級彆對 Qt 在這些平颱提供的支持有一定影響,盡管一般而言 Qt 應用程序不需要太擔心特定平颱的局限性。

Unix/X11

  • Qt hides locale-oriented fonts and input methods and provides Unicode input and output.
  • Most Unix variants use filesystem conventions such as UTF-8 by default. All Qt file functions allow Unicode, but convert filenames to the local 8-bit encoding, as this is the Unix convention.
  • 文件 I/O 默認為本地 8 位編碼,采用 Unicode 選項在 QTextStream .
  • Some older Unix distributions contain only partial support for some locales. For example, even if you have a /usr/share/locale/ja_JP.EUC directory, you cannot display Japanese text unless you install Japanese fonts and the directory is complete. For best results, use complete locales from your system vendor.

Linux

  • Qt provides full Unicode support, including input methods, fonts, clipboard, and drag-and-drop.
  • The file system is encoded in UTF-8 on all modern Linux distributions. File I/O defaults to UTF-8.

Windows

  • Qt provides full Unicode support, including input methods, fonts, clipboard, drag-and-drop, and file names.
  • 文件 I/O 默認為 Latin1,采用 Unicode 選項在 QTextStream . However, 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.
本地化應用程序 Localizing Qt and Qt Quick apps into multiple languages.
Qt Linguist 範例 使用 Qt Linguist 國際化 Qt 應用程序
Qt Linguist 手冊 Using Qt translation tools: lupdate, lrelease, and Qt Linguist
Text ID based translations 基於文本 ID 的國際化,為具有很多目標區域設置和很多要翻譯文本的大型工程提供支持
Translation Rules for Plural Forms A summary of the translation rules for plural forms produced by Qt's translation tools.
編寫翻譯源代碼 Writing source code that enables the localization of applications.