Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Change language on runtime in QML application
Qt 6.11 is out! See what's new in the release blog

Change language on runtime in QML application

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 8.5k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kalempir
    wrote on last edited by
    #1

    I want to change text on runtime. User can select languages on runtime. How to do it?
    I searched on google and see some examples.

    1. I created separate class MyClass inherited from QObject and created new slot: languageCreated(QString lang)
      @#include <QObject>
      #include <QDebug>
      #include <QGuiApplication>
      #include <QTranslator>

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    QString lang;
    explicit MyClass(QObject *parent = 0);
    void setApp(QGuiApplication *app) {
    this->app = app;
    }

    private:
    QGuiApplication *app;
    signals:

    public slots:
    void languageChanged(QString lang) {
    QTranslator translator;
    QString filename = QString("languages/lang_") + lang;
    if( translator.load(filename, ":/") ){
    app->installTranslator(&translator);
    qDebug() << "Translation file loaded" << filename;
    qDebug() << "Language changed to " << lang;
    }
    else
    qDebug() << "Translation file not loaded:" << filename;

    }
    

    };@

    in main.cpp I created object of this class and connect its slot to qml item signal.

    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QTranslator>
    #include <QTextCodec>
    #include <QLocale>
    #include <QDebug>
    #include "myclass.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    qDebug() << "local:" << QLocale::system&#40;&#41;.name();
    QString locale = "ru_RU";
    QString filename = QString("languages/lang_") + locale;
    
    static QTranslator translator;
    if( translator.load(filename, ":/") ){
        app.installTranslator(&translator);
        QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
        qDebug() << "Translation file loaded" << filename;
    } else
        qDebug() << "Translation file not loaded:" << filename;
    
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/MultiLingualQML/main.qml"&#41;);
    viewer.showExpanded();
    
    MyClass myclass;
    
    QObject *item = (QObject *)viewer.rootObject();
    QObject::connect(item, SIGNAL(languageChanging(QString)), &myclass, SLOT(languageChanged(QString)));
    
    return app.exec();
    

    }@

    and on mouse click in qml I emit signal.

    @import QtQuick 2.0
    import QtQuick.Layouts 1.0

    Item {
    id: item
    signal languageChanging(string lang)
    GridLayout {
    Rectangle {
    Layout.fillWidth: true;
    width: 80
    height: 80
    anchors.verticalCenter: parent.verticalCenter
    Text {
    id: hello
    anchors.fill: parent
    text: qsTr("Hello World") //+ Retranslate.onLanguageChanged
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    Qt.quit();
    }
    }
    }
    Rectangle {
    width: 80
    height: 80
    Layout.fillWidth: true;
    Text {
    id: lang_en
    text: "EN"
    }
    MouseArea {
    anchors.fill: lang_en
    onClicked: {
    item.languageChanging("en_US");
    hello.text = qsTr("Hello World")
    }
    }
    }
    Rectangle {
    Layout.fillWidth: true;
    width: 80
    height: 80
    Text {
    id: lang_ru
    text: "Русский"
    }
    MouseArea {
    anchors.fill: lang_ru
    onClicked: {
    item.languageChanging("ru_RU");
    hello.text = qsTr("Hello World")
    }
    }
    }
    Rectangle {
    Layout.fillWidth: true;
    width: 80
    height: 80
    Text {
    id: name
    text: qsTr("Firstname")
    }
    }
    }
    }
    @

    debug logs show that slot of myclass object is ran. But language of text not changed.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      This "How-To":http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML could be useful..


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalempir
        wrote on last edited by
        #3

        [quote author="Rahul Das" date="1385372342"]This "How-To":http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML could be useful..[/quote]
        Thank you. I will look at it.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalempir
          wrote on last edited by
          #4

          Unfortunatly I did not succeed. My project is Qt Quick project. On example above I thought is Qt GUI project.
          May be there is not big difference between this two type of projects to implement this functionality, but I could not manage it.
          Any help is appreciated.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            The example given is exactly what you need except it's a QtQuick1 project but that shouldn't be a problem.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            -1
            • K Offline
              K Offline
              kalempir
              wrote on last edited by
              #6

              ok, I will keep trying.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalempir
                wrote on last edited by
                #7

                thank you Rahul Das and SGaist.
                I have succeed. But I make a little change on example you gave. I used translation file with qm extension and included it to resource file. Otherwise it did not work on my example. I dont know why.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved