QTranslator: Expose language of translation file

This allows for instance to show the current UI language in the UI,
or to load additional translations that match the .qm file by other
means.

This is especially useful in the case of QTranslator::load(QLocale(),
...), in which case different language and country combinations might
be tried.

Another option is to inspect the file name via QTranslator::filePath();
however, this is more error-prone, and might also miss information
(if the .qm file name doesn't have a country suffix, or no suffix at
all).

Change-Id: I6f565d53d8f50e21241ccae6c4de264747ac8f81
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
bb10
Kai Koehne 2020-01-30 11:08:50 +01:00
parent dce61f0450
commit e541a3f099
4 changed files with 25 additions and 4 deletions

View File

@ -283,7 +283,7 @@ class QTranslatorPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QTranslator)
public:
enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88, Dependencies = 0x96 };
enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88, Dependencies = 0x96, Language = 0xa7 };
QTranslatorPrivate() :
#if defined(QT_USE_MMAP)
@ -316,6 +316,7 @@ public:
uint contextLength;
uint numerusRulesLength;
QString language;
QString filePath;
bool do_load(const QString &filename, const QString &directory);
@ -833,7 +834,9 @@ bool QTranslatorPrivate::do_load(const uchar *data, qsizetype len, const QString
break;
}
if (tag == QTranslatorPrivate::Contexts) {
if (tag == QTranslatorPrivate::Language) {
language = QString::fromUtf8((const char*)data, blockLen);
} else if (tag == QTranslatorPrivate::Contexts) {
contextArray = data;
contextLength = blockLen;
} else if (tag == QTranslatorPrivate::Hashes) {
@ -1095,6 +1098,7 @@ void QTranslatorPrivate::clear()
qDeleteAll(subTranslators);
subTranslators.clear();
language.clear();
filePath.clear();
if (QCoreApplicationPrivate::isTranslatorInstalled(q))
@ -1138,6 +1142,17 @@ bool QTranslator::isEmpty() const
&& d->subTranslators.isEmpty();
}
/*!
\since 5.15
Returns the target language as stored in the translation file.
*/
QString QTranslator::language() const
{
Q_D(const QTranslator);
return d->language;
}
/*!
\since 5.15

View File

@ -63,6 +63,7 @@ public:
virtual bool isEmpty() const;
QString language() const;
QString filePath() const;
bool load(const QString & filename,

View File

@ -110,9 +110,10 @@ void tst_QTranslator::load_data()
QTest::addColumn<QString>("filepath");
QTest::addColumn<bool>("isEmpty");
QTest::addColumn<QString>("translation");
QTest::addColumn<QString>("language");
QTest::newRow("hellotr_la") << "hellotr_la.qm" << false << "Hallo Welt!";
QTest::newRow("hellotr_empty") << "hellotr_empty.qm" << true << "";
QTest::newRow("hellotr_la") << "hellotr_la.qm" << false << "Hallo Welt!" << "de";
QTest::newRow("hellotr_empty") << "hellotr_empty.qm" << true << "" << "";
}
void tst_QTranslator::load()
@ -120,6 +121,7 @@ void tst_QTranslator::load()
QFETCH(QString, filepath);
QFETCH(bool, isEmpty);
QFETCH(QString, translation);
QFETCH(QString, language);
{
QTranslator tor;
@ -127,6 +129,7 @@ void tst_QTranslator::load()
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), filepath);
QCOMPARE(tor.language(), language);
}
{
@ -138,6 +141,7 @@ void tst_QTranslator::load()
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), "");
QCOMPARE(tor.language(), language);
}
{
@ -147,6 +151,7 @@ void tst_QTranslator::load()
QCOMPARE(tor.isEmpty(), isEmpty);
QCOMPARE(tor.translate("QPushButton", "Hello world!"), translation);
QCOMPARE(tor.filePath(), path);
QCOMPARE(tor.language(), language);
}
}