PDA

View Full Version : qt problem with tr() in static member of a class



Krzysztow
16th August 2010, 14:32
0 down vote favorite


Hi all,

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

The header file is similar to this:


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")
};

And whatever I do (having declared array of *char[] with QT_TR_NOOP, then tr() it in initialization of the member) 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.
Chris.

franz
17th August 2010, 07:43
Use QCoreApplication::translate() (http://doc.trolltech.com/latest/qcoreapplication.html#translate) instead.

Krzysztow
17th August 2010, 09:58
Thank You for a reply. However it doesn't work for me.

I did as follows:



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

and in *.ts file I changed:


<context>
<name>mykey</name>
<message>
<source>something to be translated</source>
<translation>My Polish translation</translation>
</message>
</context>


I removed "type="unfinished"" from the <translation> tag. Then executed lrelease and I installed the translator with this application. It is installed for sure, since other texts (but those not in static member) are translated correctly.

If You have any other ideas, please share them. I tried a lot of things and now have no solution for this issue.

Thank You once more. All the best!

Krzysztow
17th August 2010, 10:13
On the other forum (StackOverflow (http://stackoverflow.com/questions/3493540/qt-tr-in-static-variable)) I got the answer from Caleb Huitt - cjhuitt which seems to be the solution/explanatioin to the problem. It goes as follows:

"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 (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13) idiom instead of a static member variable."

Thank You for Your time and hope this post also helps someone else.

GreenScape
17th August 2010, 10:18
//at global scope
const char *yourText = QT_TRANSLATE_NOOP("MyClass", "something to be translated");
...
info MyClass::myClassInfo={
1,
MyClass::tr(yourText);
};

hint: tr() uses className() to determine context therefore MyClass must have Q_OBJECT macro.
use lupdate and Qt Linguist instead manual editind of *.ts

Krzysztow
17th August 2010, 11:29
Hey. Thanks for it. But still, it doesn't work. There is no translated text appearing. If the code is:



const char *trText = QT_TRANSLATE_NOOP("MyClass", "something to be translated");
...
info MyClass::myClassInfo={//static vaiable initialization
1,
MyClass::tr(trText);
};


But if I do "myClass.problematicString=MyClass::tr(trText);" in the class constructor, all is fine. So probably this still concerns the problem mentioined on StackOverflow - strings are created before translator is run (even though it is in main function straight after QApplication definition).

Thanks,
Chris.