views:

66

answers:

1

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 (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.

Chris.

A: 

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.

Caleb Huitt - cjhuitt
Thank You very much and I appreciate the link to appropriate reference. Yes, I have to change my source then. All the best,Chris.