QTranslator 类提供用于文本输出的国际化支持。 更多...
| 头: |
#include <QTranslator>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| 继承: | QObject |
| QTranslator (QObject * parent = nullptr) | |
| virtual | ~QTranslator () |
| QString | filePath () const |
| virtual bool | isEmpty () const |
| QString | 语言 () const |
| bool | load (const QString & filename , const QString & directory = QString(), const QString & search_delimiters = QString(), const QString & suffix = QString()) |
| bool | load (const QLocale & locale , const QString & filename , const QString & prefix = QString(), const QString & directory = QString(), const QString & suffix = QString()) |
| bool | load (const uchar * data , int len , const QString & directory = QString()) |
| virtual QString | translate (const char * context , const char * sourceText , const char * disambiguation = nullptr, int n = -1) const |
此类的对象包含一组从源语言到目标语言的翻译。QTranslator 提供在翻译文件中查找翻译的功能。翻译文件的创建是使用 Qt Linguist .
QTranslator 的最常见用法:加载翻译文件,并安装它使用 QCoreApplication::installTranslator ().
这里是范例
main()
函数在使用 QTranslator:
// Required for using the '_L1' string literal. using namespace Qt::StringLiterals; int main(int argc, char *argv[]) { QApplication app(argc, argv); QTranslator translator; // look up e.g. :/i18n/myapp_de.qm if (translator.load(QLocale(), "myapp"_L1, "_"_L1, ":/i18n"_L1)) QCoreApplication::installTranslator(&translator); QPushButton hello(QCoreApplication::translate("main", "Hello world!")); hello.resize(100, 30); hello.show(); return app.exec(); }
注意:翻译器必须被创建 before 应用程序的 Widget。
大多数应用程序从不需要对此类做任何其它事情。由此类提供的其它函数对工作于翻译文件的应用程序很有用。
它是可能的,查找翻译使用 translate () (作为 tr () 和 QCoreApplication::translate () 做的)。 translate () 函数最多接受 3 参数:
例如,对话框中的 Cancel 可能为 Anuluj (在此情况下,source text 将是 Cancel),当以波兰语运行程序时。context (通常) 是对话框类名;通常没有注释,翻译文本将是 Anuluj。
但并不总是那么简单。带 two-sided printing (双面打印) 和 binding (装订) 设置的西班牙语版打印机对话框,可能要求将 Enabled 翻译成 Activado 和 Activada 两者。在此情况下,2 案例中的 source text 将是 Enabled,而 context 将是对话框类名,但这 2 项将消除歧义,譬如一个是 two-sided printing,另一个是 binding。disambiguation (消除歧义) 使 translator (翻译器) 能够为西班牙语版选择适当语性,并使 Qt 能够区分翻译。
应用程序可以安装多个翻译文件。翻译搜索次序与其安装次序相反,因此,首先搜索最近安装的翻译文件,最后搜索最早安装的翻译文件。一旦找到包含匹配字符串的翻译,就停止搜索。
此机制使 selected 的特定翻译,或优先于其它翻译成为可能;只需从应用程序卸载 translator (翻译器),通过将它传递给 QCoreApplication::removeTranslator () 函数并重新安装它采用 QCoreApplication::installTranslator ()。那么,它将是搜索匹配字符串的第一翻译。
Only install translation files from trusted sources.
Translation files are binary files that are generated from text-based translation source files. The format of these binary files is strictly defined by Qt and any manipulation of the data in the binary file may crash the application when the file is loaded. Furthermore, even well-formed translation files may contain misleading or malicious translations.
另请参阅 QCoreApplication::installTranslator (), QCoreApplication::removeTranslator (), QObject::tr (), QCoreApplication::translate (), I18N 范例 , Hello tr() 范例 , Arrow Pad 范例 ,和 Troll Print 范例 .
[explicit]
QTranslator::
QTranslator
(
QObject
*
parent
= nullptr)
构造空的消息文件对象采用父级 parent 未连接到任何文件。
[virtual noexcept]
QTranslator::
~QTranslator
()
销毁对象并释放任何分配资源。
返回加载的翻译文件路径。
文件路径为空,若尚未加载翻译 (或加载失败,若未从文件加载翻译)。
[虚拟]
bool
QTranslator::
isEmpty
() const
返回
true
若此翻译器为空,否则返回
false
。此函数处理剥离和未剥离翻译文件。
返回存储在翻译文件中的目标语言。
加载
filename
+
suffix
(.qm 若
suffix
未指定),可能是绝对文件名或相对于
directory
。返回
true
若成功加载翻译;否则返回
false
.
若 directory 未指定,使用当前目录 (即 currentPath ()).
将丢弃此翻译器对象的先前内容。
若文件名不存在,按以下次序尝试其它文件名:
例如,以 fr_CA 区域设置 (讲法语的加拿大) 运行的应用程序可能调用 load("foo.fr_ca", "/opt/folib")。那么,Load() 将试着打开来自该列表的首个现有可读文件:
/opt/foolib/foo.fr_ca.qm
/opt/foolib/foo.fr_ca
/opt/foolib/foo.fr.qm
/opt/foolib/foo.fr
/opt/foolib/foo.qm
/opt/foolib/foo
通常,最好是使用 QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) 函数代替,因为它使用 QLocale::uiLanguages () 而不仅仅是区域设置名称,指的是日期和数字格式,而不必是 UI 语言。
加载
filename
+
prefix
+
ui language name
+
suffix
(.qm 若
suffix
未指定),可能是绝对文件名或相对于
directory
。返回
true
若成功加载翻译;否则返回
false
.
将丢弃此翻译器对象的先前内容。
若文件名不存在,按以下次序尝试其它文件名:
例如,应用程序运行在 locale 采用下列 UI 语言 -es、fr-CA、de 可以调用 load(QLocale(), "foo", ".", "/opt/foolib", ".qm")。load() 将替换 - (短划线) 采用 _ (下划线) 按 UI 语言,然后试着打开来自此列表的首个现有可读文件:
/opt/foolib/foo.es.qm
/opt/foolib/foo.es
/opt/foolib/foo.fr_CA.qm
/opt/foolib/foo.fr_CA
/opt/foolib/foo.fr.qm
/opt/foolib/foo.fr
/opt/foolib/foo.de.qm
/opt/foolib/foo.de
/opt/foolib/foo.qm
/opt/foolib/foo
.
/opt/foolib/foo
在文件系统区分大小写的操作系统, QTranslator 还会试着加载区域设置名称的小写版本。
此函数重载 load()。
加载 QM 文件数据 data 的长度 len 进翻译器。
不拷贝数据。调用者必须能够保证 data 不会被删除 (或修改)。
directory 仅用于指定基目录,当加载 QM 文件的依赖时。若文件没有依赖,忽略此自变量。
[虚拟]
QString
QTranslator::
translate
(const
char
*
context
, const
char
*
sourceText
, const
char
*
disambiguation
= nullptr,
int
n
= -1) const
返回翻译对于键 ( context , sourceText , disambiguation )。若未找到,还会尝试 ( context , sourceText , "")。若仍失败,返回 null 字符串。
注意: 不完整翻译可能导致意外行为:若没有翻译对于 ( context , sourceText , "") 的提供,在此情况下,方法可能实际返回翻译对于不同 disambiguation .
若 n 不是 -1,用于选择合适翻译形式 (如 "%n file found" vs. "%n files found")。
若需要以编程方式将翻译插入进 QTranslator ,可以重实现此函数。
另请参阅 load ().