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 can I change dynamicly language?
Forum Updated to NodeBB v4.3 + New Features

How can I change dynamicly language?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 359 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.
  • YaldizY Offline
    YaldizY Offline
    Yaldiz
    wrote on last edited by Yaldiz
    #1

    HI:

    I'm using qt5 on embedded device.
    I want to change language dynamicly of QLabes , QPushButton etc...

    I tried following code but It's doesn't work. Maybe I need to do update widget.

    void HomeWelding::on_LanguageChanged(QString language)
    {
    CurrentLanguage = language;

    if(language == "en")
    {
        qApp->removeTranslator(Tranlator);
        Tranlator->load(QString(":/translation/lcd_en.qm"));
    }
    else
    {
        qApp->removeTranslator(Tranlator);
        Tranlator->load(QString(":/translation/lcd_tr.qm"));
    }
    
    qApp->installTranslator(Tranlator);
    

    }

    What I made wrong and how can I do that?

    NOTE:

    QTranslator T;
    QString lang{argv[1]};
    
    if(lang == "Turkce")
    {
        T.load(":/translation/lcd_tr.qm");
        qDebug() << "Turkce Destek Secildi";
        a.installTranslator(&T);
    }
    else if(lang == "English")
    {
        qDebug() << "English Support";
        T.load(":/translation/lcd_en.qm");
        a.installTranslator(&T);
    }
    
    HomeWelding w;
    w.show();
    

    This code works but I need to change language at run time.

    Regards.

    J.HilkJ 1 Reply Last reply
    1
    • YaldizY Yaldiz

      HI:

      I'm using qt5 on embedded device.
      I want to change language dynamicly of QLabes , QPushButton etc...

      I tried following code but It's doesn't work. Maybe I need to do update widget.

      void HomeWelding::on_LanguageChanged(QString language)
      {
      CurrentLanguage = language;

      if(language == "en")
      {
          qApp->removeTranslator(Tranlator);
          Tranlator->load(QString(":/translation/lcd_en.qm"));
      }
      else
      {
          qApp->removeTranslator(Tranlator);
          Tranlator->load(QString(":/translation/lcd_tr.qm"));
      }
      
      qApp->installTranslator(Tranlator);
      

      }

      What I made wrong and how can I do that?

      NOTE:

      QTranslator T;
      QString lang{argv[1]};
      
      if(lang == "Turkce")
      {
          T.load(":/translation/lcd_tr.qm");
          qDebug() << "Turkce Destek Secildi";
          a.installTranslator(&T);
      }
      else if(lang == "English")
      {
          qDebug() << "English Support";
          T.load(":/translation/lcd_en.qm");
          a.installTranslator(&T);
      }
      
      HomeWelding w;
      w.show();
      

      This code works but I need to change language at run time.

      Regards.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Yaldiz

      that it works, when you load it before creating your widgets is a good sign.

      The key component here is, after load a new translation, you have to reset() all strings /texts that are marked with the tr() function.

      if you have ui forms and only set the texts in the designer and not by hand in code, you can use a call to retranslateUi to update those strings: ui->retranslateUi(this);

      QWidgets in general have a changeEvent(QEvent *event) function that can be overwritten, do that and listen to the LanguageChange event type event->type() == QEvent::LanguageChange to know when you have to reset all texts you set in code.

      if you're using QML you only have to call retranslate on the qmlengine instance and you should be done.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • YaldizY Offline
        YaldizY Offline
        Yaldiz
        wrote on last edited by
        #3

        I wrote all the codes by hand.

        Say that I have QLabel* label = new QLabel(this);

        Should I change the text like below code?

        void changeEvent(QEvent *event)
        {
        if(event->type == QEvent::LanguageChange)
        {
        label->setText(tr("Example"));
        }

           QWidget::changeEvent(event);
        

        }

        J.HilkJ 1 Reply Last reply
        0
        • YaldizY Yaldiz

          I wrote all the codes by hand.

          Say that I have QLabel* label = new QLabel(this);

          Should I change the text like below code?

          void changeEvent(QEvent *event)
          {
          if(event->type == QEvent::LanguageChange)
          {
          label->setText(tr("Example"));
          }

             QWidget::changeEvent(event);
          

          }

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Yaldiz said in How can I change dynamicly language?:

          I wrote all the codes by hand.

          Say that I have QLabel* label = new QLabel(this);

          Should I change the text like below code?

          void changeEvent(QEvent *event)
          {
          if(event->type == QEvent::LanguageChange)
          {
          label->setText(tr("Example"));
          }

             QWidget::changeEvent(event);
          

          }

          I assume this is mark-up code, because there's missing stuff, but yes in general you're correct.

          I personally have one function to wherein I set all texts. That makes it easy to call it again, when the language changes


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • YaldizY Offline
            YaldizY Offline
            Yaldiz
            wrote on last edited by
            #5

            Okay I understend. Thanks for help :)

            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