summaryrefslogtreecommitdiffstats
path: root/tools/qml/qmlruntime.cpp
diff options
context:
space:
mode:
authorWarwick Allison <warwick.allison@nokia.com>2010-05-05 03:24:25 (GMT)
committerWarwick Allison <warwick.allison@nokia.com>2010-05-05 03:24:25 (GMT)
commitc76d98da76fd687d2953f0026b16bca3c7fcf485 (patch)
treeeef6eb053421d53d9ee4935401ca5cefea635745 /tools/qml/qmlruntime.cpp
parentfde6b4cc1e720ae655ccac9d7f83c7d2dc4badc4 (diff)
downloadQt-c76d98da76fd687d2953f0026b16bca3c7fcf485.zip
Qt-c76d98da76fd687d2953f0026b16bca3c7fcf485.tar.gz
Qt-c76d98da76fd687d2953f0026b16bca3c7fcf485.tar.bz2
Allow translations without extra command-line args, and document both.
Diffstat (limited to 'tools/qml/qmlruntime.cpp')
-rw-r--r--tools/qml/qmlruntime.cpp72
1 files changed, 45 insertions, 27 deletions
diff --git a/tools/qml/qmlruntime.cpp b/tools/qml/qmlruntime.cpp
index 008f163..da31284 100644
--- a/tools/qml/qmlruntime.cpp
+++ b/tools/qml/qmlruntime.cpp
@@ -66,6 +66,7 @@
#include <QDeclarativeComponent>
#include <QWidget>
#include <QApplication>
+#include <QTranslator>
#include <QDir>
#include <QTextBrowser>
#include <QFile>
@@ -464,6 +465,7 @@ QDeclarativeViewer::QDeclarativeViewer(QWidget *parent, Qt::WindowFlags flags)
, frame_stream(0), scaleSkin(true), mb(0)
, portraitOrientation(0), landscapeOrientation(0)
, m_scriptOptions(0), tester(0), useQmlFileBrowser(true)
+ , translator(0)
{
QDeclarativeViewer::registerTypes();
setWindowTitle(tr("Qt Qml Runtime"));
@@ -937,6 +939,46 @@ void QDeclarativeViewer::launch(const QString& file_or_url)
QMetaObject::invokeMethod(this, "open", Qt::QueuedConnection, Q_ARG(QString, file_or_url));
}
+void QDeclarativeViewer::loadTranslationFile(const QString& directory)
+{
+ if (!translator) {
+ translator = new QTranslator(this);
+ QApplication::installTranslator(translator);
+ }
+
+ translator->load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
+}
+
+void QDeclarativeViewer::loadDummyDataFiles(const QString& directory)
+{
+ QDir dir(directory+"/dummydata", "*.qml");
+ QStringList list = dir.entryList();
+ for (int i = 0; i < list.size(); ++i) {
+ QString qml = list.at(i);
+ QFile f(dir.filePath(qml));
+ f.open(QIODevice::ReadOnly);
+ QByteArray data = f.readAll();
+ QDeclarativeComponent comp(canvas->engine());
+ comp.setData(data, QUrl());
+ QObject *dummyData = comp.create();
+
+ if(comp.isError()) {
+ QList<QDeclarativeError> errors = comp.errors();
+ foreach (const QDeclarativeError &error, errors) {
+ qWarning() << error;
+ }
+ if (tester) tester->executefailure();
+ }
+
+ if (dummyData) {
+ qWarning() << "Loaded dummy data:" << dir.filePath(qml);
+ qml.truncate(qml.length()-4);
+ canvas->rootContext()->setContextProperty(qml, dummyData);
+ dummyData->setParent(this);
+ }
+ }
+}
+
bool QDeclarativeViewer::open(const QString& file_or_url)
{
currentFileOrUrl = file_or_url;
@@ -966,39 +1008,15 @@ bool QDeclarativeViewer::open(const QString& file_or_url)
QString fileName = url.toLocalFile();
if (!fileName.isEmpty()) {
- QFileInfo fi(fileName);
if (fi.exists()) {
if (fi.suffix().toLower() != QLatin1String("qml")) {
qWarning() << "qml cannot open non-QML file" << fileName;
return false;
}
- QDir dir(fi.path()+"/dummydata", "*.qml");
- QStringList list = dir.entryList();
- for (int i = 0; i < list.size(); ++i) {
- QString qml = list.at(i);
- QFile f(dir.filePath(qml));
- f.open(QIODevice::ReadOnly);
- QByteArray data = f.readAll();
- QDeclarativeComponent comp(canvas->engine());
- comp.setData(data, QUrl());
- QObject *dummyData = comp.create();
-
- if(comp.isError()) {
- QList<QDeclarativeError> errors = comp.errors();
- foreach (const QDeclarativeError &error, errors) {
- qWarning() << error;
- }
- if (tester) tester->executefailure();
- }
-
- if (dummyData) {
- qWarning() << "Loaded dummy data:" << dir.filePath(qml);
- qml.truncate(qml.length()-4);
- ctxt->setContextProperty(qml, dummyData);
- dummyData->setParent(this);
- }
- }
+ QFileInfo fi(fileName);
+ loadTranslationFile(fi.path());
+ loadDummyDataFiles(fi.path());
} else {
qWarning() << "qml cannot find file:" << fileName;
return false;