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. Translate with tr() string defined with #define

Translate with tr() string defined with #define

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 4.9k Views
  • 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.
  • M Offline
    M Offline
    Mwoua
    wrote on 12 Jul 2017, 16:02 last edited by
    #1

    Hello,
    I have a string defined with #define str, and I need to translate it, but not everywhere.

    The #define is in a separate file LtDefs.h :

    #define LT_FAMILY_NAME "Lt\xE2\x84\xA2" //TM utf8 code
    

    and in certains classes, I need to translate it. I've tried to use

    tr(LT_FAMILY_NAME ) but it doesnt add the string to translation list.
    

    The class has the Q_OBJECT macro, but not the LtDefs.h

    Is there a way to translate a string defined with #define ? I've seen the QT_TR_NOOP macro, but I dont really understand it

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 12 Jul 2017, 16:37 last edited by mrjj 7 Dec 2017, 16:40
      #2

      @Mwoua said in Translate with tr() string defined with #define:

      QT_TR_NOOP

      Hi
      As far as I remember it would NOT pick up defines for translation.
      It seems to be looking for tr("") and no other form except ""
      Make sense that lupdate cannot see variables / defines as its not via the compiler.

      one way is to use a function

      
      QString FriendlyConversation::greeting(int type)
      {
          static const char *greeting_strings[] = {
              QT_TR_NOOP("Hello"),
              QT_TR_NOOP("Goodbye")
          };
          return tr(greeting_strings[type]);
      }
      
      
      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mwoua
        wrote on 12 Jul 2017, 17:42 last edited by
        #3

        My problem is that the #define is in a header included in several place in the code, so I can't replace it by a function easily

        M 1 Reply Last reply 12 Jul 2017, 18:05
        0
        • M Mwoua
          12 Jul 2017, 17:42

          My problem is that the #define is in a header included in several place in the code, so I can't replace it by a function easily

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 12 Jul 2017, 18:05 last edited by mrjj 7 Dec 2017, 18:21
          #4

          @Mwoua
          Well there is no way to get it to grab the #define.

          Also, it will not get translated anyway, as the tr() macro is key to make it work.

          So even if you add it manually to the translation file.
          It will not work when you load a new language.

          It does not have to be a function.
          This also works

           const char* LT_FAMILY_NAME [] = {
            QT_TRANSLATE_NOOP_UTF8("global","Lt\xE2\x84\xA2")
          };
          

          qDebug() << LT_FAMILY_NAME;

          Should work instead of the #define with no altering code.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mwoua
            wrote on 12 Jul 2017, 18:28 last edited by
            #5

            My header is included in several place, so it gives me a variable redefinition. I dont think there is an easy way to get this to work, so I've an ugly workaround

            M 1 Reply Last reply 12 Jul 2017, 18:30
            0
            • M Mwoua
              12 Jul 2017, 18:28

              My header is included in several place, so it gives me a variable redefinition. I dont think there is an easy way to get this to work, so I've an ugly workaround

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 12 Jul 2017, 18:30 last edited by mrjj 7 Dec 2017, 18:32
              #6

              @Mwoua

              • a variable redefinition.

              You can fix that with extern.

              in .h

              extern const char* LT_FAMILY_NAME[] ;

              and in .cpp (any)
              const char* LT_FAMILY_NAME [] = {
              QT_TRANSLATE_NOOP_UTF8("global","Lt\xE2\x84\xA2")
              };

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Mwoua
                wrote on 12 Jul 2017, 20:52 last edited by Mwoua 7 Dec 2017, 20:53
                #7

                Thats a good idea, I'll try it when I figure out how to write MC (a part of what im trying to translate) in superscript in title bar

                1 Reply Last reply
                0

                4/7

                12 Jul 2017, 18:05

                • Login

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