<QtTranslation> 代理页面
函数
QString | qtTrId(const char *id, int n = -1) |
宏
QT_TRANSLATE_NOOP3(context, sourceText, disambiguation) | |
QT_TRANSLATE_NOOP(context, sourceText) | |
QT_TRANSLATE_N_NOOP3(context, sourceText, comment) | |
QT_TRANSLATE_N_NOOP(context, sourceText) | |
QT_TRID_NOOP(id) | |
(since 6.3) | QT_TRID_N_NOOP(id) |
QT_TR_NOOP(sourceText) | |
QT_TR_N_NOOP(sourceText) |
函数文档
QString qtTrId(const char *id, int n = -1)
qtTrId 函数查找并返回翻译后的字符串。
返回由id 标识的翻译字符串。如果找不到匹配字符串,则返回 id 本身。正常情况下不会出现这种情况。
如果n >= 0,那么结果字符串中所有出现的%n
都将被替换为n 的十进制表示。此外,根据n 的值,翻译文本可能会有所不同。
元数据和注释可以按照QObject::tr() 的记录传递。此外,还可以提供类似的源代码字符串模板:
//% <C string>
或
\begincomment% <C string> \endcomment
示例:
//% "%n fooish bar(s) found.\n" //% "Do you want to continue?" QString text = qtTrId("qtn_foo_bar", n);
创建适合使用此函数的 QM 文件需要向lrelease
工具传递-idbased
选项。
警告: 只有在调用此方法之前安装了所有翻译器时,此方法才是可重入的。不支持在执行翻译时安装或删除翻译器。这样做可能会导致崩溃或其他不良行为。
注:此函数为可重入函数。
另请参阅 QObject::tr(),QCoreApplication::translate() 和Qt 的国际化。
宏文档
QT_TRANSLATE_NOOP3(context, sourceText, disambiguation)
在给定的context 中用给定的disambiguation 标记UTF-8 编码的字符串字面sourceText ,以便延迟翻译。context 通常是一个类,也需要指定为字符串字面量。字符串字面disambiguation 应该是一个简短的语义标记,以区分其他相同的字符串。
宏会告诉 lupdate 收集字符串,并展开为一个匿名结构,其中包含了作为sourceText 和disambiguation 传递的两个字符串字面量。
示例
static { const char *source; const char *comment; } greeting_strings[] = { QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello", "A really friendly hello"), QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye", "A really friendly goodbye") }; QString FriendlyConversation::greeting(int type) { return tr(greeting_strings[type].source, greeting_strings[type].comment); } QString global_greeting(int type) { return qApp->translate("FriendlyConversation", greeting_strings[type].source, greeting_strings[type].comment); }
另请参阅 QT_TR_NOOP(),QT_TRANSLATE_NOOP() 和Qt XML 国际化。
QT_TRANSLATE_NOOP(context, sourceText)
在给定的context 中标记 UTF-8 编码的字符串字面sourceText 以进行延迟翻译。context 通常是一个类名,也需要指定为字符串文字。
该宏会告诉 lupdate 收集该字符串,并扩展到sourceText 本身。
示例
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]); }
另请参阅 QT_TR_NOOP(),QT_TRANSLATE_NOOP3() 和Qt XML 国际化。
QT_TRANSLATE_N_NOOP3(context, sourceText, comment)
用给定的comment 在给定的context 中标记UTF-8 编码的字符串字面sourceText ,以便进行与分母相关的延迟翻译。context 通常是一个类,也需要指定为字符串字面量。字符串字面comment 应该是一个简短的语义标记,以区分其他相同的字符串。
宏会告诉 lupdate 收集字符串,并展开为一个匿名结构,其中包含了作为sourceText 和comment 传递的两个字符串字面量。
示例
static { const char * const source; const char * const comment; } status_strings[] = { QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)", "A login message status"), QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)", "A new message query status") }; QString FriendlyConversation::greeting(int type, int count) { return tr(status_strings[type].source, status_strings[type].comment, count); } QString global_greeting(int type, int count) { return qApp->translate("Message Status", status_strings[type].source, status_strings[type].comment, count); }
另请参阅 QT_TR_NOOP(),QT_TRANSLATE_NOOP(),QT_TRANSLATE_NOOP3() 和Qt XML 国际化。
QT_TRANSLATE_N_NOOP(context, sourceText)
在给定的context 中标记UTF-8 编码的字符串字面sourceText ,以便进行与分母相关的延迟翻译。context 通常是一个类名,也需要指定为字符串字面量。
该宏告诉 lupdate 收集该字符串,并扩展到sourceText 本身。
示例
static const char * const greeting_strings[] = { QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"), QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)") }; QString global_greeting(int type, int msgcnt) { return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt); }
另请参阅 QT_TRANSLATE_NOOP(),QT_TRANSLATE_N_NOOP3() 和Qt XML 国际化。
QT_TRID_NOOP(id)
QT_TRID_NOOP 宏标记了一个用于动态翻译的 ID。
该宏的唯一目的是为附加元数据(如qtTrId() )提供一个锚点。
该宏扩展为id 。
示例:
static const char * const ids[] = { //% "This is the first text." QT_TRID_NOOP("qtn_1st_text"), //% "This is the second text." QT_TRID_NOOP("qtn_2nd_text"), 0 }; void TheClass::addLabels() { for (int i = 0; ids[i]; ++i) new QLabel(qtTrId(ids[i]), this); }
[since 6.3]
QT_TRID_N_NOOP(id)
QT_TRID_N_NOOP 宏标记了一个与分子相关的动态翻译 id。
该宏的唯一目的是为附加元数据(如qtTrId() )提供一个锚点。
该宏扩展为id 。
举例说明:
static const char * const ids[] = { //% "%n foo(s) found." QT_TRID_N_NOOP("qtn_foo"), //% "%n bar(s) found." QT_TRID_N_NOOP("qtn_bar"), 0 }; QString result(int type, int n) { return qtTrId(ids[type], n); }
此宏在 Qt 6.3 中引入。
QT_TR_NOOP(sourceText)
标记 UTF-8 编码字符串sourceText ,以便在当前上下文(类)中进行延迟翻译。
该宏告诉 lupdate 收集该字符串,并扩展到sourceText 。
举例说明:
QString FriendlyConversation::greeting(int type) { static const char *greeting_strings[] = { QT_TR_NOOP("Hello"), QT_TR_NOOP("Goodbye") }; return tr(greeting_strings[type]); }
宏 QT_TR_NOOP_UTF8() 与之相同,但已过时;这也适用于所有其他 _UTF8 宏。
另请参阅 QT_TRANSLATE_NOOP() 和Qt 的国际化。
QT_TR_N_NOOP(sourceText)
标记 UTF-8 编码字符串字面sourceText ,以便在当前上下文(类)中进行取决于分子的延迟翻译。
该宏指示 lupdate 收集字符串,并扩展到sourceText 本身。
该宏展开为sourceText 。
示例:
static const char * const StatusClass::status_strings[] = { QT_TR_N_NOOP("There are %n new message(s)"), QT_TR_N_NOOP("There are %n total message(s)") }; QString StatusClass::status(int type, int count) { return tr(status_strings[type], nullptr, count); }
另请参阅 QT_TR_NOOP 和Qt 的国际化。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.