







使用QT_TR_NOOP宏,看例子
QString getText(OperationStateEnum state) { static std::map<OperationStateEnum, const char*> data = { {OperationStateEnum::UNKNOW, QT_TR_NOOP("未知")}, {OperationStateEnum::NOSAVE, QT_TR_NOOP("未保存")}, {OperationStateEnum::SAVED, QT_TR_NOOP("已保存")} }; auto iter = data.find(state); if ( iter != data.end()) { return tr(iter->second); } return ""; }
QT_TR_NOOP,NOOP是 No Operation 的缩写,意为 “无操作”,原型如下
#define QT_TR_NOOP(sourceText) (sourceText)
展开后就是原样返回输入的字符串,运行时不做任何操作(不翻译、不转换、不分配内存)。它存在的唯一目的就是标记:告诉 lupdate 工具(Qt 的翻译提取工具)这个字符串需要被提取到翻译文件(.ts)中。有些字符串需要存储在静态数据结构中(如 static const char* 数组、std::map),为了提高效率等原因,因此不能直接调用 tr()(否则会在程序启动时固定翻译,无法动态切换语言)。QT_TR_NOOP 让你先标记这些字符串,等到真正使用时再调用 tr() 来翻译,从而既支持 lupdate 提取,又支持运行时动态切换语言。
官方说明:
QT_TR_NOOP(sourceText)
Marks the UTF-8 encoded string literal sourceText for delayed translation in the current context (class).
The macro tells lupdate to collect the string, and expands to sourceText itself.
Example:
QString FriendlyConversation::greeting(int type)
{
static const char *greeting_strings[] = {
QT_TR_NOOP("Hello"),
QT_TR_NOOP("Goodbye")
};
return tr(greeting_strings[type]);
}
The macro QT_TR_NOOP_UTF8() is identical and obsolete; this applies to all other _UTF8 macros as well.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。