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. QTranslator - translate defines in headers
QtWS25 Last Chance

QTranslator - translate defines in headers

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.0k 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.
  • P Offline
    P Offline
    pVit
    wrote on last edited by
    #1

    Hello,
    how could I translate defines in headers?

    • tr("text") is not available in .h files
    • QT_TR_NOOP("text") and QT_TRANSLATE_NOOP("source", "text") adds text for translate to .ts file but it doesn't take translated text from .qm file
    • tr(DEFINED_TEXT_IN_HEADER) in .cpp doesn't work

    Do you know how to do that?
    Thanks

    VRoninV raven-worxR 2 Replies Last reply
    0
    • P pVit

      Hello,
      how could I translate defines in headers?

      • tr("text") is not available in .h files
      • QT_TR_NOOP("text") and QT_TRANSLATE_NOOP("source", "text") adds text for translate to .ts file but it doesn't take translated text from .qm file
      • tr(DEFINED_TEXT_IN_HEADER) in .cpp doesn't work

      Do you know how to do that?
      Thanks

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Can you post your .h file?
      is it a QObject derived class?
      did you include Q_OBJECT?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • P pVit

        Hello,
        how could I translate defines in headers?

        • tr("text") is not available in .h files
        • QT_TR_NOOP("text") and QT_TRANSLATE_NOOP("source", "text") adds text for translate to .ts file but it doesn't take translated text from .qm file
        • tr(DEFINED_TEXT_IN_HEADER) in .cpp doesn't work

        Do you know how to do that?
        Thanks

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @pVit
        also make sure that the translator is installed before the tr() methods are called.
        If it is (re-)installed later, you need to make sure you reassign all the strings using the tr() method again.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • P Offline
          P Offline
          pVit
          wrote on last edited by
          #4

          I have class texts, there are defines I need to translate:

          // texts.h ------------------------------------------------------------------------------------------
          #ifndef TEXTS_H
          #define TEXTS_H

          #include <QObject>

          class QLabel;
          class texts : public QObject
          {
          Q_OBJECT

          #define MONDAY tr("Monday")
          #define TUESDAY tr("Tuesday")
          #define WEDNESDAY tr("Wednesday")

          public:
          explicit texts(QObject *parent = 0);
          ~texts();
          void setText(QLabel *label);
          };
          #endif // TEXTS_H

          // texts.cpp ------------------------------------------------------------------------------------------
          #include "texts.h"
          #include <QDebug>
          #include <QLabel>

          texts::texts(QObject *parent) : QObject(parent){}
          texts::~texts(){}
          void texts::setText(QLabel *label)
          {
          label->setText(MONDAY);
          }

          // mainwindow ------------------------------------------------------------------------------------------
          void MainWindow::on_pushButtoncz_clicked()
          {
          setLanguage(lang_CZ);
          m_pTexts->setText(ui->label_pondeli); -------> works well
          ui->label_utery->setText(MONDAY); -------> doesn´t work
          }

          It looks like problem is that translations in .qm / .ts files are classified by class names and works only in specific classes, not works at all.

          miclandM 1 Reply Last reply
          0
          • P pVit

            I have class texts, there are defines I need to translate:

            // texts.h ------------------------------------------------------------------------------------------
            #ifndef TEXTS_H
            #define TEXTS_H

            #include <QObject>

            class QLabel;
            class texts : public QObject
            {
            Q_OBJECT

            #define MONDAY tr("Monday")
            #define TUESDAY tr("Tuesday")
            #define WEDNESDAY tr("Wednesday")

            public:
            explicit texts(QObject *parent = 0);
            ~texts();
            void setText(QLabel *label);
            };
            #endif // TEXTS_H

            // texts.cpp ------------------------------------------------------------------------------------------
            #include "texts.h"
            #include <QDebug>
            #include <QLabel>

            texts::texts(QObject *parent) : QObject(parent){}
            texts::~texts(){}
            void texts::setText(QLabel *label)
            {
            label->setText(MONDAY);
            }

            // mainwindow ------------------------------------------------------------------------------------------
            void MainWindow::on_pushButtoncz_clicked()
            {
            setLanguage(lang_CZ);
            m_pTexts->setText(ui->label_pondeli); -------> works well
            ui->label_utery->setText(MONDAY); -------> doesn´t work
            }

            It looks like problem is that translations in .qm / .ts files are classified by class names and works only in specific classes, not works at all.

            miclandM Offline
            miclandM Offline
            micland
            wrote on last edited by
            #5

            @pVit

            In your example the macro gets expanded in the context of MainWindow so you're calling MainWindow::tr(...) instead of texts::tr(...). This requires a separate translation.
            But the method is static so it should work when adjusting the macros as followed:

            #define MONDAY texts::tr("Monday")
            #define TUESDAY texts::tr("Tuesday")
            #define WEDNESDAY texts::tr("Wednesday")
            
            P 1 Reply Last reply
            2
            • miclandM micland

              @pVit

              In your example the macro gets expanded in the context of MainWindow so you're calling MainWindow::tr(...) instead of texts::tr(...). This requires a separate translation.
              But the method is static so it should work when adjusting the macros as followed:

              #define MONDAY texts::tr("Monday")
              #define TUESDAY texts::tr("Tuesday")
              #define WEDNESDAY texts::tr("Wednesday")
              
              P Offline
              P Offline
              pVit
              wrote on last edited by
              #6

              @micland
              Thanks a lot! It's working now.

              1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Just a note, if those are just days of the week, you can use QDate(2016,10,10).toString(QStringLiteral("dddd")). this will be the day of the week (Monday in this case as 2016-10-10 is a monday) automatically translated to the target locale, no need for manual translations.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                P 1 Reply Last reply
                4
                • VRoninV VRonin

                  Just a note, if those are just days of the week, you can use QDate(2016,10,10).toString(QStringLiteral("dddd")). this will be the day of the week (Monday in this case as 2016-10-10 is a monday) automatically translated to the target locale, no need for manual translations.

                  P Offline
                  P Offline
                  pVit
                  wrote on last edited by
                  #8

                  @VRonin
                  Thanks for info but these words were only examples, I have many more defines :)

                  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