qt tr() 在静态变量中 [英] qt tr() in static variable

查看:75
本文介绍了qt tr() 在静态变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 qt 中的翻译有疑问.我的项目中的所有翻译都可以正常工作,但有一个位于类的静态变量中.对应部分代码如下

I have problem concerning translations in qt. All translations in my porject work fine, but one, which is in a static variable of a class. Corresponding part of code looks as follows

头文件类似这样:

typedef struct {
    int         type;
    QString     problematicString;
} info;

MyClass::QObject_Descendant
{
Q_OBJECT;

//some functions like constructor, destructor... etc.
....

static info myClassInfo;//class that makes problems

}

在实现文件中,我按如下方式初始化变量:

and in implementation file I initialize the variable as follows:

info MyClass::myClassInfo={
    1,
    tr("something to be translated")
};

无论我做什么(尝试使用 QT_TR_NOOP,然后是 tr() 和其他),我都无法翻译 myClassInfo.problematicString.最奇怪的是文字something to be translate"出现在 *.ts 文件中.

And whatever I do (trying with QT_TR_NOOP, then tr() and others) I cannot get myClassInfo.problematicString translated. The weirdest thing is that the text "something to be translated" appears in *.ts file.

如果有人有任何提示,请与我分享.提前致谢.

If someone has any hints, please share them with me. Thanks in advance.

克里斯.

推荐答案

在运行 int main 函数之前实例化静态变量(从而运行构造函数代码).翻译代码是在 QApplication 构造函数中设置的(我相信),在输入 int main 函数之前它不会运行.因此,您试图在支持字符串的代码初始化之前获得字符串的翻译.

Static variables are instantiated (and thus, constructor code run) before your int main function is run. The translation code is set up in the QApplication constructor (I believe), which isn't run until your int main function has been entered. Thus, you are trying to get the translation of a string before the code to support it has been initialized.

为避免这种情况,您可以接受给定的字符串未翻译并在每次使用时显式翻译它,或者使用 在首次使用时构造而不是静态成员变量.

To avoid this, you could either accept that the given string isn't translated and explicitly translate it every time it is used, or use the Construct on First Use idiom instead of a static member variable.

这篇关于qt tr() 在静态变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆