Only grab system language from QLocale::system
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 4 Nov 2024 07:40:25 +0000 (16:40 +0900)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Fri, 22 Nov 2024 10:14:38 +0000 (11:14 +0100)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/application.cpp

index 3510bbb8601fc183da065eb5dba1fcca5e8840e3..48454bf0d8116007ab94910cb5c39339ee372971 100644 (file)
@@ -967,67 +967,60 @@ QString substLang(const QString &lang)
 
 void Application::setupTranslations()
 {
-    auto uiLanguages = QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore);
     const auto enforcedLocale = Theme::instance()->enforcedLocale();
-    if (!enforcedLocale.isEmpty()) {
-        uiLanguages.prepend(enforcedLocale);
-    }
+    const auto lang = substLang(!enforcedLocale.isEmpty() ? enforcedLocale : QLocale::system().name());
 
     auto *translator = new QTranslator(this);
     auto *qtTranslator = new QTranslator(this);
     auto *qtkeychainTranslator = new QTranslator(this);
 
-    for (auto &lang : qAsConst(uiLanguages)) {
-        lang = substLang(lang);
-        const auto trPath = applicationTrPath();
-        const auto trFolder = QDir{trPath};
-        if (!trFolder.exists()) {
-            qCWarning(lcApplication()) << trPath << "folder containing translations is missing. Impossible to load translations";
-            break;
-        }
-        const QString trFile = QLatin1String("client_") + lang;
-        qCDebug(lcApplication()) << "trying to load" << lang << "in" << trFile << "from" << trPath;
-        if (translator->load(trFile, trPath) || lang.startsWith(QLatin1String("en"))) {
-            // Permissive approach: Qt and keychain translations
-            // may be missing, but Qt translations must be there in order
-            // for us to accept the language. Otherwise, we try with the next.
-            // "en" is an exception as it is the default language and may not
-            // have a translation file provided.
-            qCInfo(lcApplication) << "Using" << lang << "translation";
-            setProperty("ui_lang", lang);
-            const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
-            const QString qtTrFile = QLatin1String("qt_") + lang;
-            const QString qtBaseTrFile = QLatin1String("qtbase_") + lang;
-            if (!qtTranslator->load(qtTrFile, qtTrPath)) {
-                if (!qtTranslator->load(qtTrFile, trPath)) {
-                    if (!qtTranslator->load(qtBaseTrFile, qtTrPath)) {
-                        if (!qtTranslator->load(qtBaseTrFile, trPath)) {
-                            qCDebug(lcApplication()) << "impossible to load Qt translation catalog" << qtBaseTrFile;
-                        }
+    const auto trPath = applicationTrPath();
+    const auto trFolder = QDir{trPath};
+    if (!trFolder.exists()) {
+        qCWarning(lcApplication()) << trPath << "folder containing translations is missing. Impossible to load translations";
+        return;
+    }
+    const QString trFile = QLatin1String("client_") + lang;
+    qCDebug(lcApplication()) << "trying to load" << lang << "in" << trFile << "from" << trPath;
+    if (translator->load(trFile, trPath) || lang.startsWith(QLatin1String("en"))) {
+        // Permissive approach: Qt and keychain translations
+        // may be missing, but Qt translations must be there in order
+        // for us to accept the language. Otherwise, we try with the next.
+        // "en" is an exception as it is the default language and may not
+        // have a translation file provided.
+        qCInfo(lcApplication) << "Using" << lang << "translation";
+        setProperty("ui_lang", lang);
+        const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
+        const QString qtTrFile = QLatin1String("qt_") + lang;
+        const QString qtBaseTrFile = QLatin1String("qtbase_") + lang;
+        if (!qtTranslator->load(qtTrFile, qtTrPath)) {
+            if (!qtTranslator->load(qtTrFile, trPath)) {
+                if (!qtTranslator->load(qtBaseTrFile, qtTrPath)) {
+                    if (!qtTranslator->load(qtBaseTrFile, trPath)) {
+                        qCDebug(lcApplication()) << "impossible to load Qt translation catalog" << qtBaseTrFile;
                     }
                 }
             }
-            const QString qtkeychainTrFile = QLatin1String("qtkeychain_") + lang;
-            if (!qtkeychainTranslator->load(qtkeychainTrFile, qtTrPath)) {
-                if (!qtkeychainTranslator->load(qtkeychainTrFile, trPath)) {
-                    qCDebug(lcApplication()) << "impossible to load QtKeychain translation catalog" << qtkeychainTrFile;
-                }
-            }
-            if (!translator->isEmpty())
-                installTranslator(translator);
-            if (!qtTranslator->isEmpty())
-                installTranslator(qtTranslator);
-            if (!qtkeychainTranslator->isEmpty())
-                installTranslator(qtkeychainTranslator);
-            break;
-        } else {
-            qCWarning(lcApplication()) << "translation catalog failed to load";
-            const auto folderContent = trFolder.entryList();
-            qCDebug(lcApplication()) << "folder content" << folderContent.join(QStringLiteral(", "));
         }
-        if (property("ui_lang").isNull()) {
-            setProperty("ui_lang", "C");
+        const QString qtkeychainTrFile = QLatin1String("qtkeychain_") + lang;
+        if (!qtkeychainTranslator->load(qtkeychainTrFile, qtTrPath)) {
+            if (!qtkeychainTranslator->load(qtkeychainTrFile, trPath)) {
+                qCDebug(lcApplication()) << "impossible to load QtKeychain translation catalog" << qtkeychainTrFile;
+            }
         }
+        if (!translator->isEmpty())
+            installTranslator(translator);
+        if (!qtTranslator->isEmpty())
+            installTranslator(qtTranslator);
+        if (!qtkeychainTranslator->isEmpty())
+            installTranslator(qtkeychainTranslator);
+    } else {
+        qCWarning(lcApplication()) << "translation catalog failed to load";
+        const auto folderContent = trFolder.entryList();
+        qCDebug(lcApplication()) << "folder content" << folderContent.join(QStringLiteral(", "));
+    }
+    if (property("ui_lang").isNull()) {
+        setProperty("ui_lang", "C");
     }
 }