Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Dynamic Language Translation in Qt
Forum Updated to NodeBB v4.3 + New Features

Dynamic Language Translation in Qt

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 4 Posters 23.0k Views 1 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.
  • N Offline
    N Offline
    neel2818
    wrote on last edited by
    #1

    Hi All,

    I have written one application in that application i have to do reboot the system then only after language translation applied.
    What i want to change the language without reboot i mean at run time i have to change the all text in translated localized form.
    In my sample application in main i have written following code.

    @
    QTranslator * translator;
    translator = new QTranslator;
    translator->load( "QM file path where the language file exist ");
    app.installTranslator(translator);
    @

    From another file or form how can i know the language has changed and how to handle it ?
    Can someone explain me how to do with this code ? or sample code or link will helped me a lot.

    Thanks,
    Neel

    [Edit] Use @ for code

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Andreas
      wrote on last edited by
      #2

      You need to react on the language change event within your widgets. The change event is posted to all widgets if there is a change to the installed Qt translator.

      "Dynamic translations":http://doc.qt.nokia.com/4.7/internationalization.html#dynamic-translation

      example:
      @ void MyWidget::MyWidget(QWidget* parent)
      : QWidget(parent)
      {
      retranslate();
      }

      void MyWidget::changeEvent(QEvent* event)
      {
      if (event->type() == QEvent::LanguageChange)
      {
      // retranslate designer form
      ui.retranslateUi(this);

             // retranslate other widgets which weren't added in designer
             retranslate();
         }
         
         // remember to call base class implementation
         QWidget::changeEvent(event);
      

      }

      void MyWidget::retranslate()
      {
      mylabel->setText(tr("Text"));
      }
      @

      --
      Andreas

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AlterX
        wrote on last edited by
        #3

        Sorry for my intrusion...I've just located an entire desktop app, but I'm able to see translations just for my texts and not for ui generated texts.
        shall I have to use dynamic translation to change ui texts?!

        Qt Ambassador
        Real-time cooperative teams: http://www.softairrealfight.net
        Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

        https://codereview.qt-project.org/...

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hareen Laks
          wrote on last edited by
          #4

          Some time ago I did it as follows;

          First create actions for different languages and connect their triggered() SIGNALs with language specific SLOTs.

          @
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          connect(ui->actionDanish, SIGNAL(triggered()), this, SLOT(changeLanguageToDanish()));
          connect(ui->actionEnglish, SIGNAL(triggered()), this, SLOT(changeLanguageToEnglish()));
          QApplication::instance()->installTranslator(&translator);

          }@

          Then from that SLOTs call to language change function.
          @
          void MainWindow::changeLanguageToEnglish()
          {
          changeLanguageTo(English_file_location);
          }

          void MainWindow::changeLanguageToDanish()
          {
          changeLanguageTo(Danish_file_location);
          }@

          Load the language file

          @void MainWindow::changeLanguageTo(const QString &language_file_location)
          {
          translator.load(language_file_location);
          }@

          Call language change event

          @void MainWindow::changeEvent(QEvent *e)
          {
          if(e->type() == QEvent::LanguageChange)
          {
          ui->retranslateUi(this);
          }
          QMainWindow::changeEvent(e);

          }@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AlterX
            wrote on last edited by
            #5

            So I have to do something to translate ui....I've tried to implement changeEvent for one ui file and now it is correctly located.
            My question is: why my tr(text) in the code is automatically translated at runtime and ui generated tr did not?

            Qt Ambassador
            Real-time cooperative teams: http://www.softairrealfight.net
            Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

            https://codereview.qt-project.org/...

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hareen Laks
              wrote on last edited by
              #6

              @ AlterX ,

              what do you mean by ui generated?

              is it means the text of buttons, labels, menus etc?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AlterX
                wrote on last edited by
                #7

                Yes,
                it creates ui_xxxx.h with all localized code QApplication::translate(), but even they are present like my own code, ui form doesn't get translated at runtime like my code does!

                Qt Ambassador
                Real-time cooperative teams: http://www.softairrealfight.net
                Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                https://codereview.qt-project.org/...

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  Hareen Laks
                  wrote on last edited by
                  #8

                  generally I set the text of button as _[Click Me] _.
                  Then by using 'Qt Linguist' set the text in different languages for [Click me] .

                  And I'm not doing any other thing except my previous reply. ChangeEvent do the rest.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AlterX
                    wrote on last edited by
                    #9

                    Ok so changeEvent is necessary to translate uis...

                    Qt Ambassador
                    Real-time cooperative teams: http://www.softairrealfight.net
                    Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                    https://codereview.qt-project.org/...

                    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