views:

425

answers:

3

Hi,

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?

+5  A: 

Wrap your literal in the QT_TR_NOOP macro:

// cpp file
const QString FooConstants::Foo = QT_TR_NOOP("foo");
Thomas
A: 
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));
Sam Dutton
+1  A: 

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.

rpg