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. retranslate not updating dynamically in Qt6.2, but ok in Qt5.15
Forum Updated to NodeBB v4.3 + New Features

retranslate not updating dynamically in Qt6.2, but ok in Qt5.15

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 508 Views
  • 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.
  • L Offline
    L Offline
    Leon Zhang
    wrote on last edited by
    #1

    Hello,

    Translator and retranslate works fine in Qt5.15, but not updating display dynamically when upgrade to Qt6.2; Need to reload qml manually to display translated language.

    qml file as follow:

    import QtQuick
    import QtQuick.Controls
    
    ApplicationWindow {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        property bool eng: true
    
        Column {
            spacing: 10
    
            Text {
                text: Tr.ssetWeighCycle
            }
    
            Button {
                width: 100
                height: 44
                text: eng ? "English" : "Japanese"
                onClicked: {
                    eng = !eng
                    if (eng) {
                        translate.selectLanguage(0)
                    } else {
                        translate.selectLanguage(1)
                    }
                    //reload loader2 then translate works
                    loader2.source = ""
                    loader2.source = "panel.qml"
                }
            }
        }
    
        Row {
            spacing: 10
            anchors.centerIn: parent
            Loader {
                id:loader
                width: 200
                height: 200
                source: "panel.qml"
            }
    
            Loader {
                id:loader2
                width: 200
                height: 200
                source: "panel.qml"
            }
        }
    }
    
    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemons
      wrote on last edited by
      #2

      I always do the translation switch from C++ like this:

      
      Translator::Translator(QQmlEngine *engine)
      {
          _translator = new QTranslator(this);
          _engine = engine;
      }
      
      void Translator::selectLanguage(QString language)
      {
          QString path = ":/i18n";
          if (!_translator->load(language, "AppName",  "_", path, ".qm"))
          {
              qDebug() << "Failed to load translation file, falling back to English";
          }
          qApp->installTranslator(_translator);
          
          // change default e.g. for dates | decimals
          QLocale::setDefault(language);
      
          // no QML reload required, handled automatically by engine
          _engine->retranslate();
      
          // if you want to do anything else onLanguageChanged
          emit languageChanged();
      }
      
      1 Reply Last reply
      0

      • Login

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