QTranslator: Expose origin file of the translation

Task-number: QTBUG-36608
Change-Id: I8ef0968404370b8fc25dc8b4c1e2c9e4484ae55c
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
bb10
Kai Koehne 2020-01-23 12:02:06 +01:00
parent b1089787d1
commit dce61f0450
3 changed files with 29 additions and 2 deletions

View File

@ -316,6 +316,8 @@ public:
uint contextLength;
uint numerusRulesLength;
QString filePath;
bool do_load(const QString &filename, const QString &directory);
bool do_load(const uchar *data, qsizetype len, const QString &directory);
QString do_translate(const char *context, const char *sourceText, const char *comment,
@ -597,8 +599,10 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo
}
}
if (ok && d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength, directory))
if (ok && d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength, directory)) {
d->filePath = realname;
return true;
}
#if defined(QT_USE_MMAP)
if (used_mmap) {
@ -1091,6 +1095,8 @@ void QTranslatorPrivate::clear()
qDeleteAll(subTranslators);
subTranslators.clear();
filePath.clear();
if (QCoreApplicationPrivate::isTranslatorInstalled(q))
QCoreApplication::postEvent(QCoreApplication::instance(),
new QEvent(QEvent::LanguageChange));
@ -1132,6 +1138,21 @@ bool QTranslator::isEmpty() const
&& d->subTranslators.isEmpty();
}
/*!
\since 5.15
Returns the path of the loaded translation file.
The file path is empty if no translation was loaded yet,
the loading failed, or if the translation was not loaded
from a file.
*/
QString QTranslator::filePath() const
{
Q_D(const QTranslator);
return d->filePath;
}
QT_END_NAMESPACE
#include "moc_qtranslator.cpp"

View File

@ -63,6 +63,8 @@ public:
virtual bool isEmpty() const;
QString filePath() const;
bool load(const QString & filename,
const QString & directory = QString(),
const QString & search_delimiters = QString(),

View File

@ -126,6 +126,7 @@ void tst_QTranslator::load()
QVERIFY(tor.load(QFileInfo(filepath).baseName()));
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), filepath);
}
{
@ -136,13 +137,16 @@ void tst_QTranslator::load()
QVERIFY(tor.load((const uchar *)data.constData(), data.length()));
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), "");
}
{
QTranslator tor;
QVERIFY(tor.load(QString(":/tst_qtranslator/%1").arg(filepath)));
QString path = QString(":/tst_qtranslator/%1").arg(filepath);
QVERIFY(tor.load(path));
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), path);
}
}