Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved Problem with dynamic language change in QML

    QML and Qt Quick
    4
    14
    2821
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J.Hilk
      J.Hilk Moderators @bgfortran last edited by

      @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

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply Reply Quote 0
      • bgfortran
        bgfortran last edited by

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

        J.Hilk 1 Reply Last reply Reply Quote 0
        • J.Hilk
          J.Hilk Moderators @bgfortran last edited by

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

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply Reply Quote 0
          • bgfortran
            bgfortran last edited by bgfortran

            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();
            
            J.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @bgfortran last edited by

              @bgfortran
              have you checked if your loading is successfull?

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

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 0
              • bgfortran
                bgfortran last edited by

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

                Englisch

                BG loaded? true
                Englisch loaded? true
                
                1 Reply Last reply Reply Quote 0
                • bgfortran
                  bgfortran last edited by

                  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

                  1 Reply Last reply Reply Quote 0
                  • johngod
                    johngod last edited by

                    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")
                    	}
                    
                    
                    1 Reply Last reply Reply Quote 0
                    • Pablo J. Rogina
                      Pablo J. Rogina last edited by

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

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply Reply Quote 0
                      • bgfortran
                        bgfortran last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • bgfortran
                          bgfortran last edited by

                          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();
                          }
                          
                          1 Reply Last reply Reply Quote 0
                          • bgfortran
                            bgfortran last edited by

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

                            1 Reply Last reply Reply Quote 0
                            • johngod
                              johngod last edited by

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

                              1 Reply Last reply Reply Quote 1
                              • First post
                                Last post