Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
6.5.2
-
None
-
-
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
- relates to
-
QTBUG-116588 Documentation about dynamic translations with QML could be improved
- Reported