Qt Forum

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

    Dynamic translation of strings in widgets

    General and Desktop
    qtcreator translate dynamic
    3
    10
    5979
    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.
    • G
      GregWilsonLindberg last edited by

      I understand that for strings that I implement in my code using tr(), I must re-send them on a languageChange event, but how do strings that are entered as part of a widget get re-translated? Is there some way to call the widget, or the window they are on to automatically re-translate them? Will strings get re-translated if hide() and then show() are called on the widget or window? Or should I just programatically generate all strings and then re-translate them myself?

      1 Reply Last reply Reply Quote 0
      • A
        alex_malyu last edited by

        If you need dynamic translation you have to write your code to support it.

        Check:
        http://www.informit.com/articles/article.aspx?p=1405555&seqNum=3
        and
        http://doc.qt.io/qt-4.8/internationalization.html

        G 1 Reply Last reply Reply Quote 0
        • G
          GregWilsonLindberg @alex_malyu last edited by

          @alex_malyu thanks for the pointer to the informit article.
          I already have my code designed for translations. My question is specifically, for things like a QButton, is the text in the 'text' property automatically handled by the system, or do I need to explicitly translate all text?
          It appears that I need to explicitly translate all text, and therefore there is no reason to enter any default text into the text property of any widget as the widget doesn't handle the LanguageChange event by itself, even though it will handle translation of the default string when it is first displayed.
          I was also asking if there was any way to force a widget to essentially re-translate it's text property by, maybe, calling hide() and then show() on the widget. Sounds like that won't work.
          Handling of the default value of the 'text' property is not explicitly dealt with in any article I've been able to find on dynamic translation.

          G 2 Replies Last reply Reply Quote 0
          • G
            GregWilsonLindberg @GregWilsonLindberg last edited by

            Sorry for the double post, I din't know that I had hit send twice.

            1 Reply Last reply Reply Quote 0
            • G
              GregWilsonLindberg @GregWilsonLindberg last edited by

              On further investigation, the UI code of a widget creates a retranlsateUi() function for all translatable text fields in any widgets on a window. So now the question is, how do you call that function?
              The setupUi() function is called through the ui parameter that is added to any QWidget derived object, so the retranslateUi() function can be called in the same way, ui->retranslateUi().
              This means that any translatable text contained in any widgets on a window can be re-translated by calling the ui->retranslateUi() function, which can be done in the users retranslateUi() function called from the LanguageChange event handler.
              It would be nice if this was documented somewhere and mentioned in the Qt article on Dynamic Translation.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi,

                Do you mean the retranslateUI part should be added to that chapter ?

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

                G 1 Reply Last reply Reply Quote 0
                • A
                  alex_malyu last edited by

                  That was covered in the links I gave you. look for Dynamic Translation

                  G 1 Reply Last reply Reply Quote 0
                  • G
                    GregWilsonLindberg @alex_malyu last edited by

                    @alex_malyu Generating your own retranslateUi() was covered in the links that you sent me, no where that I have found is it mentioned that each of the Ui_xxx.h files that are generated for each window that your program has contains a retranslateUi() function that you can call to re-translate the default values that have been entered for 'text' properties of widgets in your window.

                    1 Reply Last reply Reply Quote 0
                    • G
                      GregWilsonLindberg @SGaist last edited by

                      @SGaist I think that the fact that the Ui compiler generates a retranslateUi() function for each window should be mentioned. I've been trying to figure out if I needed to change all of my widget that I have entered default values for the 'text' property, and it turns out that the Ui compiler creates a retranslateUi() function, but it is not documented in the section on Dynamic Translation.

                      1 Reply Last reply Reply Quote 0
                      • A
                        alex_malyu last edited by alex_malyu

                        Below is an answer.

                        void MyWidget::changeEvent(QEvent *event)
                        {
                        if (e->type() == QEvent::LanguageChange) {
                        titleLabel->setText(tr("Document Title"));
                        ...
                        okPushButton->setText(tr("&OK"));
                        } else
                        QWidget::changeEvent(event);
                        }

                        code within if (e->type() == QEvent::LanguageChange) { ... )
                        should contain ALL the code which has to be called when language changed.
                        Basically every line of the code with tr() has to be called there.

                        In there you can put call to retranslateUi,
                        but retranslateUi is just a function where QtDesigner put strings created in there.
                        this might be or might not be sufficient for your widget.
                        Finally there are people who do not use designer.
                        That is why you could not find retranslateUi mentioned anywhere.
                        Keyword is tr().

                        For example you may fill tree widget outside of designer.
                        If you used text which has to be internationalized there, you need to make sure the same code is executed as a reaction on language changed.

                        Common logic suggests to put all code which requires initialization in a single function or slot.
                        Such function or slot should be called when QEvent::LanguageChange arrives.
                        Call this function myRetranslateUi if you want.

                        You do not have to subclass every widget (as suggested by example) though.
                        Alternative is to install event filter.

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