Add translation lookup for mimetype descriptions
Extend QMimeType::comment() to support translations from .qm files as a fallback when system-provided translations (e.g., shared-mime-info) are unavailable. Pick-to: 6.9 Task-number: QTBUG-127004 Change-Id: I23060ab7a690c774691b2d96174e77a252c047ad Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
parent
d8f34049ff
commit
2edb569773
@ -7,6 +7,7 @@
|
||||
#include "qmimetype_p.h"
|
||||
#include "qmimedatabase_p.h"
|
||||
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtCore/QLocale>
|
||||
#include <QtCore/QHashFunctions>
|
||||
@ -180,40 +181,36 @@ QString QMimeType::name() const
|
||||
\property QMimeType::comment
|
||||
\brief the description of the MIME type to be displayed on user interfaces
|
||||
|
||||
The default language (QLocale().name()) is used to select the appropriate translation.
|
||||
Returns a description for a MIME type, localized to the user's
|
||||
current language settings.
|
||||
|
||||
While this property was introduced in 5.10, the
|
||||
corresponding accessor method has always been there.
|
||||
*/
|
||||
QString QMimeType::comment() const
|
||||
{
|
||||
const auto isEnUs = [](QStringView lang) {
|
||||
// All synonyms of en_US, according to CLDR likely subtag rules:
|
||||
static constexpr QLatin1StringView usaIsh[] =
|
||||
{ "en_Latn_US"_L1, "en_US"_L1, "en_Latn"_L1, "en"_L1 };
|
||||
return std::find(std::begin(usaIsh), std::end(usaIsh), lang) != std::end(usaIsh);
|
||||
};
|
||||
const auto localeComments = QMimeDatabasePrivate::instance()->localeComments(d->name);
|
||||
|
||||
QStringList languageList = QLocale().uiLanguages(QLocale::TagSeparator::Underscore);
|
||||
qsizetype defaultIndex = languageList.indexOf(u"en_US"_s);
|
||||
|
||||
// Include the default locale as fall-back.
|
||||
if (defaultIndex >= 0) {
|
||||
// en_US is generally the default, and may be omitted from the
|
||||
// overtly-named locales in the MIME type's data (QTBUG-105007).
|
||||
++defaultIndex; // Skip over en_US.
|
||||
// That's typically followed by en_Latn_US and en (in that order):
|
||||
if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en_Latn_US")
|
||||
++defaultIndex;
|
||||
if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en")
|
||||
++defaultIndex;
|
||||
} else {
|
||||
// Absent en-US, just append it:
|
||||
defaultIndex = languageList.size();
|
||||
}
|
||||
languageList.insert(defaultIndex, u"default"_s);
|
||||
const QStringList languageList = QLocale().uiLanguages(QLocale::TagSeparator::Underscore);
|
||||
QString comment = localeComments.value(u"default"_s);
|
||||
|
||||
for (const QString &language : std::as_const(languageList)) {
|
||||
const QString lang = language == "C"_L1 ? u"en_US"_s : language;
|
||||
QString comm = localeComments.value(lang);
|
||||
if (!comm.isEmpty())
|
||||
return comm;
|
||||
QString translated = localeComments.value(lang);
|
||||
if (!translated.isEmpty())
|
||||
return translated;
|
||||
if (!comment.isEmpty() && isEnUs(lang))
|
||||
return comment; // The default entry is assumed to be in en-US
|
||||
}
|
||||
QString translated =
|
||||
QCoreApplication::translate("QMimeType", comment.toUtf8().constData());
|
||||
if (!translated.isEmpty())
|
||||
return translated;
|
||||
|
||||
// Use the mimetype name as fallback
|
||||
return d->name;
|
||||
|
Loading…
x
Reference in New Issue
Block a user