Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I change dynamicly language?



  • 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.


  • Moderators

    @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.



  • 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);
    

    }


  • Moderators

    @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



  • Okay I understend. Thanks for help :)


Log in to reply