Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-116589

Dynamic translations seem not working with QQmlApplicationEngine::loadFromModule()

    XMLWordPrintable

Details

    • All
    • 69fce1185 (dev), e0eb137af (6.6), 204b47558 (tqtc/lts-6.5), 986b4d155 (dev), 8206c3d86 (6.7), 8e4a2a888 (6.6), c37b7612c (tqtc/lts-6.5)

    Description

      Hi,

      I cannot find how to use dynamic translation by relying on the QQmlApplicationEngine like explained in the documentation (https://doc.qt.io/qt-6/i18n-source-translation.html#qml-use-qqmlapplicationengine ) with the function loadFromModule() which seems the way to go now (https://www.qt.io/blog/whats-new-for-qml-modules-in-6.5 ).

       

      I looked a bit at the source and is not translationsDirectory  the issue here?

      I am not sure where it is defined when using loadFromModule().

      void QQmlApplicationEnginePrivate::init()
      {
          Q_Q(QQmlApplicationEngine);
          q->connect(q, &QQmlApplicationEngine::quit, QCoreApplication::instance(),
                     &QCoreApplication::quit, Qt::QueuedConnection);
          q->connect(q, &QQmlApplicationEngine::exit, QCoreApplication::instance(),
                     &QCoreApplication::exit, Qt::QueuedConnection);
          QObject::connect(q, &QJSEngine::uiLanguageChanged, q, [this]()
      
      {         _q_loadTranslations();     }
      
      );
      #if QT_CONFIG(translation)
          QTranslator* qtTranslator = new QTranslator(q);
          if (qtTranslator->load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLibraryInfo::path(QLibraryInfo::TranslationsPath), QLatin1String(".qm")))
              QCoreApplication::installTranslator(qtTranslator);
          else
              delete qtTranslator;
      #endif
          auto *selector = new QQmlFileSelector(q,q);
          selector->setExtraSelectors(extraFileSelectors);
          QCoreApplication::instance()->setProperty("__qml_using_qqmlapplicationengine", QVariant(true));
      }
      
      void QQmlApplicationEnginePrivate::_q_loadTranslations()
      {
      #if QT_CONFIG(translation)
          Q_Q(QQmlApplicationEngine);
          if (translationsDirectory.isEmpty())
              return;
      
          auto translator = std::make_unique<QTranslator>();
          if (!uiLanguage.value().isEmpty()) {
              QLocale locale(uiLanguage);
              if (translator->load(locale, QLatin1String("qml"), QLatin1String("_"), translationsDirectory, QLatin1String(".qm")))
      
      {             if (activeTranslator)                 QCoreApplication::removeTranslator(activeTranslator.get());             QCoreApplication::installTranslator(translator.get());             activeTranslator.swap(translator);         }
      
          } else
      
      {         activeTranslator.reset();     }
      
          q->retranslate();
      #endif
      }
      
      void QQmlApplicationEnginePrivate::startLoad(const QUrl &url, const QByteArray &data, bool dataFlag)
      {
          Q_Q(QQmlApplicationEngine);
      
          ensureInitialized();
      
          if (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc"))
      
      {         QFileInfo fi(QQmlFile::urlToLocalFileOrQrc(url));         translationsDirectory = fi.path() + QLatin1String("/i18n");     }
      
      else
      
      {         translationsDirectory.clear();     }
      
          _q_loadTranslations(); //Translations must be loaded before the QML file is
          QQmlComponent *c = new QQmlComponent(q, q);
      
          if (dataFlag)
              c->setData(data,url);
          else
              c->loadUrl(url);
      
          ensureLoadingFinishes(c);
      }
      
      void QQmlApplicationEnginePrivate::startLoad(QAnyStringView uri, QAnyStringView type)
      
      {     Q_Q(QQmlApplicationEngine);     _q_loadTranslations(); //Translations must be loaded before the QML file is     QQmlComponent *c = new QQmlComponent(q, q);     ensureInitialized();     c->loadFromModule(uri, type);     ensureLoadingFinishes(c); }
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              olivier.decanniere Olivier De Cannière
              pijaouen Pierre Jaouen
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes