Qt.locale() and JS locale type extension.

Task-number: QTBUG-17129

Change-Id: I69cbbe858735b750b4e37ce489f2fa1ad5d8b5d3
Reviewed-by: Martin Jones <martin.jones@nokia.com>
This commit is contained in:
Martin Jones 2011-11-10 18:33:27 +10:00 committed by Qt by Nokia
parent d2c1adc6f9
commit 88fefbc68d
26 changed files with 3024 additions and 11 deletions

View File

@ -34,6 +34,7 @@
\nextpage {QML Features}
\title QML Internationalization
\section1 Translation
Strings in QML can be marked for translation using the qsTr(), qsTranslate(),
QT_TR_NOOP(), and QT_TRANSLATE_NOOP() functions.
@ -55,14 +56,14 @@ capabilities are described more fully in:
You can test a translation with the \l {QML Viewer} using the -translation option.
\section1 Example
\section2 Example
First we create a simple QML file with text to be translated. The string
that needs to be translated is enclosed in a call to \c qsTr().
hello.qml:
\qml
import QtQuick 1.0
import QtQuick 2.0
Rectangle {
width: 200; height: 200
@ -83,6 +84,15 @@ Finally, we can test the translation:
qmlviewer -translation hello.qm hello.qml
\endcode
You can see a complete example and source code in the \l{declarative/i18n}{QML Internationalization example}.
\section1 Localization
Localization is the process of adapting to local conventions,
for example presenting dates and times using the locally preferred formats.
Qt Quick supports localization via the \l {QtQuick2::Locale}{Locale} object and extensions to the
ECMAScript \l {QtQuick2::Date}{Date} and \l {QtQuick2::Number}{Number} types.
*/

View File

@ -0,0 +1,149 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\qmlclass Date
\inqmlmodule QtQuick 2
\brief The Date object provides date functions
The QML Date object extends the JS Date object with
locale aware functions.
Functions that accept a locale format may be either an enumeration
value:
\table
\row \i Locale.LongFormat \i The long version of day and month names; for example, returning "January" as a month name.
\row \i Locale.ShortFormat \i The short version of day and month names; for example, returning "Jan" as a month name.
\row \i Locale.NarrowFormat \i A special version of day and month names for use when space is limited;
for example, returning "J" as a month name. Note that the narrow format might contain
the same text for different months and days or it can even be an empty string if the
locale doesn't support narrow names, so you should avoid using it for date formatting.
Also, for the system locale this format is the same as ShortFormat.
\endtable
or a string specifying the format:
\table
\header \i Expression \i Output
\row \i d \i the day as number without a leading zero (1 to 31)
\row \i dd \i the day as number with a leading zero (01 to 31)
\row \i ddd
\i the abbreviated localized day name (e.g. 'Mon' to 'Sun').
\row \i dddd
\i the long localized day name (e.g. 'Monday' to 'Sunday').
\row \i M \i the month as number without a leading zero (1 to 12)
\row \i MM \i the month as number with a leading zero (01 to 12)
\row \i MMM
\i the abbreviated localized month name (e.g. 'Jan' to 'Dec').
\row \i MMMM
\i the long localized month name (e.g. 'January' to 'December').
\row \i yy \i the year as two digit number (00 to 99)
\row \i yyyy \i the year as four digit number. If the year is negative,
a minus sign is prepended in addition.
\endtable
All other input characters will be ignored. Any sequence of characters that
are enclosed in singlequotes will be treated as text and not be used as an
expression. Two consecutive singlequotes ("''") are replaced by a singlequote
in the output.
Example format strings (assuming that the Date is the 20 July
1969):
\table
\header \o Format \o Result
\row \o dd.MM.yyyy \o 20.07.1969
\row \o ddd MMMM d yy \o Sun July 20 69
\row \o 'The day is' dddd \o The day is Sunday
\endtable
\sa {QtQuick2::Locale}{Locale}
*/
/*!
\qmlmethod string Date::toLocaleString(locale,format)
Converts the Date to a string containing the date and time
suitable for the specified \a locale
in the specified \a format.
If the format is not specified Locale.LongFormat will be used.
If \a locale is not specified, the default locale will be used.
The following example shows the current date and time formatted
for the German locale:
\code
import QtQuick 2.0
Text {
text: "The date is: " + Date().toLocaleString(Qt.locale("de_DE"))
}
\endcode
*/
/*!
\qmlmethod string Date::toLocaleDateString(locale,format)
Converts the Date to a string containing the date suitable for the specified \a locale
in the specified \a format.
If the format is not specified Locale.LongFormat will be used.
If \a locale is not specified, the default locale will be used.
The following example shows the current date formatted
for the German locale:
\code
import QtQuick 2.0
Text {
text: "The date is: " + Date().toLocaleDateString(Qt.locale("de_DE"))
}
\endcode
*/
/*!
\qmlmethod string Date::toLocaleTimeString(locale,format)
Converts the Date to a string containing the time suitable for the specified \a locale
in the specified \a format.
If the format is not specified Locale.LongFormat will be used.
If \a locale is not specified, the default locale will be used.
The following example shows the current time formatted
for the German locale:
\code
import QtQuick 2.0
Text {
text: "The date is: " + Date().toLocaleTimeString(Qt.locale("de_DE"))
}
\endcode
*/

View File

@ -0,0 +1,105 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\qmlclass Number
\inqmlmodule QtQuick 2
\brief The Number object provides represents a number value
The QML Number object extends the JS Number object with
locale aware functions.
\sa {QtQuick2::Locale}{Locale}
*/
/*!
\qmlmethod string Number::toLocaleString(locale,format,precision)
Converts the Number to a string suitable for the specified \a locale
in the specified \a format, with the specified \a precision.
Valid formats are:
\list
\o 'f' Decimal floating point, e.g. 248.65
\o 'e' Scientific notation using e character, e.g. 2.4865e+2
\o 'E' Scientific notation using E character, e.g. 2.4865E+2
\o 'g' Use the shorter of e or f
\o 'G' Use the shorter of E or f
\endlist
If precision is not specified, the precision will be 2.
If the format is not specified 'f' will be used.
If \a locale is not specified, the default locale will be used.
The following example shows a number formatted for the German locale:
\code
import QtQuick 2.0
Text {
text: "The value is: " + Number(4742378.423).toLocaleString(Qt.locale("de_DE"))
}
\endcode
You can apply toLocaleString() directly to constants, provided the decimal
is included in the constant, e.g.
\code
123.0.toLocaleString(Qt.locale("de_DE")) // OK
123..toLocaleString(Qt.locale("de_DE")) // OK
123.toLocaleString(Qt.locale("de_DE")) // fails
\endcode
*/
/*!
\qmlmethod string Number::toLocaleCurrencyString(locale,symbol)
Converts the Number to a currency using the currency and conventions of the specified
\a locale. If \a symbol is specified it will be used as the currency
symbol.
\sa Locale::currencySymbol()
*/
/*!
\qmlmethod string Number::fromLocaleString(locale,number)
Returns a Number by parsing \a number using the conventions of the supplied \a locale.
If \a locale is not supplied the default locale will be used.
For example, using the German locale:
\code
var german = Qt.locale("de_DE");
var d;
d = Number.fromLocaleString(german, "1234,56) // d == 1234.56
d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1234.56") // throws exception
d = Number.fromLocaleString(german, "1.234") // d == 1234.0
\endcode
*/

View File

@ -77,7 +77,7 @@ Qt applications.
\o \l{Integrating QML Code with Existing Qt UI Code}
\o \l{Dynamic Object Management in QML}{Dynamic Object Management}
\o \l{Network Transparency}{Loading Resources in QML}
\o \l{QML Internationalization}{Internationalization}
\o \l{QML Internationalization}{Qt Quick 1 Internationalization}
\endlist
\section1 QML Add-Ons

View File

@ -32,7 +32,7 @@
\contentspage QML Features
\previouspage {Network Transparency}{Loading Resources in QML}
\nextpage {QML Features}
\title QML Internationalization
\title Qt Quick 1 Internationalization
Strings in QML can be marked for translation using the qsTr(), qsTranslate(),

View File

@ -0,0 +1,132 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.0
Rectangle {
id: root
width: 320
height: 480
color: "lightgray"
property string locale: view.currentItem.locale
Text {
id: title
text: "Select locale:"
}
Rectangle {
id: chooser
anchors.top: title.bottom
anchors.topMargin: 5
width: parent.width-10
x: 5
height: parent.height/2 - 10
color: "#40300030"
ListView {
id: view
clip: true
focus: true
anchors.fill: parent
model: [
"en_US",
"en_GB",
"fi_FI",
"de_DE",
"ar_SA",
"hi_IN",
"zh_CN",
"th_TH",
"fr_FR",
"nb_NO",
"sv_SE"
]
delegate: Text {
property string locale: modelData
height: 30
width: view.width
text: Qt.locale(modelData).name + " ("+ Qt.locale(modelData).nativeCountryName + "/" + Qt.locale(modelData).nativeLanguageName + ")"
MouseArea {
anchors.fill: parent
onClicked: view.currentIndex = index
}
}
highlight: Rectangle {
height: 30
color: "#60300030"
}
}
}
Rectangle {
color: "white"
anchors.top: chooser.bottom
anchors.topMargin: 5
anchors.bottom: parent.bottom
anchors.bottomMargin: 5
x: 5; width: parent.width - 10
Column {
anchors.fill: parent
spacing: 5
Text {
property var date: new Date()
text: "Date: " + date.toLocaleDateString(Qt.locale(root.locale))
}
Text {
property var date: new Date()
text: "Time: " + date.toLocaleTimeString(Qt.locale(root.locale))
}
Text {
property var dow: Qt.locale(root.locale).firstDayOfWeek
text: "First day of week: " + Qt.locale(root.locale).standaloneDayName(dow)
}
Text {
property var num: 10023823
text: "Number: " + num.toLocaleString(Qt.locale(root.locale))
}
Text {
property var num: 10023823
text: "Currency: " + num.toLocaleCurrencyString(Qt.locale(root.locale))
}
}
}
}

View File

@ -85,6 +85,7 @@
#include <private/qdeclarativeutilmodule_p.h>
#include <private/qquickitemsmodule_p.h>
#include <private/qquickparticlesmodule_p.h>
#include <private/qdeclarativelocale_p.h>
#ifdef Q_OS_WIN // for %APPDATA%
#include <qt_windows.h>
@ -174,6 +175,7 @@ void QDeclarativeEnginePrivate::defineModule()
{
registerBaseTypes("QtQuick", 2, 0);
qmlRegisterType<QDeclarativeBinding>();
qmlRegisterUncreatableType<QDeclarativeLocale>("QtQuick",2,0,"Locale",QDeclarativeEngine::tr("Locale cannot be instantiated. Use Qt.locale()"));
}
/*!

View File

@ -81,6 +81,7 @@
#include <private/qobject_p.h>
#include <private/qv8engine_p.h>
#include <private/qjsengine_p.h>
QT_BEGIN_NAMESPACE
@ -119,7 +120,7 @@ public:
QDeclarativeJavaScriptExpressionGuard *next;
};
class Q_DECLARATIVE_EXPORT QDeclarativeEnginePrivate : public QObjectPrivate
class Q_DECLARATIVE_EXPORT QDeclarativeEnginePrivate : public QJSEnginePrivate
{
Q_DECLARE_PUBLIC(QDeclarativeEngine)
public:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,132 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QDECLARATIVELOCALE_H
#define QDECLARATIVELOCALE_H
#include <qdeclarative.h>
#include <QtCore/qlocale.h>
#include <QtCore/qobject.h>
#include <private/qv8engine_p.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class QDeclarativeDateExtension
{
public:
static void registerExtension(QV8Engine *engine);
private:
static v8::Handle<v8::Value> toLocaleString(const v8::Arguments& args);
static v8::Handle<v8::Value> toLocaleTimeString(const v8::Arguments& args);
static v8::Handle<v8::Value> toLocaleDateString(const v8::Arguments& args);
static v8::Handle<v8::Value> fromLocaleString(const v8::Arguments& args);
static v8::Handle<v8::Value> fromLocaleTimeString(const v8::Arguments& args);
static v8::Handle<v8::Value> fromLocaleDateString(const v8::Arguments& args);
};
class QDeclarativeNumberExtension
{
public:
static void registerExtension(QV8Engine *engine);
private:
static v8::Handle<v8::Value> toLocaleString(const v8::Arguments& args);
static v8::Handle<v8::Value> fromLocaleString(const v8::Arguments& args);
static v8::Handle<v8::Value> toLocaleCurrencyString(const v8::Arguments& args);
};
class Q_AUTOTEST_EXPORT QDeclarativeLocale
{
Q_GADGET
Q_ENUMS(MeasurementSystem)
Q_ENUMS(FormatType)
Q_ENUMS(CurrencySymbolFormat)
Q_ENUMS(DayOfWeek)
public:
~QDeclarativeLocale();
enum MeasurementSystem {
MetricSystem = QLocale::MetricSystem,
ImperialSystem = QLocale::ImperialSystem
};
enum FormatType {
LongFormat = QLocale::LongFormat,
ShortFormat = QLocale::ShortFormat,
NarrowFormat = QLocale::NarrowFormat
};
enum CurrencySymbolFormat {
CurrencyIsoCode = QLocale::CurrencyIsoCode,
CurrencySymbol = QLocale::CurrencySymbol,
CurrencyDisplayName = QLocale::CurrencyDisplayName
};
// Qt defines Sunday as 7, but JS Date assigns Sunday 0
enum DayOfWeek {
Sunday = 0,
Monday = Qt::Monday,
Tuesday = Qt::Tuesday,
Wednesday = Qt::Wednesday,
Thursday = Qt::Thursday,
Friday = Qt::Friday,
Saturday = Qt::Saturday
};
static v8::Handle<v8::Value> locale(QV8Engine *v8engine, const QString &lang);
private:
QDeclarativeLocale();
};
QT_END_NAMESPACE
QT_END_HEADER
#endif

View File

@ -43,6 +43,7 @@ SOURCES += \
$$PWD/qdeclarativeextensionplugin.cpp \
$$PWD/qdeclarativeimport.cpp \
$$PWD/qdeclarativelist.cpp \
$$PWD/qdeclarativelocale.cpp \
HEADERS += \
$$PWD/qdeclarativeglobal_p.h \
@ -104,7 +105,8 @@ HEADERS += \
$$PWD/qdeclarativeimport_p.h \
$$PWD/qdeclarativeextensionplugin.h \
$$PWD/qdeclarativenullablevalue_p_p.h \
$$PWD/qdeclarativescriptstring_p.h
$$PWD/qdeclarativescriptstring_p.h \
$$PWD/qdeclarativelocale_p.h \
QT += sql
include(parser/parser.pri)

View File

@ -45,6 +45,7 @@
#include <private/qdeclarativeengine_p.h>
#include <private/qdeclarativecomponent_p.h>
#include <private/qdeclarativestringconverters_p.h>
#include <private/qdeclarativelocale_p.h>
#include <private/qv8engine_p.h>
#include <QtCore/qstring.h>
@ -521,6 +522,8 @@ the possible format values as described for
If \a format is not specified, \a date is formatted using
\l {Qt::DefaultLocaleShortDate}{Qt.DefaultLocaleShortDate}.
\sa Locale
*/
v8::Handle<v8::Value> formatDate(const v8::Arguments &args)
{
@ -560,6 +563,8 @@ described for \l{QML:Qt::formatDateTime()}{Qt.formatDateTime()}.
If \a format is not specified, \a time is formatted using
\l {Qt::DefaultLocaleShortDate}{Qt.DefaultLocaleShortDate}.
\sa Locale
*/
v8::Handle<v8::Value> formatTime(const v8::Arguments &args)
{
@ -680,6 +685,8 @@ with the \a format values below to produce the following results:
\row \i "hh:mm:ss.zzz" \i 14:13:09.042
\row \i "h:m:s ap" \i 2:13:9 pm
\endtable
\sa Locale
*/
v8::Handle<v8::Value> formatDateTime(const v8::Arguments &args)
{
@ -1087,6 +1094,42 @@ v8::Handle<v8::Value> qsTrIdNoOp(const v8::Arguments &args)
return args[0];
}
/*!
\qmlmethod Qt::locale(name)
Returns a JS object representing the locale with the specified
name, which has the format "language[_territory][.codeset][@modifier]"
or "C", where:
\list
\o language is a lowercase, two-letter, ISO 639 language code,
\o territory is an uppercase, two-letter, ISO 3166 country code,
\o and codeset and modifier are ignored.
\endlist
If the string violates the locale format, or language is not a
valid ISO 369 code, the "C" locale is used instead. If country
is not present, or is not a valid ISO 3166 code, the most
appropriate country is chosen for the specified language.
\sa QtQuick2::Locale
*/
v8::Handle<v8::Value> locale(const v8::Arguments &args)
{
QString code;
if (args.Length() > 1)
V8THROW_ERROR("locale() requires 0 or 1 argument");
if (args.Length() == 1 && !args[0]->IsString())
V8THROW_TYPE("locale(): argument (locale code) must be a string");
QV8Engine *v8engine = V8ENGINE();
if (args.Length() == 1)
code = v8engine->toString(args[0]);
return QDeclarativeLocale::locale(v8engine, code);
}
} // namespace QDeclarativeBuiltinFunctions
QT_END_NAMESPACE

View File

@ -96,6 +96,7 @@ v8::Handle<v8::Value> qsTrNoOp(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrId(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrIdNoOp(const v8::Arguments &args);
v8::Handle<v8::Value> stringArg(const v8::Arguments &args);
v8::Handle<v8::Value> locale(const v8::Arguments &args);
}
QT_END_NAMESPACE

View File

@ -22,6 +22,7 @@
****************************************************************************/
#include "qjsengine.h"
#include "qjsengine_p.h"
#include "qjsvalue.h"
#include "qjsvalue_p.h"
#include "qscriptisolate_p.h"
@ -178,7 +179,7 @@ QJSEngine::QJSEngine(QObject *parent)
{
}
QJSEngine::QJSEngine(QObjectPrivate &dd, QObject *parent)
QJSEngine::QJSEngine(QJSEnginePrivate &dd, QObject *parent)
: QObject(dd, parent)
, d(new QV8Engine(this))
{

View File

@ -45,6 +45,7 @@ class QRegExp;
template <typename T>
inline T qjsvalue_cast(const QJSValue &);
class QJSEnginePrivate;
class Q_DECLARATIVE_EXPORT QJSEngine
: public QObject
{
@ -111,11 +112,12 @@ private:
friend inline bool qjsvalue_cast_helper(const QJSValue &, int, void *);
protected:
QJSEngine(QObjectPrivate &dd, QObject *parent = 0);
QJSEngine(QJSEnginePrivate &dd, QObject *parent = 0);
private:
QV8Engine *d;
Q_DISABLE_COPY(QJSEngine)
Q_DECLARE_PRIVATE(QJSEngine)
friend class QV8Engine;
};

View File

@ -0,0 +1,57 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtScript module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-ONLY$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QJSENGINE_P_H
#define QJSENGINE_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/private/qobject_p.h>
#include "qjsengine.h"
QT_BEGIN_NAMESPACE
class QJSEnginePrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QJSEngine)
public:
static QJSEnginePrivate* get(QJSEngine*e) { return e->d_func(); }
QJSEnginePrivate() {}
};
QT_END_NAMESPACE
#endif // QJSENGINE_P_H

View File

@ -47,6 +47,7 @@
#include "qv8gccallback_p.h"
#include "qv8sequencewrapper_p.h"
#include "qv8include_p.h"
#include "qjsengine_p.h"
#include "../../../3rdparty/javascriptcore/DateMath.h"
#include <private/qdeclarativebuiltinfunctions_p.h>
@ -55,6 +56,7 @@
#include <private/qdeclarativeapplication_p.h>
#include <private/qdeclarativexmlhttprequest_p.h>
#include <private/qdeclarativesqldatabase_p.h>
#include <private/qdeclarativelocale_p.h>
#include "qscript_impl_p.h"
#include "qv8domerrors_p.h"
@ -113,6 +115,7 @@ static bool ObjectComparisonCallback(v8::Local<v8::Object> lhs, v8::Local<v8::Ob
return false;
}
QV8Engine::QV8Engine(QJSEngine* qq, QJSEngine::ContextOwnership ownership)
: q(qq)
, m_engine(0)
@ -226,6 +229,7 @@ QVariant QV8Engine::toVariant(v8::Handle<v8::Value> value, int typeHint)
case QV8ObjectResource::ListModelType:
case QV8ObjectResource::Context2DType:
case QV8ObjectResource::ParticleDataType:
case QV8ObjectResource::LocaleDataType:
return QVariant();
case QV8ObjectResource::TypeType:
return m_typeWrapper.toVariant(r);
@ -565,6 +569,7 @@ void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global)
qt->Set(v8::String::New("btoa"), V8FUNCTION(btoa, this));
qt->Set(v8::String::New("atob"), V8FUNCTION(atob, this));
qt->Set(v8::String::New("resolvedUrl"), V8FUNCTION(resolvedUrl, this));
qt->Set(v8::String::New("locale"), V8FUNCTION(locale, this));
if (m_engine) {
qt->Set(v8::String::New("application"), newQObject(new QDeclarativeApplication(m_engine)));
@ -604,6 +609,9 @@ void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global)
#undef STRING_ARG
}
QDeclarativeDateExtension::registerExtension(this);
QDeclarativeNumberExtension::registerExtension(this);
qt_add_domexceptions(this);
m_xmlHttpRequestData = qt_add_qmlxmlhttprequest(this);
m_sqlDatabaseData = qt_add_qmlsqldatabase(this);

View File

@ -139,7 +139,7 @@ public:
ValueTypeType, XMLHttpRequestType, DOMNodeType, SQLDatabaseType,
ListModelType, Context2DType, Context2DStyleType, Context2DPixelArrayType,
ParticleDataType, SignalHandlerType, IncubatorType, VisualDataItemType,
SequenceType };
SequenceType, LocaleDataType };
virtual ResourceType resourceType() const = 0;
QV8Engine *engine;
@ -412,6 +412,9 @@ public:
QObject *qtObjectFromJS(v8::Handle<v8::Value> value);
QSet<int> visitedConversionObjects;
static QDateTime qtDateTimeFromJsDate(double jsDate);
protected:
QJSEngine* q;
QDeclarativeEngine *m_engine;
@ -449,7 +452,6 @@ protected:
void initializeGlobal(v8::Handle<v8::Object>);
double qtDateTimeToJsDate(const QDateTime &dt);
QDateTime qtDateTimeFromJsDate(double jsDate);
private:
typedef QScriptIntrusiveList<QJSValuePrivate, &QJSValuePrivate::m_node> ValueList;

View File

@ -5,6 +5,7 @@ SOURCES += \
HEADERS += \
$$PWD/qjsengine.h \
$$PWD/qjsengine_p.h \
$$PWD/qjsvalue.h \
$$PWD/qjsvalue_p.h \
$$PWD/qjsvalueiterator.h \

View File

@ -22,6 +22,7 @@ PUBLICTESTS += \
qdeclarativeqt \
qdeclarativetranslation \
qdeclarativexmlhttprequest \
qdeclarativelocale \
qjsvalue \
qjsvalueiterator \
qjsengine \

View File

@ -0,0 +1,45 @@
import QtQuick 2.0
QtObject {
property var locale: Qt.locale()
function setLocale(l) {
locale = Qt.locale(l)
}
function toLocaleString(fmt) {
var d = new Date(2011, 9, 7, 18, 53, 48, 345);
if (fmt < 0)
return d.toLocaleString(locale);
else
return d.toLocaleString(locale, fmt);
}
function toLocaleDateString(fmt) {
var d = new Date(2011, 9, 7, 18, 53, 48, 345);
if (fmt < 0)
return d.toLocaleDateString(locale);
else
return d.toLocaleDateString(locale, fmt);
}
function toLocaleTimeString(fmt) {
var d = new Date(2011, 9, 7, 18, 53, 48, 345);
if (fmt < 0)
return d.toLocaleTimeString(locale);
else
return d.toLocaleTimeString(locale, fmt);
}
function fromLocaleString(d,fmt) {
return Date.fromLocaleString(locale, d, fmt)
}
function fromLocaleDateString(d,fmt) {
return Date.fromLocaleDateString(locale, d, fmt)
}
function fromLocaleTimeString(d,fmt) {
return Date.fromLocaleTimeString(locale, d, fmt)
}
}

View File

@ -0,0 +1,65 @@
import QtQuick 2.0
QtObject {
property var locale: Qt.locale()
function setLocale(l) {
locale = Qt.locale(l)
}
function currencySymbol(type) {
if (type < 0)
return locale.currencySymbol()
else
return locale.currencySymbol(type)
}
function monthName(month,type) {
if (type < 0)
return locale.monthName(month)
else
return locale.monthName(month, type)
}
function standaloneMonthName(month,type) {
if (type < 0)
return locale.standaloneMonthName(month)
else
return locale.standaloneMonthName(month, type)
}
function dayName(month,type) {
if (type < 0)
return locale.dayName(month)
else
return locale.dayName(month, type)
}
function standaloneDayName(month,type) {
if (type < 0)
return locale.standaloneDayName(month)
else
return locale.standaloneDayName(month, type)
}
function dateTimeFormat(fmt) {
if (fmt < 0)
return locale.dateTimeFormat()
else
return locale.dateTimeFormat(fmt)
}
function dateFormat(fmt) {
if (fmt < 0)
return locale.dateFormat()
else
return locale.dateFormat(fmt)
}
function timeFormat(fmt) {
if (fmt < 0)
return locale.timeFormat()
else
return locale.timeFormat(fmt)
}
}

View File

@ -0,0 +1,30 @@
import QtQuick 2.0
QtObject {
property var locale: Qt.locale()
function setLocale(l) {
locale = Qt.locale(l)
}
function toLocaleString(n,fmt,prec) {
if (prec < 0)
return n.toLocaleString(locale, fmt);
else
return n.toLocaleString(locale, fmt, prec);
}
function toLocaleCurrencyString(n,symbol) {
if (symbol.length == 0)
return n.toLocaleCurrencyString(locale);
else
return n.toLocaleCurrencyString(locale, symbol);
}
function fromLocaleString(n) {
return Number.fromLocaleString(locale, n)
}
property var const1: 1234.56.toLocaleString(locale);
property var const2: 1234..toLocaleString(locale);
}

View File

@ -0,0 +1,26 @@
import QtQuick 2.0
QtObject {
property var locale: Qt.locale()
function setLocale(l) {
locale = Qt.locale(l);
}
property var name: locale.name
property var amText: locale.amText
property var pmText: locale.pmText
property var nativeLanguageName: locale.nativeLanguageName
property var nativeCountryName: locale.nativeCountryName
property var decimalPoint: locale.decimalPoint
property var groupSeparator: locale.groupSeparator
property var percent: locale.percent
property var zeroDigit: locale.zeroDigit
property var negativeSign: locale.negativeSign
property var positiveSign: locale.positiveSign
property var exponential: locale.exponential
property var firstDayOfWeek: locale.firstDayOfWeek
property var measurementSystem: locale.measurementSystem
property var textDirection: locale.textDirection
property var weekDays: locale.weekDays
}

View File

@ -0,0 +1,13 @@
CONFIG += testcase
TARGET = tst_qdeclarativelocale
macx:CONFIG -= app_bundle
SOURCES += tst_qdeclarativelocale.cpp
testDataFiles.files = data
testDataFiles.path = .
DEPLOYMENT += testDataFiles
CONFIG += parallel_test
QT += declarative testlib

File diff suppressed because it is too large Load Diff