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. How to do dynamic translations if we pass strings as text to widgets constructor.
Forum Updated to NodeBB v4.3 + New Features

How to do dynamic translations if we pass strings as text to widgets constructor.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k 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
    PoonamGupta
    wrote on last edited by VRonin
    #1

    Hello All,

    I am creating an application with a ability to have multiple language. It works pretty well if I load translations file on start in main function.

    But now I want to implement, dynamic translation. I am following Qts documents and some example. So I have implemented language change event in main window class like this

    void MainWindow::changeEvent(QEvent* event)
    {
     if(0 != event) {
      switch(event->type()) {
       // this event is send if a translator is loaded
       case QEvent::LanguageChange:
        ui.retranslateUi(this);
        break;
    
      }
    

    and for any widget I have to follow this code :

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

    but now problem with this code is text should be defined within the widget class, but for my case my all widgets are isolated with a business logic. I create instance of a widget and I pass text as a parameter to them. All text are pre defined in header file with tr keyword.

    so is there any other way to do dynamic translation and make widgets to update with new translation words ?

    Thanks in advance.

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

      Just pass the same parameters again, they should get translated correctly if your single-header is correctly implemented

      "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
      1
      • VRoninV VRonin

        Just pass the same parameters again, they should get translated correctly if your single-header is correctly implemented

        P Offline
        P Offline
        PoonamGupta
        wrote on last edited by PoonamGupta
        #3

        @VRonin

        Thanks for your reply.

        What to do mean by if single header is correctly implemented ? I just have bunch statement like this

        #define ABC QObject::tr("abc")

        like this. I guess it shouldnt be like this ?

        JonBJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          You need to use the method described here: http://doc.qt.io/qt-5/i18n-source-translation.html#using-qt-tr-noop-and-qt-translate-noop-in-c

          They use a static char*[] in the example but it's exactly the same with a defined string

          "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
          1
          • P PoonamGupta

            @VRonin

            Thanks for your reply.

            What to do mean by if single header is correctly implemented ? I just have bunch statement like this

            #define ABC QObject::tr("abc")

            like this. I guess it shouldnt be like this ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @PoonamGupta
            Since it is using QObject::tr("abc"), it looks like it will get a newly-translated string by calling tr() afresh. So long as code hasn't cached the old value somewhere and instead re-uses your macro each time it should be good to go.

            P 1 Reply Last reply
            0
            • JonBJ JonB

              @PoonamGupta
              Since it is using QObject::tr("abc"), it looks like it will get a newly-translated string by calling tr() afresh. So long as code hasn't cached the old value somewhere and instead re-uses your macro each time it should be good to go.

              P Offline
              P Offline
              PoonamGupta
              wrote on last edited by
              #6

              @JonB
              Thanks for the help.
              Yes I kept my original string as a member variable to a class and then I am assigning text to a widget using QObject::Tr(sourceText).
              It resolves my issue.

              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