Problem with dynamic language change in QML



  • In my app I need internationalization. Say I have created several .ts file for different languages.
    I have follow function.

    void Parking::switchTranslator(QString lang)
    {
    
        static QTranslator translator;
        if (!translator.isEmpty())
           appl->removeTranslator(&translator);
        if(lang == "bg") {
          translator.load(":/bulgarian.qm");
        } else if (lang == "en") {
          translator.load(":/english.qm");
        }
          appl->installTranslator(&translator);
    
          qmlEngine->retranslate();
    }
    

    The appl is an instance from main

     QApplication app(argc, argv);
    

    The qmlEngine is an instance from

    new QQmlApplicationEngine(this);
    

    whitch load an application. The code works correctly only on initial startup of the App. But it doesn't work when i change the language of my application at runtime. where is the problem?



  • @bgfortran
    what version of Qt are you using? Because the retranslate function was not correctly working before 5.10 and one had to do a workaround



  • @J-Hilk the version of Qt is 5.10 :)



  • @bgfortran
    ok, looking at your example,

    I may be wrong here, but when you enter you function, you create a new Translator which should be empty and you only remove the old translator when the new one is not empty.

    I would say you never remove the old translator and simply load an additional one. That will prioritize the old translations and only fall back to the new ones, when the old one do not match.



  • I tried this way, but the result is the same :)

     QTranslator trBG;
      QTranslator trEN;
    
       trBG.load(":/bulgarian.qm");
       trEN.load(":/english.qm")
    
    if(lang == "bg") {
         appl->removeTranslator(&trEN);
         appl->installTranslator(&trBG);
        } else if (lang == "en") {
            appl->removeTranslator(&trBG);
            appl->installTranslator(&trEN);
        }
    
          qmlEngine->retranslate();
    


  • @bgfortran
    have you checked if your loading is successfull?

    qDebug() << "Englisch loaded?" << trEN.load(":/english.qm");
    


  • @J.Hilk said in Problem with dynamic language change in QML:

    Englisch

    BG loaded? true
    Englisch loaded? true
    


  • I think that the QML is doesn't refresh. But why- i don't know. after install a new translator. The first time the Translator is worked, but in runtime it doesn't work



  • Are you sure that switchTranslator() is being called ? Put some debbuging code inside to check it.
    Any way here is a sample from my projects that's tested and it works.

    main.cpp
    	
    #include "qmltranslator.h"
    
    void main() {
    	QGuiApplication app(argc, argv);
        
        QmlTranslator translator;
        qmlRegisterType<QmlTranslator>("com.QMLTranslator", 1, 0, "QmlTranslator");
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("qmlTranslator", &translator);
    
        qDebug()<<"connection: "<<QObject::connect(&translator, SIGNAL(languageChanged()),
                           &engine,  SLOT(retranslate()));
    
    }
    
    qmltranslator.h
    
    #ifndef QMLTRANSLATOR_H
    #define QMLTRANSLATOR_H
    
    #include <QTranslator>
    
    class QmlTranslator : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged)
    public:
        explicit QmlTranslator(QObject *parent = nullptr);
    
        QString language(){return m_language;}
        void setLocaleTranslation(){
            if (!appTranslator.isEmpty())
                qApp->removeTranslator(&appTranslator);
            if (!appTranslator.load("file_" + QLocale::system().name(), qmPath)) //file_ is your qm file name
                qWarning("Translation file not found");
            qApp->installTranslator(&appTranslator);
        }
    
    signals:
        void languageChanged();
    
    public slots:
    
        void setLanguage(QString language) {
            if(language == m_language)
              return;
          m_language = language;
          if (!appTranslator.isEmpty())
              qApp->removeTranslator(&appTranslator);
    
          appTranslator.load("file_" + language, qmPath);//file_ is your qm file name
          qApp->installTranslator(&appTranslator);
    
          emit languageChanged();
        }
    
    private:
        QTranslator appTranslator;
        QString m_language;
        QString qmPath;
    };
    
    #endif // QMLTRANSLATOR_H
    
    
    qmltranslator.cpp
    
    #include "qmltranslator.h"
    
    QmlTranslator::QmlTranslator(QObject *parent)
        : QObject(parent)
    {
        qmPath = ":/translations";
    }
    
    qml file
    
    import com.QMLTranslator 1.0
    ....
    	onSomeButtonClicked: {
    		qmlTranslator.setLanguage("pt")
    	}
    
    


  • @bgfortran maybe this wiki post can help you a little bit.



  • @johngod

    i created simple project with some code and it is Ok. I have to say that the language setting comes from a mobile application. Тhe language changes only when the application starts up. If Bulgarian is selected the my application reads this setting and loads the Bulgarian qml. But when the app is running and the change is made, the qml is not reloaded.
    Important!!! All translators are loaded.



  • Here is my code

    Translator.h

    class Translator: public QObject
    {
        Q_OBJECT
    
    public:
        static QSharedPointer<Translator> getInstance();
        virtual ~Translator();
        void setQApplication(QApplication *app);
        void setQmlEngine(QQmlApplicationEngine *qmlEn);
        void switchTranslator(QString lang);
    private:
        explicit Translator(QObject *parent = 0);
        static QSharedPointer<Translator> instance;
    
        QApplication *qApplication;
        QQmlApplicationEngine *qmlEngine;
    
        QTranslator trBG;
        QTranslator trEN;
    
    };
    
    

    Translator.cpp

    #include "Translator.h"
    
    QSharedPointer<Translator> Translator::instance = QSharedPointer<Translator>(nullptr);
    Translator::Translator(QObject *parent) : QObject(parent)
    {
    
        trBG.load(":/bg.qm");
        trEN.load(":/en.qm");
    }
    
    Translator::~Translator() {}
    
    QSharedPointer<Translator> Translator::getInstance()
    {
      if (instance.isNull()) {
        instance = QSharedPointer<Translator>(new Translator());
      }
      return instance;
    }
    
    void Translator::setQApplication(QApplication *app)
    {
        qApplication = app;
    }
    
    void Translator::setQmlEngine(QQmlApplicationEngine *qmlEn)
    {
        qmlEngine = qmlEn;
    }
    
    void Translator::switchTranslator(QString lang)
    {
        if(trBG.isEmpty()) {
            qDebug()<<"The BG translator is empty!";
        }
    
        if(trEN.isEmpty()) {
            qDebug()<<"The EN translator is empty!";
        }
    
        if(lang == "bg") {
         qApplication->removeTranslator(&trEN);
         qApplication->installTranslator(&trBG);
        } else if (lang == "en") {
         qApplication->removeTranslator(&trBG);
         qApplication->installTranslator(&trEN);
        }
    
        qmlEngine->retranslate();
    }
    


  • If I stop the debugging after qApplication->installTranslator() and after a long time continue- the operation is successful



  • @bgfortran How are you calling switchTranslator(...) from qml ?


Log in to reply
 

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