Make QLocale::uiLanguages() available in Qt.locale() object
authorMartin Jones <martin.jones@nokia.com>
Fri, 17 Feb 2012 00:11:43 +0000 (10:11 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 17 Feb 2012 03:29:42 +0000 (04:29 +0100)
Available as uiLanguages property returning an array of strings.

Change-Id: Ic698bbaff2eb0f9f6720ae06952c12a987298964
Reviewed-by: Glenn Watson <glenn.watson@nokia.com>
src/declarative/qml/qdeclarativelocale.cpp
tests/auto/declarative/qdeclarativelocale/data/properties.qml
tests/auto/declarative/qdeclarativelocale/tst_qdeclarativelocale.cpp

index fc6017c..8ec51b0 100644 (file)
@@ -556,6 +556,19 @@ static v8::Handle<v8::Value> locale_get_weekDays(v8::Local<v8::String>, const v8
     return result;
 }
 
+static v8::Handle<v8::Value> locale_get_uiLanguages(v8::Local<v8::String>, const v8::AccessorInfo &info)
+{
+    GET_LOCALE_DATA_RESOURCE(info.This());
+
+    QStringList langs = r->locale.uiLanguages();
+    v8::Handle<v8::Array> result = v8::Array::New(langs.size());
+    for (int i = 0; i < langs.size(); ++i) {
+        result->Set(i, r->engine->toString(langs.at(i)));
+    }
+
+    return result;
+}
+
 static v8::Handle<v8::Value> locale_currencySymbol(const v8::Arguments &args)
 {
     GET_LOCALE_DATA_RESOURCE(args.This());
@@ -717,6 +730,7 @@ QV8LocaleDataDeletable::QV8LocaleDataDeletable(QV8Engine *engine)
     ft->PrototypeTemplate()->SetAccessor(v8::String::New("weekDays"), locale_get_weekDays);
     ft->PrototypeTemplate()->SetAccessor(v8::String::New("measurementSystem"), locale_get_measurementSystem);
     ft->PrototypeTemplate()->SetAccessor(v8::String::New("textDirection"), locale_get_textDirection);
+    ft->PrototypeTemplate()->SetAccessor(v8::String::New("uiLanguages"), locale_get_uiLanguages);
 
     constructor = qPersistentNew(ft->GetFunction());
 }
@@ -998,6 +1012,17 @@ v8::Handle<v8::Value> QDeclarativeLocale::locale(QV8Engine *v8engine, const QStr
     \sa firstDayOfWeek
 */
 
+/*!
+    \qmlproperty Array<string> QtQuick2::Locale::uiLanguages
+
+    Returns an ordered list of locale names for translation purposes in
+    preference order.
+
+    The return value represents locale names that the user expects to see the
+    UI translation in.
+
+    The first item in the list is the most preferred one.
+*/
 
 /*!
     \qmlproperty enumeration QtQuick2::Locale::textDirection
index 1d2968b..16d1f40 100644 (file)
@@ -23,4 +23,5 @@ QtObject {
     property var measurementSystem: locale.measurementSystem
     property var textDirection: locale.textDirection
     property var weekDays: locale.weekDays
+    property var uiLanguages: locale.uiLanguages
 }
index ab11dbc..5afc235 100644 (file)
@@ -70,6 +70,8 @@ private slots:
     void standaloneDayName();
     void weekDays_data();
     void weekDays();
+    void uiLanguages_data();
+    void uiLanguages();
     void dateFormat_data();
     void dateFormat();
     void dateTimeFormat_data();
@@ -463,6 +465,45 @@ void tst_qdeclarativelocale::weekDays()
     delete obj;
 }
 
+void tst_qdeclarativelocale::uiLanguages_data()
+{
+    QTest::addColumn<QString>("locale");
+
+    QTest::newRow("en_US") << "en_US";
+    QTest::newRow("de_DE") << "de_DE";
+    QTest::newRow("ar_SA") << "ar_SA";
+    QTest::newRow("hi_IN") << "hi_IN";
+    QTest::newRow("zh_CN") << "zh_CN";
+    QTest::newRow("th_TH") << "th_TH";
+}
+
+void tst_qdeclarativelocale::uiLanguages()
+{
+    QFETCH(QString, locale);
+
+    QDeclarativeComponent c(&engine, testFileUrl("properties.qml"));
+
+    QObject *obj = c.create();
+    QVERIFY(obj);
+
+    QMetaObject::invokeMethod(obj, "setLocale", Qt::DirectConnection,
+        Q_ARG(QVariant, QVariant(locale)));
+
+    QVariant val = obj->property("uiLanguages");
+    QVERIFY(val.type() == QVariant::List);
+
+    QList<QVariant> qmlLangs = val.toList();
+    QStringList langs = QLocale(locale).uiLanguages();
+
+    QVERIFY(langs.count() == qmlLangs.count());
+
+    for (int i = 0; i < langs.count(); ++i) {
+        QCOMPARE(langs.at(i), qmlLangs.at(i).toString());
+    }
+
+    delete obj;
+}
+
 
 void tst_qdeclarativelocale::dateTimeFormat_data()
 {