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.
This commit is contained in:
Jonathan White 2019-04-20 12:00:45 -04:00 committed by Janek Bevendorff
parent acd6847cd4
commit bbe7e8a45a
2 changed files with 17 additions and 15 deletions

View File

@ -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<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;
@ -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;

View File

@ -20,6 +20,7 @@
#include <QPair>
#include <QString>
#include <QLocale>
class Translator
{
@ -28,8 +29,8 @@ public:
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