From 360a8c20aa1c1c6980a033956ff79bac54d246e5 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Fri, 19 Jun 2015 14:29:32 +0200 Subject: Unify handling of translations Mimic the logic implemented in installerbase/QTranslator::load() by - using QLocale().uiLanguages() - splitting up the locales into candidates (first en-US, then en ...) - implicitly assuming the default is English Task-number: QTIFW-390 Change-Id: I294288d5fc739ebf12c93a0e1a43d613b3834721 Reviewed-by: Jarek Kobus --- src/libs/installer/component.cpp | 38 +++++++++++++++++++------------ src/libs/installer/utils.cpp | 19 ++++++++++++++++ src/libs/installer/utils.h | 2 ++ src/libs/kdtools/kdupdaterupdatesinfo.cpp | 25 ++++++++++++++------ 4 files changed, 63 insertions(+), 21 deletions(-) (limited to 'src/libs') diff --git a/src/libs/installer/component.cpp b/src/libs/installer/component.cpp index dddca9f14..b49bfe3a4 100644 --- a/src/libs/installer/component.cpp +++ b/src/libs/installer/component.cpp @@ -42,6 +42,7 @@ #include "packagemanagercore.h" #include "remoteclient.h" #include "settings.h" +#include "utils.h" #include #include @@ -604,23 +605,32 @@ void Component::loadLicenses(const QString &directory, const QHashisValidLicenseTextFile(fileName)) continue; - QFileInfo fileInfo(fileName); - QFile file(QString::fromLatin1("%1%2_%3.%4").arg(directory, fileInfo.baseName(), - QLocale().name().toLower(), fileInfo.completeSuffix())); - if (!file.exists()) { - file.setFileName(QString::fromLatin1("%1%2_%3.%4").arg(directory, fileInfo.baseName(), - QLocale().name().left(2), fileInfo.completeSuffix())); + QFileInfo fileInfo(directory, fileName); + foreach (const QString &lang, QLocale().uiLanguages()) { + if (QLocale(lang).language() == QLocale::English) // we assume English is the default language + break; + + QList fileCandidates; + foreach (const QString &locale, QInstaller::localeCandidates(lang.toLower())) { + fileCandidates << QFileInfo(QString::fromLatin1("%1%2_%3.%4").arg( + directory, fileInfo.baseName(), locale, + fileInfo.completeSuffix())); + } + + auto fInfo = std::find_if(fileCandidates.constBegin(), fileCandidates.constEnd(), + [](const QFileInfo &file) { + return file.exists(); + }); + if (fInfo != fileCandidates.constEnd()) { + fileInfo = *fInfo; + break; + } } + QFile file(fileInfo.filePath()); if (!file.open(QIODevice::ReadOnly)) { - // No translated license, use untranslated file - qDebug().nospace() << "Unable to open translated license file" << file.fileName() - << ". Using untranslated fallback."; - file.setFileName(directory + fileName); - if (!file.open(QIODevice::ReadOnly)) { - throw Error(tr("Could not open the requested license file '%1'. Error: %2").arg(fileName, - file.errorString())); - } + throw Error(tr("Could not open the requested license file '%1'. Error: %2").arg( + file.fileName(), file.errorString())); } QTextStream stream(&file); stream.setCodec("UTF-8"); diff --git a/src/libs/installer/utils.cpp b/src/libs/installer/utils.cpp index eccd1602d..199c544ed 100644 --- a/src/libs/installer/utils.cpp +++ b/src/libs/installer/utils.cpp @@ -114,6 +114,23 @@ bool QInstaller::startDetached(const QString &program, const QStringList &argume return success; } +// Returns ["en-us", "en"] for "en-us" +QStringList QInstaller::localeCandidates(const QString &locale_) +{ + QStringList candidates; + QString locale = locale_; + candidates.reserve(locale.count(QLatin1Char('-'))); + forever { + candidates.append(locale); + int r = locale.lastIndexOf(QLatin1Char('-')); + if (r <= 0) + break; + locale.truncate(r); + } + return candidates; +} + + static bool verb = false; void QInstaller::setVerbose(bool v) @@ -400,3 +417,5 @@ QString QInstaller::windowsErrorString(int errorCode) } #endif + + diff --git a/src/libs/installer/utils.h b/src/libs/installer/utils.h index f0a70e11c..0fa6f2983 100644 --- a/src/libs/installer/utils.h +++ b/src/libs/installer/utils.h @@ -66,6 +66,8 @@ namespace QInstaller { QString createCommandline(const QString &program, const QStringList &arguments); #endif + QStringList INSTALLER_EXPORT localeCandidates(const QString &locale); + void INSTALLER_EXPORT setVerbose(bool v); bool INSTALLER_EXPORT isVerbose(); diff --git a/src/libs/kdtools/kdupdaterupdatesinfo.cpp b/src/libs/kdtools/kdupdaterupdatesinfo.cpp index b46d96943..67935100b 100644 --- a/src/libs/kdtools/kdupdaterupdatesinfo.cpp +++ b/src/libs/kdtools/kdupdaterupdatesinfo.cpp @@ -33,9 +33,12 @@ ****************************************************************************/ #include "kdupdaterupdatesinfo_p.h" +#include "utils.h" #include #include +#include +#include #include using namespace KDUpdater; @@ -116,6 +119,7 @@ bool UpdatesInfoData::parsePackageUpdateElement(const QDomElement &updateE) return false; UpdateInfo info; + QMap localizedDescriptions; for (int i = 0; i < updateE.childNodes().count(); i++) { QDomElement childE = updateE.childNodes().at(i).toElement(); if (childE.isNull()) @@ -141,13 +145,10 @@ bool UpdatesInfoData::parsePackageUpdateElement(const QDomElement &updateE) childE.attribute(QLatin1String("inheritVersionFrom"))); info.data[childE.tagName()] = childE.text(); } else if (childE.tagName() == QLatin1String("Description")) { - QString languageAttribute = childE.attribute(QLatin1String("xml:lang")).toLower(); - if (!info.data.contains(QLatin1String("Description")) && (languageAttribute.isEmpty())) - info.data[childE.tagName()] = childE.text(); - - // overwrite default if we have a language specific description - if (languageAttribute == QLocale().name().toLower()) - info.data[childE.tagName()] = childE.text(); + if (!childE.hasAttribute(QLatin1String("xml:lang"))) + info.data[QLatin1String("Description")] = childE.text(); + QString languageAttribute = childE.attribute(QLatin1String("xml:lang"), QLatin1String("en")); + localizedDescriptions.insert(languageAttribute.toLower(), childE.text()); } else if (childE.tagName() == QLatin1String("UpdateFile")) { info.data[QLatin1String("CompressedSize")] = childE.attribute(QLatin1String("CompressedSize")); info.data[QLatin1String("UncompressedSize")] = childE.attribute(QLatin1String("UncompressedSize")); @@ -156,6 +157,16 @@ bool UpdatesInfoData::parsePackageUpdateElement(const QDomElement &updateE) } } + QStringList candidates; + foreach (const QString &lang, QLocale().uiLanguages()) + candidates << QInstaller::localeCandidates(lang.toLower()); + foreach (const QString &candidate, candidates) { + if (localizedDescriptions.contains(candidate)) { + info.data[QLatin1String("Description")] = localizedDescriptions.value(candidate); + break; + } + } + if (!info.data.contains(QLatin1String("Name"))) { setInvalidContentError(tr("PackageUpdate element without Name")); return false; -- cgit v1.2.3