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_TRANSLATE_NOOP inside static const QMap?
Forum Update on Monday, May 27th 2025

QT_TRANSLATE_NOOP inside static const QMap?

Scheduled Pinned Locked Moved Solved General and Desktop
translateinternationali18ntranslator
11 Posts 2 Posters 5.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
    mrjj
    Lifetime Qt Champion
    wrote on 28 Dec 2016, 17:31 last edited by mrjj
    #2

    Hi
    Yes this marks them for extraction by the lupdate tool.

    You need a call to tr() to translate it.

    QString FriendlyConversation::greeting(int type)
    {
        static const char *greeting_strings[] = {
            QT_TR_NOOP("Hello"),
            QT_TR_NOOP("Goodbye")
        };
        return tr(greeting_strings[type]); <<<<<<< returns via tr()
    }
    

    http://doc.qt.io/qt-5/i18n-source-translation.html

    1 Reply Last reply
    2
    • O Offline
      O Offline
      Opa114
      wrote on 28 Dec 2016, 18:01 last edited by
      #3

      yes i tried to print it with (for my example above)

      for (int i = 0; flowers[i]; ++i)
              qDebug() << tr(flowers[i]);
      

      but i don't get the translated content printed out.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 28 Dec 2016, 18:06 last edited by mrjj
        #4

        How do you load the translation ?
        https://wiki.qt.io/How_to_create_a_multi_language_application

        O 1 Reply Last reply 28 Dec 2016, 18:21
        1
        • M mrjj
          28 Dec 2016, 18:06

          How do you load the translation ?
          https://wiki.qt.io/How_to_create_a_multi_language_application

          O Offline
          O Offline
          Opa114
          wrote on 28 Dec 2016, 18:21 last edited by Opa114
          #5

          @mrjj said in QT_TRANSLATE_NOOP inside static const QMap?:

          How do you load the translation ?
          https://wiki.qt.io/How_to_create_a_multi_language_application

          i'm exactly loading the translation this way. It work's very well with my other translations, for example the Menu-Entries or other QLabel on which i directly call tr(VALUE). But not the one i posted. Could it be problemativ because i declare and initialize it outside the class?
          But i found no other way to declare and initialize a static const variable.

          I did some research and found ouut that:
          When i call qDebug() << qApp->translate( "OrderForm2", flowers[0] );
          instead of qDebug() << tr(flowers[0]); i got the translated content.

          But i did it not work without calling qApp->translate() ?

          M 1 Reply Last reply 28 Dec 2016, 21:14
          0
          • O Opa114
            28 Dec 2016, 18:21

            @mrjj said in QT_TRANSLATE_NOOP inside static const QMap?:

            How do you load the translation ?
            https://wiki.qt.io/How_to_create_a_multi_language_application

            i'm exactly loading the translation this way. It work's very well with my other translations, for example the Menu-Entries or other QLabel on which i directly call tr(VALUE). But not the one i posted. Could it be problemativ because i declare and initialize it outside the class?
            But i found no other way to declare and initialize a static const variable.

            I did some research and found ouut that:
            When i call qDebug() << qApp->translate( "OrderForm2", flowers[0] );
            instead of qDebug() << tr(flowers[0]); i got the translated content.

            But i did it not work without calling qApp->translate() ?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 28 Dec 2016, 21:14 last edited by
            #6

            @Opa114
            Well you are using it with a context ("OrderForm2")
            and the sample for that actually use app trans

            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 qApp->translate("FriendlyConversation", <<<< context
                       greeting_strings[type]);
            }
            

            So it could be related to that. ( context)

            1 Reply Last reply
            2
            • O Offline
              O Offline
              Opa114
              wrote on 28 Dec 2016, 23:16 last edited by
              #7

              ok it seems so. I tried your other example from the official Qt-Source. But when i declare the greetings_Strings[]-Array outside the class the Strings will not be detectet for translation. I think these are only wokring inside a Class or Method :/

              M 1 Reply Last reply 29 Dec 2016, 07:18
              0
              • O Opa114
                28 Dec 2016, 23:16

                ok it seems so. I tried your other example from the official Qt-Source. But when i declare the greetings_Strings[]-Array outside the class the Strings will not be detectet for translation. I think these are only wokring inside a Class or Method :/

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 29 Dec 2016, 07:18 last edited by
                #8

                @Opa114
                Oh, are you saying the greeting_strings sample do not work?
                It is not in a class. Mind the context thing. It does matter. :)

                O 1 Reply Last reply 29 Dec 2016, 11:09
                0
                • M mrjj
                  29 Dec 2016, 07:18

                  @Opa114
                  Oh, are you saying the greeting_strings sample do not work?
                  It is not in a class. Mind the context thing. It does matter. :)

                  O Offline
                  O Offline
                  Opa114
                  wrote on 29 Dec 2016, 11:09 last edited by
                  #9

                  @mrjj yes. outside a class it is only working with the context stuff and QT_TRANSLATE_NOOP. QT_TR_NOOP only works inside class. So thanks for helping out.

                  M 1 Reply Last reply 29 Dec 2016, 11:20
                  1
                  • O Opa114
                    29 Dec 2016, 11:09

                    @mrjj yes. outside a class it is only working with the context stuff and QT_TRANSLATE_NOOP. QT_TR_NOOP only works inside class. So thanks for helping out.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 29 Dec 2016, 11:20 last edited by
                    #10

                    @Opa114
                    Well when not in an object, the context is different, so
                    I guess that's why to specify one, makes it work.
                    Well just use what works, it all ends up being the same type
                    of lookup in a table so TR or qApp->translate should not matter much.

                    O 1 Reply Last reply 29 Dec 2016, 13:09
                    0
                    • M mrjj
                      29 Dec 2016, 11:20

                      @Opa114
                      Well when not in an object, the context is different, so
                      I guess that's why to specify one, makes it work.
                      Well just use what works, it all ends up being the same type
                      of lookup in a table so TR or qApp->translate should not matter much.

                      O Offline
                      O Offline
                      Opa114
                      wrote on 29 Dec 2016, 13:09 last edited by
                      #11

                      @mrjj okay - thanks for help!

                      1 Reply Last reply
                      1

                      11/11

                      29 Dec 2016, 13:09

                      • Login

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