Translation at runtime (dynamic translation)



  • I have a screen to pick the language for the app.
    I understand that I need to call app.removeTranslator() and then app.installTranslator(); on the language switch, and then listen to QEvent::LanguageChange to retranslate.
    But the problem is I don't have access to app in that screen:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv); //app is declared here
    
        QTranslator translator;
        translator.load("myApp_en");
        app.installTranslator(&translator);
    
        LoadingScreen loadingScreen;
        loadingScreen.showWindow();
    
        return app.exec();
    }
    

    And I need to change the language from another class, which inherits from QMainWindow. All widgets are created dynamically in code. For example:

    title = new QLabel();
    title->setText(tr("Choose Language"));
    

    So can someone tell me please how can I install a new translator from this other class?

    Thanks


  • Lifetime Qt Champion

    hi
    there is qApp that works in all application.



  • @mrjj Oh! Life saver!
    Do I really have to removeTranslator before installing a new one though?


  • Lifetime Qt Champion

    @AlaaM ¨
    Hi I think you can have multiple translators(file) active, so when you switch language
    its best to remove old one or it might get messy as the old one would also be searched.
    But if completely different language, i doubt anything crazy will happen.

    http://www.informit.com/articles/article.aspx?p=1405555&seqNum=3


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.