diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2020-01-22 13:37:48 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2020-01-23 15:56:40 +0100 |
commit | 020a6e67766595351bcf911e965b26952a7c81b8 (patch) | |
tree | 6cbb9340cb28f18efa98c9395b782cd13b81f101 /src/qml/jsapi | |
parent | aca7b33368655949d238a0cdfc0073b6b8532872 (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/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 38 | ||||
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 7 | ||||
-rw-r--r-- | src/qml/jsapi/qjsengine_p.h | 1 |
3 files changed, 38 insertions, 8 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 065fbc1c0a..6946a64d11 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -71,11 +71,6 @@ #include <private/qqmlglobal_p.h> #include <qqmlengine.h> -#undef Q_D -#undef Q_Q -#define Q_D(blah) -#define Q_Q(blah) - Q_DECLARE_METATYPE(QList<int>) /*! @@ -257,7 +252,7 @@ Q_DECLARE_METATYPE(QList<int>) \l installExtensions(). \value TranslationExtension Indicates that translation functions (\c qsTr(), - for example) should be installed. + for example) should be installed. This also installs the Qt.uiLanguage property. \value ConsoleExtension Indicates that console functions (\c console.log(), for example) should be installed. @@ -692,7 +687,6 @@ QJSValue QJSEngine::newArray(uint length) */ QJSValue QJSEngine::newQObject(QObject *object) { - Q_D(QJSEngine); QV4::ExecutionEngine *v4 = m_v4Engine; QV4::Scope scope(v4); if (object) { @@ -719,7 +713,6 @@ QJSValue QJSEngine::newQObject(QObject *object) */ QJSValue QJSEngine::newQMetaObject(const QMetaObject* metaObject) { - Q_D(QJSEngine); QV4::ExecutionEngine *v4 = m_v4Engine; QV4::Scope scope(v4); QV4::ScopedValue v(scope, QV4::QMetaObjectWrapper::create(v4, metaObject)); @@ -1004,6 +997,35 @@ void QJSEngine::throwError(QJSValue::ErrorType errorType, const QString &message m_v4Engine->throwError(e); } +/*! + \property QJSEngine::uiLanguage + \brief the language to be used for translating user interface strings + \since 5.15 + + This property holds the name of the language to be used for user interface + string translations. It is exposed for reading and writing as \c{Qt.uiLanguage} when + the QJSEngine::TranslationExtension is installed on the engine. It is always exposed + in instances of QQmlEngine. + + You can set the value freely and use it in bindings. It is recommended to set it + after installing translators in your application. By convention, an empty string + means no translation from the language used in the source code is intended to occur. +*/ +void QJSEngine::setUiLanguage(const QString &language) +{ + Q_D(QJSEngine); + if (language == d->uiLanguage) + return; + d->uiLanguage = language; + emit uiLanguageChanged(); +} + +QString QJSEngine::uiLanguage() const +{ + Q_D(const QJSEngine); + return d->uiLanguage; +} + QJSEnginePrivate *QJSEnginePrivate::get(QV4::ExecutionEngine *e) { return e->jsEngine()->d_func(); diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index 31a4d68baa..31229e1f20 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -60,6 +60,7 @@ class Q_QML_EXPORT QJSEngine : public QObject { Q_OBJECT + Q_PROPERTY(QString uiLanguage READ uiLanguage WRITE setUiLanguage NOTIFY uiLanguageChanged) public: QJSEngine(); explicit QJSEngine(QObject *parent); @@ -121,6 +122,12 @@ public: void throwError(const QString &message); void throwError(QJSValue::ErrorType errorType, const QString &message = QString()); + QString uiLanguage() const; + void setUiLanguage(const QString &language); + +Q_SIGNALS: + void uiLanguageChanged(); + private: QJSValue create(int type, const void *ptr); diff --git a/src/qml/jsapi/qjsengine_p.h b/src/qml/jsapi/qjsengine_p.h index 164a70d000..7866a5bdda 100644 --- a/src/qml/jsapi/qjsengine_p.h +++ b/src/qml/jsapi/qjsengine_p.h @@ -107,6 +107,7 @@ public: // Shared by QQmlEngine mutable QRecursiveMutex mutex; + QString uiLanguage; // These methods may be called from the QML loader thread inline QQmlPropertyCache *cache(QObject *obj, int minorVersion = -1); |