QT_TR_NOOP gives "Cannot be called without context"


  • Qt Champions 2017

    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 context

    So 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?



  • @mrjj Are the strings inside a class?


  • Qt Champions 2017

    @Eeli-K
    No, they are in global scope

    Seems 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 :)



  • @mrjj So maybe they have to be inside a class which becomes a "context"? A QObject?


  • Qt Champions 2017

    @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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.