PDA

View Full Version : tr() and static fields



drkbkr
4th June 2007, 20:47
Hi,

I want to have all the translated strings in one place in my source code, so I've created a Strings class that extends QObject that consists of static QString objects.

The header file contains declarations like:


const static QString PENDING;

and then a .cpp file contains


const QString Strings::PENDING(tr("Pending"));

I can run lupdate on the .cpp file, open the resulting .ts file in linguist and see that I need to translate "Pending". I do that, save everything, and run lrelease.

Then in the main method of my app, before creating widgets I run:


QString file ("/usr/local/swdevel/share/dbaker/lib/instr_fr");
bool loaded = translator->load(file);
TRACE("Loaded: %s\n", loaded ? "true" : "false");
app.installTranslator(translator);

but when I try to display or just print out the value of the variable, I only get the English.

Is there some problem with using tr in static members of a class?

Thanks in advance.

Derek

wysota
4th June 2007, 23:53
Can you provide a compilable example?

jacek
5th June 2007, 01:50
but when I try to display or just print out the value of the variable, I only get the English.

Is there some problem with using tr in static members of a class?
The problem is that you do the translation when the variable is initialized, what happens before you install the translator.

What you could try is:

static const char PENDING[] = QT_TR_NOOP( "..." );
...
label->setText( tr( PENDING ) );

drkbkr
5th June 2007, 16:13
I'm attaching an example that shows a couple different tries at having translatable text in a class of static variables. The only text I can get to show up translated is the "maybe" string where the literal text to translate is right in the setText() call.

The one I'd really like to get to work is the "No" string where the QString and tr call are in the strings class.

Any help is greatly appreciated.

Thanks.
Derek

jacek
5th June 2007, 16:49
The one I'd really like to get to work is the "No" string where the QString and tr call are in the strings class.

Would such class (or namespace) be acceptable to you?

class strings
{
public:
enum StringConstant
{
Yes = 0,
No = 1,
AndSoOn = 2
};

static QString get( StringConstant str );
...
};
...
label->setText( strings::get( strings::No ) );

jacek
5th June 2007, 16:54
Another possibility is:
class TranslatedString
{
public:
TranslatedString( const * const str ) : _str( str ) {}
operator QString() const { return QObject::tr( _str ); }

private:
const * const _str;
};
...
TranslatedString strings::No = QT_TR_NOOP( "No" );
...
label->setText( strings::No );

patrik08
5th June 2007, 17:01
So you can make it..




#define _AbortAction_ \
QCoreApplication::translate("coniglioe","comment",QApplication::UnicodeUTF8)
#define _NextAbortAction_ \
QCoreApplication::translate("conigliob","comment",QApplication::UnicodeUTF8)
#define _PrevAction_ \
QCoreApplication::translate("coniglioc","comment",QApplication::UnicodeUTF8)