<QtTranslation>
标记待翻译的字符串。更多...
| Header: | #include <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 标识的已翻译字符串。如果未找到匹配的字符串,则返回该标识符本身。在正常情况下不应发生这种情况。
如果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 XML 中国际化。
宏文档
QT_TRANSLATE_NOOP3(context, sourceText, disambiguation)
将 UTF-8 编码的字符串字面量sourceText 标记为延迟翻译,并在给定的context 中使用给定的disambiguation 。context 通常是一个类,也需作为字符串字面量指定。字符串字面量disambiguation 应为一个简短的语义标签,用于区分其他方面完全相同的字符串。
该宏会指示 lupdate 收集该字符串,并展开为一个匿名结构体,其中包含作为 `sourceText ` 和 `disambiguation` 传递的两个字符串字面量。
示例:
static struct { 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)
将 UTF-8 编码的字符串字面量sourceText 标记为在给定的context 中进行延迟翻译。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)
将 UTF-8 编码的字符串字面量sourceText 标记为在给定的context 中进行分子依赖的延迟翻译,并使用给定的comment 。context 通常是一个类,也需要作为字符串字面量指定。字符串字面量comment 应为一个简短的语义标签,用于区分其他方面完全相同的字符串。
该宏会指示 lupdate 收集该字符串,并展开为一个匿名结构体,其中包含作为sourceText 和comment 传递的两个字符串字面量。
示例:
static struct { 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)
将 UTF-8 编码的字符串字面量sourceText 标记为在给定的context 中进行分子依赖的延迟翻译。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 宏用于标记一个用于动态翻译的标识符。
该宏的唯一目的是提供一个锚点,用于附加元数据,例如调用 `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);
}另请参阅 qtTrId() 以及Qt XML 中国际化。
[since 6.3] QT_TRID_N_NOOP(id)
宏 QT_TRID_N_NOOP 用于标记一个用于分子依赖型动态翻译的标识符。
该宏的唯一目的是提供一个锚点,用于附加诸如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 。
示例:
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 XML 中国际化。
© 2026 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.