aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlapplicationengine.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2020-01-22 13:37:48 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2020-01-23 15:56:40 +0100
commit020a6e67766595351bcf911e965b26952a7c81b8 (patch)
tree6cbb9340cb28f18efa98c9395b782cd13b81f101 /src/qml/qml/qqmlapplicationengine.cpp
parentaca7b33368655949d238a0cdfc0073b6b8532872 (diff)
Add Qt.uiLanguage and QJSEngine::uiLanguage properties
[ChangeLog][QtQml] Added Qt.uiLanguage and QJSEngine::uiLanguage properties These properties mirror the same value in QML and C++ and can be used freely. They also provide API symmetry to Qt for MCUs. QQmlApplicationEngine binds to this property and applies translations accordingly by constructing a QLocale with the value and using QTranslator::load(locale). Change-Id: Id87d6ee64679b07ff3cb47844594e8eeebd8c8b6 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Christian Kamm <mail@ckamm.de>
Diffstat (limited to 'src/qml/qml/qqmlapplicationengine.cpp')
-rw-r--r--src/qml/qml/qqmlapplicationengine.cpp37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/qml/qml/qqmlapplicationengine.cpp b/src/qml/qml/qqmlapplicationengine.cpp
index 7f80fe5e1c..7d961cd0c7 100644
--- a/src/qml/qml/qqmlapplicationengine.cpp
+++ b/src/qml/qml/qqmlapplicationengine.cpp
@@ -50,6 +50,7 @@ QT_BEGIN_NAMESPACE
QQmlApplicationEnginePrivate::QQmlApplicationEnginePrivate(QQmlEngine *e)
: QQmlEnginePrivate(e)
{
+ uiLanguage = QLocale().bcp47Name();
}
QQmlApplicationEnginePrivate::~QQmlApplicationEnginePrivate()
@@ -72,6 +73,7 @@ void QQmlApplicationEnginePrivate::init()
&QCoreApplication::quit, Qt::QueuedConnection);
q->connect(q, &QQmlApplicationEngine::exit, QCoreApplication::instance(),
&QCoreApplication::exit, Qt::QueuedConnection);
+ q->connect(q, SIGNAL(uiLanguageChanged()), q_func(), SLOT(_q_loadTranslations()));
#if QT_CONFIG(translation)
QTranslator* qtTranslator = new QTranslator(q);
if (qtTranslator->load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath), QLatin1String(".qm")))
@@ -83,20 +85,27 @@ void QQmlApplicationEnginePrivate::init()
QCoreApplication::instance()->setProperty("__qml_using_qqmlapplicationengine", QVariant(true));
}
-void QQmlApplicationEnginePrivate::loadTranslations(const QUrl &rootFile)
+void QQmlApplicationEnginePrivate::_q_loadTranslations()
{
#if QT_CONFIG(translation)
- if (rootFile.scheme() != QLatin1String("file") && rootFile.scheme() != QLatin1String("qrc"))
+ if (translationsDirectory.isEmpty())
return;
- QFileInfo fi(QQmlFile::urlToLocalFileOrQrc(rootFile));
-
Q_Q(QQmlApplicationEngine);
- QTranslator *translator = new QTranslator(q);
- if (translator->load(QLocale(), QLatin1String("qml"), QLatin1String("_"), fi.path() + QLatin1String("/i18n"), QLatin1String(".qm")))
- QCoreApplication::installTranslator(translator);
- else
- delete translator;
+
+ QScopedPointer<QTranslator> translator(new QTranslator);
+ if (!uiLanguage.isEmpty()) {
+ QLocale locale(uiLanguage);
+ if (translator->load(locale, QLatin1String("qml"), QLatin1String("_"), translationsDirectory, QLatin1String(".qm"))) {
+ if (activeTranslator)
+ QCoreApplication::removeTranslator(activeTranslator.data());
+ QCoreApplication::installTranslator(translator.data());
+ activeTranslator.swap(translator);
+ }
+ } else {
+ activeTranslator.reset();
+ }
+ q->retranslate();
#else
Q_UNUSED(rootFile)
#endif
@@ -106,7 +115,14 @@ void QQmlApplicationEnginePrivate::startLoad(const QUrl &url, const QByteArray &
{
Q_Q(QQmlApplicationEngine);
- loadTranslations(url); //Translations must be loaded before the QML file is
+ 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)
@@ -181,6 +197,7 @@ void QQmlApplicationEnginePrivate::finishLoad(QQmlComponent *c)
\list
\li Translation files must have "qml_" prefix e.g. qml_ja_JP.qm.
\endlist
+ \li Translations are reloaded when the \c QJSEngine::uiLanguage / \c Qt.uiLanguage property is changed.
\li Automatically sets an incubation controller if the scene contains a QQuickWindow.
\li Automatically sets a \c QQmlFileSelector as the url interceptor, applying file selectors to all
QML files and assets.