Reputation: 1799
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?
Upvotes: 5
Views: 7953
Reputation: 181745
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).
Upvotes: 12
Reputation: 7777
As Thomas mentioned, you have to use a macro.
The reason is that Qt doesn't know which strings to translate by default, it scans the files and looks for a set of patterns. One of them is tr("text")
, but if you want to use a constant, you will have to mark it explicitly with QT_TRANSLATE_NOOP
or QT_TR_NOOP
when it's defined.
Upvotes: 2
Reputation: 15269
editMenu->addAction(tr(FooConstants::Foo));
I think your problem is that tr takes a char* argument, not a QString:
QString QObject::tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )
You could change the type of FooConstants::Foo, or convert it to a char* when you create the menu action, for example:
const QByteArray byteArray = FooConstants::Foo.toLatin1();
char *data = byteArray.data();
editMenu->addAction(tr(data));
Upvotes: 1