Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT_TR_NOOP gives "Cannot be called without context"

QT_TR_NOOP gives "Cannot be called without context"

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #1

    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?

    E 1 Reply Last reply
    0
    • mrjjM mrjj

      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?

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @mrjj Are the strings inside a class?

      mrjjM 1 Reply Last reply
      0
      • E Eeli K

        @mrjj Are the strings inside a class?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

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

        E 1 Reply Last reply
        0
        • mrjjM mrjj

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

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

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

          mrjjM 1 Reply Last reply
          3
          • E Eeli K

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            E 1 Reply Last reply
            0
            • mrjjM mrjj

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

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved