PDA

View Full Version : Using tr() in static consts and variables



adrianoleal
16th March 2011, 17:55
Hello guys, how are you?

I'm newbie in Qt, brazilian and i'm working with Qt just 4 months.

I have a doubt about if is possible use the tr() method with constants or variables.

For example: I'm doing that...

static const QString TitleSubgenreOther = tr("Outros"); //Meanings: 'others', but's in portuguese

ui->category_list->item(2)->setText(TitleSubgenreOther);

When i apply the english language with translate() method, those constants aren't translated. How can i do?

Thanks and sorry my english.

agarny
16th March 2011, 19:02
What about using the disambiguation parameter, i.e.
static const QString TitleSubgenreOther = QApplication::tr("Outros", "Global");

Lesiok
16th March 2011, 19:43
I think that the problem is because all static variables are initiated BEFORE QApplication instantiation.

agarny
16th March 2011, 20:13
I think that the problem is because all static variables are initiated BEFORE QApplication instantiation.Actually, even without static, it doesn't work. When I made m y suggestion above, I thought it could have worked since that added a new entry to the .ts file, but then it doesn't make any difference. Oh well...

SixDegrees
16th March 2011, 22:45
const values are typically built into the object file at compile time; their value cannot be changed at runtime, which is when translation takes place. Because translation is a dynamic, runtime operation, you can't use const values; declaring something to be const when it's value can change is a contradiction to begin with. You really don't want them to be const; you want them to be changeable.

conner686
16th March 2011, 23:19
const values are typically built into the object file at compile time; their value cannot be changed at runtime, which is when translation takes place. Because translation is a dynamic, runtime operation, you can't use const values; declaring something to be const when it's value can change is a contradiction to begin with. You really don't want them to be const; you want them to be changeable.
Whether or not the variable should be declared const is a design consideration that has no bearing on this problem as I understand it. It would take an *extremely* aggressive optimizer to get a const QString to be written directly into the object file; QString objects require several heap allocations, which by definition is not part of the object file. Simply removing the const will accomplish nothing as initialization still only occurs once.

In this case, the problem seems to be one of lifetime. He initializes the variable, then translates to English, but since the variable is already initialized it doesn't notice the change. Keep the "static const", but move the tr() to where the string is used, and I believe everything will work as expected. You also apparently need QT_TR_NOOP to mark the string for translation.



static const QString Portugese = qT_TR_NOOP("text");
...
const QString English = tr(Portugese);

stampede
17th March 2011, 00:03
I don't know if it helps you, but you can always solve it by declaring a helper class instead of string constants, something like:


class MyStrings{
public:
static QString TitleSubgenreOther(){
return QObject::tr("Outros" );
}
static QString AnotherStringConstant(){
return QObject::tr("Text to translate");
}
// ...
};

ui->category_list->item(2)->setText( MyStrings::TitleSubgenreOther() );