From 427646b8d7c52e5b84240e07ffd391217ce3bfa8 Mon Sep 17 00:00:00 2001
From: Shawn Rutledge <shawn.rutledge@digia.com>
Date: Thu, 4 Sep 2014 14:29:21 +0200
Subject: [PATCH] use the new form of QTranslator::load() for more flexibility
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As the docs explain, the variant of QTranslator::load() taking a
const QLocale& is better because it "uses QLocale::uiLanguages()
and not simply the locale name, which refers to the formatting of
dates and numbers and not necessarily the UI language."  And, using
a default-constructed QLocale permits QLocale::setDefault() to
override the system locale, so for example an application's
main.cpp can do that before constructing a QQmlApplicationEngine.

Task-number: QTBUG-7329
Change-Id: Ia29a4c894087c92b071c0fe484728866f2660fe6
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
---
 examples/quick/demos/photoviewer/main.cpp |  3 ++-
 src/qml/qml/qqmlapplicationengine.cpp     |  6 ++++--
 tools/qml/main.cpp                        |  6 ++----
 tools/qmlscene/main.cpp                   | 14 ++++++++------
 4 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/examples/quick/demos/photoviewer/main.cpp b/examples/quick/demos/photoviewer/main.cpp
index ca495242a4..1351f043f3 100644
--- a/examples/quick/demos/photoviewer/main.cpp
+++ b/examples/quick/demos/photoviewer/main.cpp
@@ -40,8 +40,9 @@ int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
 
+    QLocale locale;
     QTranslator qtTranslator;
-    qtTranslator.load("qml_" + QLocale::system().name(), ":/i18n/");
+    qtTranslator.load(locale, QLatin1String("qml"), QLatin1String("_"), QLatin1String(":/i18n"));
     app.installTranslator(&qtTranslator);
 
     QQmlApplicationEngine engine;
diff --git a/src/qml/qml/qqmlapplicationengine.cpp b/src/qml/qml/qqmlapplicationengine.cpp
index 240c01233b..9ccaee7cd7 100644
--- a/src/qml/qml/qqmlapplicationengine.cpp
+++ b/src/qml/qml/qqmlapplicationengine.cpp
@@ -65,7 +65,8 @@ void QQmlApplicationEnginePrivate::init()
     q->connect(q, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
 #ifndef QT_NO_TRANSLATION
     QTranslator* qtTranslator = new QTranslator;
-    if (qtTranslator->load(QLatin1String("qt_") + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
+    QLocale locale;
+    if (qtTranslator->load(locale, QLatin1String("qt"), QLatin1String("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
         QCoreApplication::installTranslator(qtTranslator);
     translators << qtTranslator;
 #endif
@@ -82,7 +83,8 @@ void QQmlApplicationEnginePrivate::loadTranslations(const QUrl &rootFile)
     QFileInfo fi(rootFile.toLocalFile());
 
     QTranslator *translator = new QTranslator;
-    if (translator->load(QLatin1String("qml_") + QLocale::system().name(), fi.path() + QLatin1String("/i18n"))) {
+    QLocale locale;
+    if (translator->load(locale, QLatin1String("qml"), QLatin1String("_"), fi.path() + QLatin1String("/i18n"))) {
         QCoreApplication::installTranslator(translator);
         translators << translator;
     } else {
diff --git a/tools/qml/main.cpp b/tools/qml/main.cpp
index 69ccd7a316..7a08562a13 100644
--- a/tools/qml/main.cpp
+++ b/tools/qml/main.cpp
@@ -450,10 +450,8 @@ int main(int argc, char *argv[])
         verboseMode = false;
 
 #ifndef QT_NO_TRANSLATION
-    //qt_ translations loaded by QQmlApplicationEngine
-    QString sysLocale = QLocale::system().name();
-
-    if (!translationFile.isEmpty()) { //Note: installed before QQmlApplicationEngine's automatic translation loading
+    // qt_ translations are loaded by QQmlApplicationEngine
+    if (!translationFile.isEmpty()) { // Note: installed before QQmlApplicationEngine's automatic translation loading
         QTranslator translator;
 
         if (translator.load(translationFile)) {
diff --git a/tools/qmlscene/main.cpp b/tools/qmlscene/main.cpp
index 208226c8aa..7c2fd9fc32 100644
--- a/tools/qmlscene/main.cpp
+++ b/tools/qmlscene/main.cpp
@@ -307,7 +307,8 @@ static void displayFileDialog(Options *options)
 #ifndef QT_NO_TRANSLATION
 static void loadTranslationFile(QTranslator &translator, const QString& directory)
 {
-    translator.load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
+    QLocale locale;
+    translator.load(locale, QLatin1String("qml"), QLatin1String("_"), directory + QLatin1String("/i18n"));
     QCoreApplication::installTranslator(&translator);
 }
 #endif
@@ -415,17 +416,18 @@ int main(int argc, char ** argv)
     app.setOrganizationDomain("qt-project.org");
 
 #ifndef QT_NO_TRANSLATION
-    QTranslator translator;
+    QLocale locale;
     QTranslator qtTranslator;
-    QString sysLocale = QLocale::system().name();
-    if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
+    if (qtTranslator.load(locale, QLatin1String("qt"), QLatin1String("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
         app.installTranslator(&qtTranslator);
-    if (translator.load(QLatin1String("qmlscene_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
+
+    QTranslator translator;
+    if (translator.load(locale, QLatin1String("qmlscene"), QLatin1String("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
         app.installTranslator(&translator);
 
     QTranslator qmlTranslator;
     if (!options.translationFile.isEmpty()) {
-        if (qmlTranslator.load(options.translationFile)) {
+        if (qmlTranslator.load(locale, options.translationFile)) {
             app.installTranslator(&qmlTranslator);
         } else {
             qWarning() << "Could not load the translation file" << options.translationFile;
-- 
GitLab