QT_TR_NOOP gives "Cannot be called without context"
-
Hi
Trying to make it translate/extract non UI texts. ( plain c++ so to speak)The docs says (http://doc.qt.io/qt-5/i18n-source-translation.html)
"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. "
( in mainwindow.cpp )
(ignore the static. was just test)static std::string texts[] = {
{std::string(QT_TR_NOOP("text 1"))}, //: ID must be last
{std::string(QT_TR_NOOP("text 2"))},//= <666>
{std::string(QT_TR_NOOP("text 3"))}
};So it all sounds good but , running lupdate gives
tr() cannot be called without contextSo doc is wrong ?
It MUST be in a function as Docs shows ( and dont mention)Or why is it not extracting the texts and create the .ts file?
-
@Eeli-K
No, they are in global scopeSeems to like it if i say
static std::string texts[] = {
{std::string(QT_TRANSLATE_NOOP("global","text 1"))}, //: ID must be last
{std::string(QT_TRANSLATE_NOOP("global","text 2"))},//= <666>
{std::string(QT_TRANSLATE_NOOP("global","text 3"))}
};but still no ts file :)
-
@Eeli-K
Yes it seems it wants a context.
So even docs sounds like you can use QT_TR_NOOP, for global scope you seems to need a context too.I can get a ts file with QT_TRANSLATE_NOOP
so I'm going to go with that and a custom macro for the
few globals text we must have.So they don't as such need a class, but a context must be defined it seems.
Maybe I just didn't reads docs good enough :)
Thanks for your input.
-
@mrjj I just looked into http://doc.qt.io/qt-5/i18n-source-translation.html and it really sounds like you can use it as you did, even with an example:
"The macros expand to just the text (without the context).
Example of QT_TR_NOOP():
QString FriendlyConversation::greeting(int type) { static const char *greeting_strings[] = { QT_TR_NOOP("Hello"), QT_TR_NOOP("Goodbye") }; return tr(greeting_strings[type]); }
Example of QT_TRANSLATE_NOOP():
static const char *greeting_strings[] = { QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"), QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye") }; QString FriendlyConversation::greeting(int type) { return tr(greeting_strings[type]); } QString global_greeting(int type) { return QCoreApplication::translate("FriendlyConversation", greeting_strings[type]); }
"
I assume you have the file in your .pro file, as stated in http://doc.qt.io/qt-5/linguist-programmers.html?