summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@qt.io>2020-01-23 12:02:06 +0100
committerKai Koehne <kai.koehne@qt.io>2020-02-10 15:14:48 +0000
commitdce61f04503d6730b954b153395cc3f9ce5d7c24 (patch)
tree14f2e9667d060aa308ff546232a834dc6c69671d
parentb1089787d1dd9dd2b6b09a74923ea5f573d04bb2 (diff)
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>
-rw-r--r--src/corelib/kernel/qtranslator.cpp23
-rw-r--r--src/corelib/kernel/qtranslator.h2
-rw-r--r--tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp6
3 files changed, 29 insertions, 2 deletions
diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp
index 93d75feafa..822f96c499 100644
--- a/src/corelib/kernel/qtranslator.cpp
+++ b/src/corelib/kernel/qtranslator.cpp
@@ -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"
diff --git a/src/corelib/kernel/qtranslator.h b/src/corelib/kernel/qtranslator.h
index e7c39191e7..0c6efbbc7f 100644
--- a/src/corelib/kernel/qtranslator.h
+++ b/src/corelib/kernel/qtranslator.h
@@ -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(),
diff --git a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
index b3efa97dbd..cc0f97139f 100644
--- a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
+++ b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
@@ -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);
}
}