Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QTranslator - translate defines in headers

    General and Desktop
    4
    8
    2251
    Loading More Posts
    • 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
      pVit last edited by

      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

      VRonin raven-worx 2 Replies Last reply Reply Quote 0
      • VRonin
        VRonin @pVit last edited by

        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 Reply Quote 2
        • raven-worx
          raven-worx Moderators @pVit last edited by raven-worx

          @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 Reply Quote 3
          • P
            pVit last edited by

            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.

            micland 1 Reply Last reply Reply Quote 0
            • micland
              micland @pVit last edited by

              @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 Reply Quote 2
              • P
                pVit @micland last edited by

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

                1 Reply Last reply Reply Quote 1
                • VRonin
                  VRonin last edited by

                  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 Reply Quote 4
                  • P
                    pVit @VRonin last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post