Qt 国际化

The 国际化 and 本地化 对于应用程序是使应用程序适配不同语言、地区差异及目标市场技术要求的过程。

  • 国际化 意味着使设计应用程序可以适配各种语言和地区,且不用改变工程。
  • 本地化 意味着通过添加特定区域设置组件 (譬如:日期、时间及数字格式) 和翻译文本,使国际化应用程序适配特定地区 (或语言)。

国际化需要的范围从拼写改变到使应用程序能以不同语言运转,和使用不同输入技术、字符编码及表示约定。

Qt 中的所有输入控件和文本绘制方法,有为所有支持语言提供内置支持。内置字体引擎可以正确渲染,同时包含来自各种不同书写系统字符的文本。

了解更多有关信息
国际化源代码 编写翻译源代码
配置和部署翻译,及使用现有 Qt 模块翻译 本地化应用程序
使用 Qt 翻译工具 Qt Linguist 手册

以下视频展示如何国际化和本地化简单范例应用程序:

用于国际化的 Qt 类

下列类支持 Qt 应用程序的国际化。

QCollator 根据本地整理算法比较字符串
QCollatorSortKey 可以用于加速字符串整理
QLocale 在数字及其各种语言的字符串表示之间转换
QStringConverter 用于编码和解码文本的基类
QStringDecoder 用于文本基于状态的解码器
QStringEncoder 用于文本基于状态的编码器
QTextCodec 在文本编码间转换
QTextDecoder 基于状态的解码器
QTextEncoder 基于状态的编码器
QTranslator 用于文本输出的国际化支持

编写翻译源代码 了解如何在应用程序中使用类的更多有关信息。

语言和书写系统

Qt 支持大多数 languages 今天在使用。

输入控件,譬如 Qt Quick TextInput 类型和 QLineEdit , QTextEdit ,和派生类,及显示控件,譬如 Text 类型和 QLabel 类处理下列不同书写系统的特殊特征:

  • 换行

    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.

  • 双向书写

    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, 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.

  • 连字

    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 文本引擎 支持不同 书写系统 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 . 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 .

编码

编码与应用程序源文件和应用程序读取 (或写入) 的文本文件两者相关。

编码源代码

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.

编码文本输入/输出

使用 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 文档编制。

操作和窗口系统

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

  • 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.
  • 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, 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.
  • File I/O defaults to Latin1, with Unicode options in 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.
本地化应用程序 将 Qt 和 Qt Quick APP 本地化成多种语言。
Qt Linguist 范例 使用 Qt Linguist 国际化 Qt 应用程序
Qt Linguist 手册 Using Qt translation tools: lupdate, lrelease, and Qt Linguist
基于文本 ID 的翻译 Text ID based internationalization provides support for large scale projects with many target locales and many texts to translate
复数形式翻译规则 A summary of the translation rules for plural forms produced by Qt's translation tools.
编写翻译源代码 Writing source code that enables the localization of applications.