Refactor Translator class to load qtbase translations from local directory if available

This commit is contained in:
Janek Bevendorff 2017-12-20 00:18:42 +01:00
parent b807215aec
commit 5b525eb9ca
3 changed files with 59 additions and 55 deletions

View File

@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de> * Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -21,33 +22,45 @@
#include <QDir> #include <QDir>
#include <QLibraryInfo> #include <QLibraryInfo>
#include <QLocale> #include <QLocale>
#include <QRegExp>
#include <QTranslator> #include <QTranslator>
#include <QRegularExpression>
#include "config-keepassx.h" #include "config-keepassx.h"
#include "core/Config.h" #include "core/Config.h"
#include "core/FilePath.h" #include "core/FilePath.h"
void Translator::installTranslator() /**
* Install all KeePassXC and Qt translators.
*/
void Translator::installTranslators()
{ {
QString language = config()->get("GUI/Language").toString(); QString language = config()->get("GUI/Language").toString();
if (language == "system" || language.isEmpty()) { if (language == "system" || language.isEmpty()) {
language = QLocale::system().name(); language = QLocale::system().name();
} }
if (!installTranslator(language)) { const QStringList paths = {
// English fallback still needs translations for plurals #ifdef QT_DEBUG
if (!installTranslator("en_plurals")) { QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR),
qWarning("Couldn't load translations."); #endif
} filePath()->dataPath("translations")
};
bool translationsLoaded = false;
for (const QString& path : paths) {
translationsLoaded |= installTranslator(language, path) || installTranslator("en_plurals", path);
installQtTranslator(language, path);
}
if (!translationsLoaded) {
// couldn't load configured language or fallback
qWarning("Couldn't load translations.");
} }
installQtTranslator(language);
availableLanguages();
} }
QList<QPair<QString, QString> > Translator::availableLanguages() /**
* @return list of pairs of available language codes and names
*/
QList<QPair<QString, QString>> Translator::availableLanguages()
{ {
const QStringList paths = { const QStringList paths = {
#ifdef QT_DEBUG #ifdef QT_DEBUG
@ -59,12 +72,13 @@ QList<QPair<QString, QString> > Translator::availableLanguages()
QList<QPair<QString, QString> > languages; QList<QPair<QString, QString> > languages;
languages.append(QPair<QString, QString>("system", "System default")); languages.append(QPair<QString, QString>("system", "System default"));
QRegExp regExp("keepassx_([a-zA-Z_]+)\\.qm", Qt::CaseInsensitive, QRegExp::RegExp2); QRegularExpression regExp("^keepassx_([a-zA-Z_]+)\\.qm$", QRegularExpression::CaseInsensitiveOption);
for (const QString& path : paths) { for (const QString& path : paths) {
const QStringList fileList = QDir(path).entryList(); const QStringList fileList = QDir(path).entryList();
for (const QString& filename : fileList) { for (const QString& filename : fileList) {
if (regExp.exactMatch(filename)) { QRegularExpressionMatch match = regExp.match(filename);
QString langcode = regExp.cap(1); if (match.hasMatch()) {
QString langcode = match.captured(1);
if (langcode == "en_plurals") { if (langcode == "en_plurals") {
langcode = "en"; langcode = "en";
} }
@ -85,46 +99,37 @@ QList<QPair<QString, QString> > Translator::availableLanguages()
return languages; return languages;
} }
bool Translator::installTranslator(const QString& language) /**
* Install KeePassXC translator.
*
* @param language translator language
* @param path local search path
* @return true on success
*/
bool Translator::installTranslator(const QString& language, const QString& path)
{ {
const QStringList paths = { QScopedPointer<QTranslator> translator(new QTranslator(qApp));
#ifdef QT_DEBUG if (translator->load(QString("keepassx_%1").arg(language), path)) {
QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR), return QCoreApplication::installTranslator(translator.take());
#endif
filePath()->dataPath("translations")
};
for (const QString& path : paths) {
if (installTranslator(language, path)) {
return true;
}
} }
return false; return false;
} }
bool Translator::installTranslator(const QString& language, const QString& path) /**
* Install Qt5 base translator from the specified local search path or the default system path
* if no qtbase_* translations were found at the local path.
*
* @param language translator language
* @param path local search path
* @return true on success
*/
bool Translator::installQtTranslator(const QString& language, const QString& path)
{ {
QTranslator* translator = new QTranslator(qApp); QScopedPointer<QTranslator> qtTranslator(new QTranslator(qApp));
if (translator->load(QString("keepassx_").append(language), path)) { if (qtTranslator->load(QString("qtbase_%1").arg(language), path)) {
QCoreApplication::installTranslator(translator); return QCoreApplication::installTranslator(qtTranslator.take());
return true; } else if (qtTranslator->load(QString("qtbase_%1").arg(language), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
} return QCoreApplication::installTranslator(qtTranslator.take());
else {
delete translator;
return false;
}
}
bool Translator::installQtTranslator(const QString& language)
{
QTranslator* qtTranslator = new QTranslator(qApp);
if (qtTranslator->load(QString("%1/qtbase_%2").arg(QLibraryInfo::location(QLibraryInfo::TranslationsPath), language))) {
QCoreApplication::installTranslator(qtTranslator);
return true;
}
else {
delete qtTranslator;
return false;
} }
return false;
} }

View File

@ -24,13 +24,12 @@
class Translator class Translator
{ {
public: public:
static void installTranslator(); static void installTranslators();
static QList<QPair<QString, QString> > availableLanguages(); static QList<QPair<QString, QString>> availableLanguages();
private: private:
static bool installTranslator(const QString& language);
static bool installTranslator(const QString& language, const QString& path); static bool installTranslator(const QString& language, const QString& path);
static bool installQtTranslator(const QString& language); static bool installQtTranslator(const QString& language, const QString& path);
}; };
#endif // KEEPASSX_TRANSLATOR_H #endif // KEEPASSX_TRANSLATOR_H

View File

@ -99,7 +99,7 @@ int main(int argc, char** argv)
Config::createConfigFromFile(parser.value(configOption)); Config::createConfigFromFile(parser.value(configOption));
} }
Translator::installTranslator(); Translator::installTranslators();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
// Don't show menu icons on OSX // Don't show menu icons on OSX