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/qml/v8 | |
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/qml/v8')
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 43 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 2 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index ac4b9b85b9..b1046b254b 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -1338,6 +1338,41 @@ ReturnedValue QtObject::method_createComponent(const FunctionObject *b, const Va return QV4::QObjectWrapper::wrap(scope.engine, c); } +ReturnedValue QtObject::method_get_uiLanguage(const FunctionObject *b, const Value * /*thisObject*/, const Value * /*argv*/, int /*argc*/) +{ + QV4::Scope scope(b); + QJSEngine *jsEngine = scope.engine->jsEngine(); + if (!jsEngine) + return Encode::null(); + + QQmlEnginePrivate *ep = QQmlEnginePrivate::get(scope.engine); + if (ep && ep->propertyCapture) { + static int propertyIndex = -1; + static int notifySignalIndex = -1; + if (propertyIndex < 0) { + QMetaProperty metaProperty = + QQmlEngine::staticMetaObject.property(QQmlEngine::staticMetaObject.indexOfProperty("uiLanguage")); + propertyIndex = metaProperty.propertyIndex(); + notifySignalIndex = metaProperty.notifySignalIndex(); + } + ep->propertyCapture->captureProperty(QQmlEnginePrivate::get(ep), propertyIndex, notifySignalIndex); + } + + return Encode(scope.engine->newString(QJSEnginePrivate::get(jsEngine)->uiLanguage)); +} + +ReturnedValue QtObject::method_set_uiLanguage(const FunctionObject *b, const Value * /*thisObject*/, const Value *argv, int argc) +{ + Scope scope(b); + if (!argc) + THROW_TYPE_ERROR(); + QJSEngine *jsEngine = scope.engine->jsEngine(); + if (!jsEngine) + THROW_TYPE_ERROR(); + jsEngine->setUiLanguage(argv[0].toQString()); + return Encode::undefined(); +} + #if QT_CONFIG(qml_locale) /*! \qmlmethod Qt::locale(name) @@ -1835,6 +1870,14 @@ void QV4::GlobalExtensions::init(Object *globalObject, QJSEngine::Extensions ext globalObject->defineDefaultProperty(QStringLiteral("qsTrId"), QV4::GlobalExtensions::method_qsTrId); globalObject->defineDefaultProperty(QStringLiteral("QT_TRID_NOOP"), QV4::GlobalExtensions::method_qsTrIdNoOp); + ScopedString qtName(scope, v4->newString(QStringLiteral("Qt"))); + ScopedObject qt(scope, globalObject->get(qtName)); + if (!qt) { + qt = v4->newObject(); + globalObject->defineDefaultProperty(qtName, qt); + } + qt->defineAccessorProperty(QStringLiteral("uiLanguage"), QV4::QtObject::method_get_uiLanguage, QV4::QtObject::method_set_uiLanguage); + // string prototype extension scope.engine->stringPrototype()->defineDefaultProperty(QStringLiteral("arg"), QV4::GlobalExtensions::method_string_arg); #endif diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h index d87b83ba10..5cbb52471d 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h @@ -126,6 +126,8 @@ struct QtObject : Object static ReturnedValue method_resolvedUrl(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_createQmlObject(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_createComponent(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_get_uiLanguage(const FunctionObject *b, const Value *, const Value *, int); + static ReturnedValue method_set_uiLanguage(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); #if QT_CONFIG(qml_locale) static ReturnedValue method_locale(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); #endif |