From d8fadcbf162e68aa1e5c70787792933d14c296b5 Mon Sep 17 00:00:00 2001 From: Charles Yin Date: Fri, 29 Jun 2012 10:15:16 +1000 Subject: [PATCH] Add I18N support to qmlscene Task-number: QTBUG-22391 Change-Id: Id567d4aabf47ce4790cf33979c4e464ba4f09357 Reviewed-by: Yunqiao Yin --- tools/qmlscene/main.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tools/qmlscene/main.cpp b/tools/qmlscene/main.cpp index f0f262c..058f9cd 100644 --- a/tools/qmlscene/main.cpp +++ b/tools/qmlscene/main.cpp @@ -62,6 +62,8 @@ #include #endif +#include +#include #ifdef QML_RUNTIME_TESTING class RenderStatistics @@ -165,6 +167,7 @@ struct Options bool slowAnimations; bool quitImmediately; bool resizeViewToRootItem; + QString translationFile; }; #if defined(QMLSCENE_BUNDLE) @@ -303,6 +306,12 @@ static void displayFileDialog(Options *options) #endif } +static void loadTranslationFile(QTranslator &translator, const QString& directory) +{ + translator.load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n")); + QApplication::installTranslator(&translator); +} + static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory) { QDir dir(directory+"/dummydata", "*.qml"); @@ -346,6 +355,7 @@ static void usage() qWarning(" --quit .................................... Quit immediately after starting"); qWarning(" -I ................................. Add to the list of import paths"); qWarning(" -B .......................... Add a named bundle"); + qWarning(" -translation ........... set the language to run in"); qWarning(" "); exit(1); @@ -376,6 +386,8 @@ int main(int argc, char ** argv) options.slowAnimations = true; else if (lowerArgument == QLatin1String("--quit")) options.quitImmediately = true; + else if (lowerArgument == QLatin1String("-translation")) + options.translationFile = QLatin1String(argv[++i]); else if (lowerArgument == QLatin1String("--resize-to-root")) options.resizeViewToRootItem = true; else if (lowerArgument == QLatin1String("-i") && i + 1 < argc) @@ -401,6 +413,27 @@ int main(int argc, char ** argv) app.setOrganizationName("Nokia"); app.setOrganizationDomain("nokia.com"); + QTranslator translator; + QTranslator qtTranslator; + QString sysLocale = QLocale::system().name(); + if (translator.load(QLatin1String("qmlscene_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { + app.installTranslator(&translator); + if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { + app.installTranslator(&qtTranslator); + } else { + app.removeTranslator(&translator); + } + } + + QTranslator qmlTranslator; + if (!options.translationFile.isEmpty()) { + if (qmlTranslator.load(options.translationFile)) { + app.installTranslator(&qmlTranslator); + } else { + qWarning() << "Could not load the translation file" << options.translationFile; + } + } + QUnifiedTimer::instance()->setSlowModeEnabled(options.slowAnimations); if (options.file.isEmpty()) @@ -416,6 +449,7 @@ int main(int argc, char ** argv) if (!options.file.isEmpty()) { if (!options.versionDetection || checkVersion(options.file)) { + QTranslator translator; QQuickView qxView; engine = qxView.engine(); for (int i = 0; i < imports.size(); ++i) @@ -424,6 +458,7 @@ int main(int argc, char ** argv) engine->addNamedBundle(bundles.at(i).first, bundles.at(i).second); if (options.file.isLocalFile()) { QFileInfo fi(options.file.toLocalFile()); + loadTranslationFile(translator, fi.path()); loadDummyDataFiles(*engine, fi.path()); } qxView.setSource(options.file); @@ -466,4 +501,3 @@ int main(int argc, char ** argv) return exitCode; } - -- 2.7.4