Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
tommy20
Posts: 253
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10
Accepted Solution

QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

[ Edited ]

Hi,

 

Because I want to declare all translatable texts in one place, I define a seperate class for this. The .h file looks like below:

#ifndef TEXTCONSTANTS_H_
#define TEXTCONSTANTS_H_

#include "qobject.h"

class TextConstants : public QObject {
	Q_OBJECT

public:
	static const char *SWITCH_APP;
};

#endif /* TEXTCONSTANTS_H_ */

 And the .cpp file looks like below:

#include "TextConstants.h"

const char *TextConstants::SWITCH_APP = QT_TR_NOOP("Switch App");

Note that, in the file above, I use QT_TR_NOOP() macro to mark the given string for extraction to the .ts file. This is mentioned in the documentation at https://developer.blackberry.com/native/documentation/cascades/device_platform/internationalization/....

 

I then call tr(TextConstants::smileyfrustrated:WITCH_APP) in another subclass of QObject. The problem is that the "Switch App" text is not extracted to the .ts file, why? If I call tr() and directly pass "Switch App" text like tr("Switch App"), then this text is extracted to the .ts file. Am I missing some things to make it work? Thanks.

 

PS. I am using QNX® Momentics® IDE for BlackBerry® 10 Native SDK, Version: 10.1.0, Build id: v201303191709

 

--tom
Please use plain text.
BlackBerry Development Advisor
RSperanza
Posts: 141
Registered: ‎03-08-2012
My Device: Z10
My Carrier: Bell

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

Hi.

 

I checked the documentation and it states you should use that macro outside of a QObject.  You seem to be using it inside a QObject so I think you should just use the tr() call directly in your cpp file to extract the string to the translation file. 

 

Try that and reply back with the result.  Thanks.

Please use plain text.
Developer
tommy20
Posts: 253
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

No, please look at my code, I use QT_TR_NOOP() macro outside of a QObject. It is a static constant of a class:

const char *TextConstants::smileyfrustrated:WITCH_APP = QT_TR_NOOP("Switch App");
--tom
Please use plain text.
BlackBerry Development Advisor
RSperanza
Posts: 141
Registered: ‎03-08-2012
My Device: Z10
My Carrier: Bell

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

It is a constant for the TextConstants class, which you tagged as a QOBJECT.  Therefore, it is a QObject class in C++. 

 

There is no harm in rewrapping the string in tr() instead of the macro and seeing if it works.  If not, we'll try something else.  Thanks.

Please use plain text.
Developer
tommy20
Posts: 253
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

1. The document says that "When you need to translate text outside of functions or QObjects". I am using the text outside of functions, therefore, I need to use the QT_TR_NOOP() macro.

2. I have to tag the TextConstants class as Q_OBJECT because without this the QT_TR_NOOP() macro gives error.

3. "There is no harm in rewrapping the string in tr() instead of the macro and seeing if it works."
>>Yes, I have tried this, and it works (I state this in my first post) but I don't want to place text everywhere in my .cpp files, I want them to be in one place for easier code maintenance.

Thanks very much for following this.
--tom
Please use plain text.
BlackBerry Development Advisor
RSperanza
Posts: 141
Registered: ‎03-08-2012
My Device: Z10
My Carrier: Bell

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

Hi.

 

I think you misunderstood my suggestion.  I was only suggesting that you call the tr() function in your constant declaration ithat you posted nstead of QT_TR_NOOP(), not in other parts of your code, to see if the compiler accepts it and the string gets extracted properly.

 

If that doesn't work, I will try to track down some working code for comparison.

 

Sorry if I wasn't clear before.

Please use plain text.
Developer
tommy20
Posts: 253
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

Ah, yes, I misunderstood your suggestion. As per your suggestion, I changed the code to call the tr() function in the constant declaration instead of QT_TR_NOOP(). With this, the compiler accepts the tr() call but the string is still not extracted into the .ts file.

 

And the TextConstants.hpp file now looks like below:

class TextConstants : public QObject {
	Q_OBJECT
public:
	static const QString SWITCH_APP;
};

And the TextConstants.cpp file now looks like this: 

#include "TextConstants.h"

const QString TextConstants::SWITCH_APP = tr("Switch App");

 

 

 

 

--tom
Please use plain text.
BlackBerry Development Advisor
RSperanza
Posts: 141
Registered: ‎03-08-2012
My Device: Z10
My Carrier: Bell

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

Sorry for the delay.

 

This seems to work for me with the latest SDK:

 

const QString TextConstants::smileyfrustrated:WITCH_APP = QT_TRANSLATE_NOOP( "TextConstants", "Switch App");

The compiler complained about not providing a context with the tr() call using the latest Momentics. 

 

Try it out and let me know if it works for you as well.

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: QT_TR_NOOP() macro does not mark the string for extraction to the .ts file

Make sure that the file path is in the lupdate inclusions part of the .pro file too.
Like all of my posts
Please use plain text.