Qt World Summit: Submit your Presentation

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;
        LoadingScreen loadingScreen;
        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?


  • Lifetime Qt Champion

    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.


Log in to reply