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. Translation from an header file
Forum Updated to NodeBB v4.3 + New Features

Translation from an header file

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.3k Views 3 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.
  • O Offline
    O Offline
    Oreonan
    wrote on last edited by
    #2

    @Oreonan said in Translation from an header file:

    QString nick = ttSettings->value(SETTINGS_GENERAL_NICKNAME, tr(SETTINGS_GENERAL_NICKNAME_DEFAULT)).toString();
    I forgot to precise that using tr() function isn't helpfull, I just tried this on line above but "noname" isn't present in .ts files

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi
      it will not see #defines sadly

      https://doc.qt.io/qt-5/i18n-source-translation.html
      Section "Using QT_TR_NOOP() and QT_TRANSLATE_NOOP() in C++"

      so maybe using QT_TR_NOOP on the define can work but not sure.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Oreonan
        wrote on last edited by
        #4

        Hi and thanks for your answer,
        unfortunately it doesn't work. I've no error when building, but "noname" it's still not appearing in .ts file

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi,

          If I may, using translated keys for settings is usually not a good idea. If your user changes its locale for some reason, they're going to lose the settings.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • O Offline
            O Offline
            Oreonan
            wrote on last edited by
            #6

            This defines are mainly use for display. For example, when an user set his nickname to a blank line, we display the default nickname for other users.
            So we have to translate "noname" in user language

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #7

              The problem is most likely between your source code and how the tool for extracting translations for your .ts file from it works. The tool does not use a regular C++ parser. This also means that it does not run the preprocessor. What I do expect is that it looks for things like tr("...") in the source code. What you have instead is tr(SETTINGS_GENERAL_NICKNAME_DEFAULT) which does not fit this pattern.

              You could try to to use tr already in your define:

              #define SETTINGS_GENERAL_NICKNAME_DEFAULT tr("NoName")
              
              QString nick = ttSettings->value(SETTINGS_GENERAL_NICKNAME, SETTINGS_GENERAL_NICKNAME_DEFAULT).toString();
              

              However, I would expect that tr() in it self does not work as the translation tool cannot extract the context for the tr-function. QT_TR_NOOP() could help instead.

              The problem with your initial code is not that it would not use the translation when run, it is only that the text is not extracted for translation. You can sprinkle a QT_TR_NOOP("NoName") or rather a QT_TRANSLATE_NOOP("MyClass", "NoName") somewhere in your source code. Maybe right above it's use (the compiler would throw this out). Or maybe as a global variable. Then the translation tool can pick it up.

              I don't see the exact reason why you choose a #define in the first place. It would only make sense if you could define them as a compiler parameter (with a corresponding guard). This does not make sense for translations, though. In modern C++ style you should aim for constants instead of #define whereever you can. This certainly applies here:

              // in header
              char const *const SETTINGS_GENERAL_NICKNAME;
              char const *const SETTINGS_GENERAL_NICKNAME_DEFAULT;
              // in cpp
              char const *const SETTINGS_GENERAL_NICKNAME = "general_/nickname";
              char const *const SETTINGS_GENERAL_NICKNAME_DEFAULT = QT_TR_NOOP("NoName");
              

              The rest of your code could then remain unchanged. If you are on the newest standard you can shorten the above by using constinit. (I hope I did everything right in my example with the whole global const thing.)

              1 Reply Last reply
              0
              • O Offline
                O Offline
                Oreonan
                wrote on last edited by
                #8

                Hi,
                thanks for your ansers. I added my string to .ts files using QT_TRANSLATE_NOOP("MainWindow", "NoName") in my .h file.
                Unfortunately I ahave again another problem. I have another file (common.cpp) with a lot of functions used in all files of project, in this file I have a function to return nickname of an user. If this user has set his nickname to a blank string, it's automatically set to "NoName" with this line:
                nickname = QString("%1").arg(SETTINGS_GENERAL_NICKNAME_DEFAULT);
                I tried the following:
                nickname = QString("%1").arg(QObject::tr(SETTINGS_GENERAL_NICKNAME_DEFAULT));
                But I still get "NonMae" instead of translated string.
                How I missed?
                Thanks.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Oreonan said in Translation from an header file:

                  How I missed?

                  You simply used a macro without reading the documentation on how to use it.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    Oreonan
                    wrote on last edited by Oreonan
                    #10

                    I understand that I need to use QCoreApplication::translate, but I don't understand how.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Oreonan said in Translation from an header file:

                      but I don't understand how.

                      What exactly is not understandable with this example?

                      static const char *greeting_strings[] = {
                          QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
                          QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
                      };
                      
                      QString global_greeting(int type)
                      {
                          return QCoreApplication::translate("FriendlyConversation",
                                                             greeting_strings[type]);
                      }
                      

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • O Offline
                        O Offline
                        Oreonan
                        wrote on last edited by
                        #12

                        Oh, yes, OK, just read correctly :)
                        It's working perfectly, thanks.

                        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