Skip to content

Commit

Permalink
Use QLocale for translation search instead of custom method (#3035)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
droidmonkey authored and phoerious committed Apr 20, 2019
1 parent acd6847 commit bbe7e8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
27 changes: 14 additions & 13 deletions src/core/Translator.cpp
Expand Up @@ -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 = {
Expand All @@ -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.");
Expand Down Expand Up @@ -114,10 +116,10 @@ QList<QPair<QString, QString>> 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<QTranslator> 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;
Expand All @@ -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<QTranslator> 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;
Expand Down
5 changes: 3 additions & 2 deletions src/core/Translator.h
Expand Up @@ -20,6 +20,7 @@

#include <QPair>
#include <QString>
#include <QLocale>

class Translator
{
Expand All @@ -28,8 +29,8 @@ class Translator
static QList<QPair<QString, QString>> 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

0 comments on commit bbe7e8a

Please sign in to comment.