diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2017-07-12 14:39:45 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2017-07-14 10:29:49 +0000 |
commit | 61887379b0c823953b61120532055fcbd881aadd (patch) | |
tree | 979ce7e32537acbdd96fa4a5ab9b95db6f765a91 /src/qml/qml/qqmlengine.cpp | |
parent | 22a2cc43387ec3b9f74a6c01f8665378a4541147 (diff) |
Add support for QEvent::LanguageChange
Respond to the language change event by refreshing all binding
expressions. For constant string translation bindings we must now create
special QQmlBinding instances instead of a one-time property write
meta-call upon instantiation. Those however are more lightweight than an
entire JavaScript expression.
In addition this provides a slot to explicitly trigger a re-evaluation
of bindings, to make it a little easier to discover for the developer.
[ChangeLog][QtQml][QQmlEngine] Added retranslate() slot and
QEvent::LanguageChange support to refresh bindings when changing the
language at run-time.
Task-number: QTBUG-15602
Change-Id: Ide174648e1d8a5738acb88e15495018d0869d7bc
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Diffstat (limited to 'src/qml/qml/qqmlengine.cpp')
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index c0cabe4dd0..38a2978712 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -1328,6 +1328,27 @@ void QQmlEngine::setOutputWarningsToStandardError(bool enabled) } /*! + Refreshes all binding expressions that use strings marked for translation. + + Call this function after you have installed a new translator with + QCoreApplication::installTranslator, to ensure that your user-interface + shows up-to-date translations. + + \note Due to a limitation in the implementation, this function + refreshes all the engine's bindings, not only those that use strings + marked for translation. + This may be optimized in a future release. + + \since 5.10 +*/ +void QQmlEngine::retranslate() +{ + Q_D(QQmlEngine); + if (QQmlContextData *firstChildContext = QQmlContextData::get(d->rootContext)->childContexts) + firstChildContext->refreshExpressions(); +} + +/*! Returns the QQmlContext for the \a object, or 0 if no context has been set. @@ -1447,6 +1468,9 @@ bool QQmlEngine::event(QEvent *e) Q_D(QQmlEngine); if (e->type() == QEvent::User) d->doDeleteInEngineThread(); + else if (e->type() == QEvent::LanguageChange) { + retranslate(); + } return QJSEngine::event(e); } |