From bbe7e8a45a463adbd210856bd18b50604869bf5e Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 20 Apr 2019 12:00:45 -0400 Subject: [PATCH] Use QLocale for translation search instead of custom method (#3035) Use built-in facilities of Qt to traverse QLocale::uiLanguages() to find a valid "most preferred" language, but still respect user's choice in the application settings. Fixes #3030. Fixes #1924. --- src/core/Translator.cpp | 27 ++++++++++++++------------- src/core/Translator.h | 5 +++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/Translator.cpp b/src/core/Translator.cpp index 595dadfa1..95de3ce91 100644 --- a/src/core/Translator.cpp +++ b/src/core/Translator.cpp @@ -34,13 +34,14 @@ */ void Translator::installTranslators() { + QLocale locale; QString language = config()->get("GUI/Language").toString(); - if (language == "system" || language.isEmpty()) { - language = QLocale::system().name(); - } - if (language == "en") { + if (!language.isEmpty() && language != "system") { // use actual English translation instead of the English locale source language - language = "en_US"; + if (language == "en") { + language = "en_US"; + } + locale = QLocale(language); } const QStringList paths = { @@ -51,11 +52,12 @@ void Translator::installTranslators() bool translationsLoaded = false; for (const QString& path : paths) { - translationsLoaded |= installTranslator(language, path) || installTranslator("en_US", path); + translationsLoaded |= installTranslator(locale, path) || installTranslator(QLocale("en_US"), path); if (!installQtTranslator(language, path)) { - installQtTranslator("en", path); + installQtTranslator(QLocale("en"), path); } } + if (!translationsLoaded) { // couldn't load configured language or fallback qWarning("Couldn't load translations."); @@ -114,10 +116,10 @@ QList> Translator::availableLanguages() * @param path local search path * @return true on success */ -bool Translator::installTranslator(const QString& language, const QString& path) +bool Translator::installTranslator(const QLocale& locale, const QString& path) { QScopedPointer translator(new QTranslator(qApp)); - if (translator->load(QString("keepassx_%1").arg(language), path)) { + if (translator->load(locale, "keepassx_", "", path)) { return QCoreApplication::installTranslator(translator.take()); } return false; @@ -131,13 +133,12 @@ bool Translator::installTranslator(const QString& language, const QString& path) * @param path local search path * @return true on success */ -bool Translator::installQtTranslator(const QString& language, const QString& path) +bool Translator::installQtTranslator(const QLocale& locale, const QString& path) { QScopedPointer qtTranslator(new QTranslator(qApp)); - if (qtTranslator->load(QString("qtbase_%1").arg(language), path)) { + if (qtTranslator->load(locale, "qtbase_", "", path)) { return QCoreApplication::installTranslator(qtTranslator.take()); - } else if (qtTranslator->load(QString("qtbase_%1").arg(language), - QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { + } else if (qtTranslator->load(locale, "qtbase_", "", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { return QCoreApplication::installTranslator(qtTranslator.take()); } return false; diff --git a/src/core/Translator.h b/src/core/Translator.h index cf62f48e4..cfc49d710 100644 --- a/src/core/Translator.h +++ b/src/core/Translator.h @@ -20,6 +20,7 @@ #include #include +#include class Translator { @@ -28,8 +29,8 @@ public: static QList> availableLanguages(); private: - static bool installTranslator(const QString& language, const QString& path); - static bool installQtTranslator(const QString& language, const QString& path); + static bool installTranslator(const QLocale& locale, const QString& path); + static bool installQtTranslator(const QLocale& locale, const QString& path); }; #endif // KEEPASSX_TRANSLATOR_H