From 6673a1afb304714d05ccfb0a3e257d58388f8f2f Mon Sep 17 00:00:00 2001 From: Not sure who <> Date: Tue, 12 Oct 2021 14:02:41 +0200 Subject: [PATCH] Translate desktop entries using gettext For the desktop-file-translations mechanism: https://github.com/openSUSE/desktop-file-translations --- src/core/kconfig.cpp | 13 +++++++++++++ src/core/kconfig_p.h | 1 + src/core/kconfigdata_p.h | 14 ++++++++++++++ src/core/kconfiggroup.cpp | 12 ++++++++++++ src/core/kconfiggroup.h | 5 +++++ src/core/kdesktopfile.cpp | 37 +++++++++++++++++++++++++++++++------ src/core/kdesktopfile.h | 2 ++ 7 files changed, 78 insertions(+), 6 deletions(-) Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfig.cpp =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kconfig.cpp +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfig.cpp @@ -1008,6 +1008,19 @@ void KConfigPrivate::revertEntry(const Q } } +QByteArray KConfigPrivate::lookupExactData(const QByteArray &group, const char *key, + KEntryMap::SearchFlags flags) const +{ + if (bReadDefaults) { + flags |= KEntryMap::SearchDefaults; + } + const KEntryMapConstIterator it = entryMap.findExactEntry(group, key, flags); + if (it == entryMap.constEnd()) { + return QByteArray(); + } + return it->mValue; +} + QByteArray KConfigPrivate::lookupData(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags) const { return lookupInternalEntry(group, key, flags).mValue; Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfig_p.h =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kconfig_p.h +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfig_p.h @@ -35,6 +35,7 @@ public: QString lookupData(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags, bool *expand) const; QByteArray lookupData(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags) const; KEntry lookupInternalEntry(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags) const; + QByteArray lookupExactData(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags) const; void putData(const QByteArray &group, const char *key, const QByteArray &value, KConfigBase::WriteConfigFlags flags, bool expand = false); void setEntryData(const QByteArray &group, const char *key, const QByteArray &value, KEntryMap::EntryOptions flags) Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfigdata_p.h =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kconfigdata_p.h +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfigdata_p.h @@ -192,6 +192,20 @@ public: Iterator findEntry(const QByteArray &group, const QByteArray &key = QByteArray(), SearchFlags flags = SearchFlags()); + ConstIterator findExactEntry(const QByteArray &group, const QByteArray &key = QByteArray(), + SearchFlags flags = SearchFlags()) const + { + KEntryKey theKey(group, key, false, bool(flags & SearchDefaults)); + + // try the localized key first + if (flags & SearchLocalized) { + theKey.bLocal = true; + return find(theKey); + } + return find(theKey); + } + + ConstIterator findEntry(const QByteArray &group, const QByteArray &key = QByteArray(), SearchFlags flags = SearchFlags()) const { return constFindEntry(group, key, flags); Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfiggroup.cpp =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kconfiggroup.cpp +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfiggroup.cpp @@ -1165,6 +1165,18 @@ bool KConfigGroup::hasKey(const char *ke return !config()->d_func()->lookupData(d->fullName(), key, flags).isNull(); } +bool KConfigGroup::hasTranslatedKey(const char *key) const +{ + Q_ASSERT_X(isValid(), "KConfigGroup::hasTranslatedKey", "accessing an invalid group"); + + KEntryMap::SearchFlags flags = KEntryMap::SearchLocalized; + if (config()->readDefaults()) { + flags |= KEntryMap::SearchDefaults; + } + + return !config()->d_func()->lookupExactData(d->fullName(), key, flags).isNull(); +} + bool KConfigGroup::hasKey(const QString &key) const { return hasKey(key.toUtf8().constData()); Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfiggroup.h =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kconfiggroup.h +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kconfiggroup.h @@ -707,6 +707,11 @@ public: */ QMap entryMap() const; + /** + * @internal + */ + bool hasTranslatedKey(const char *key) const; + protected: bool hasGroupImpl(const QByteArray &group) const override; KConfigGroup groupImpl(const QByteArray &b) override; Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kdesktopfile.cpp =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kdesktopfile.cpp +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kdesktopfile.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #ifndef Q_OS_WIN #include @@ -166,6 +167,33 @@ bool KDesktopFile::isAuthorizedDesktopFi return false; } +QString KDesktopFile::translatedEntry(const char *key) const +{ + Q_D(const KDesktopFile); + if (d->desktopGroup.hasTranslatedKey(key)) { + return d->desktopGroup.readEntry(key); + } + + if (d->desktopGroup.hasKey(key)) { + QString value = d->desktopGroup.readEntryUntranslated(key); + QString fName = fileName(); + fName = fName.mid(fName.lastIndexOf(QLatin1Char('/'))+1); + QString po_lookup_key = QString::fromLatin1(key) + QStringLiteral("(") + fName + QStringLiteral("): ") + value; + char *msgid = strdup(po_lookup_key.toUtf8().constData()); + const char *po_value = dgettext("desktop_translations", msgid); + + if (po_value == msgid) { + free(msgid); + return value; + } + + free(msgid); + return QString::fromUtf8(po_value); + } + + return QString(); +} + QString KDesktopFile::readType() const { Q_D(const KDesktopFile); @@ -180,20 +208,17 @@ QString KDesktopFile::readIcon() const QString KDesktopFile::readName() const { - Q_D(const KDesktopFile); - return d->desktopGroup.readEntry("Name", QString()); + return translatedEntry("Name"); } QString KDesktopFile::readComment() const { - Q_D(const KDesktopFile); - return d->desktopGroup.readEntry("Comment", QString()); + return translatedEntry("Comment"); } QString KDesktopFile::readGenericName() const { - Q_D(const KDesktopFile); - return d->desktopGroup.readEntry("GenericName", QString()); + return translatedEntry("GenericName"); } QString KDesktopFile::readPath() const Index: kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kdesktopfile.h =================================================================== --- kconfig-5.62.0git.20211128T012443~973dfd4.orig/src/core/kdesktopfile.h +++ kconfig-5.62.0git.20211128T012443~973dfd4/src/core/kdesktopfile.h @@ -252,6 +252,8 @@ public: #endif private: + QString translatedEntry(const char *) const; + Q_DISABLE_COPY(KDesktopFile) Q_DECLARE_PRIVATE(KDesktopFile)