Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt tr does not seem to work on static constant members?

I'm working on translating our Qt gui at the moment.

I have the following code:

// header file
static const QString Foo;

// cpp file
const QString FooConstants::Foo = "foo"; 

// another cpp file
editMenu->addAction(tr(FooConstants::Foo));

This doesn't seem to work though.

That is, there is no entry in the .ts file for the above constant.

If I do this then it works:

// another cpp file
editMenu->addAction(tr("foo"));

However, this constant is used in many places, and I don't want to have to manually update each string literal. (if it were to change in the future)

Can anyone help?

like image 485
Steven Keith Avatar asked Dec 17 '22 05:12

Steven Keith


1 Answers

Wrap your literal in the QT_TR_NOOP macro:

// cpp file
const QString FooConstants::Foo = QT_TR_NOOP("foo");

From the guide:

If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). They merely mark the text for extraction by the lupdate tool. The macros expand to just the text (without the context).

like image 168
Thomas Avatar answered Dec 19 '22 18:12

Thomas