Switch back to native language after install qm file by QTranslator



  • I would like to switch back to native language after translation are done, but can't find a way to do it.

    #include <QApplication>
    #include <QDebug>
    #include <QPushButton>
    #include <QTranslator>
    
    int main(int argc, char *argv[])
    
    {
        QApplication app(argc, argv);
    
        QTranslator translator;
        translator.load("hellotr_la");
        app.installTranslator(&translator);
    
        QPushButton hello(QPushButton::tr("Hello world!"));
        hello.resize(100, 30);
    
        QTranslator default_language;  
    
        QObject::connect(&hello, &QPushButton::clicked, [&]()
        {
            static int removed = false;
            if(removed){
                qDebug()<<"translator reinstall";
                app.installTranslator(&translator);
                removed = false;
            }else{
                qDebug()<<"translator removed";
                removed = QCoreApplication::removeTranslator(&translator);
               //with or without this line, the result is same, the language never convert back to english
                app.installTranslator(&default_language);
            }
        });
    
        hello.show();
        return app.exec();
    }
    

    What is the proper way to do it?Thanks


  • Moderators


Log in to reply
 

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