diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp index 2e853dde5f..034cddb663 100644 --- a/src/corelib/kernel/qtranslator.cpp +++ b/src/corelib/kernel/qtranslator.cpp @@ -603,9 +603,14 @@ bool QTranslatorPrivate::do_load(const QString &realname, const QString &directo } } - if (ok && d->do_load(reinterpret_cast(d->unmapPointer), d->unmapLength, directory)) { - d->filePath = realname; - return true; + if (ok) { + const QString base_dir = + !directory.isEmpty() ? directory : QFileInfo(realname).absolutePath(); + if (d->do_load(reinterpret_cast(d->unmapPointer), d->unmapLength, + base_dir)) { + d->filePath = realname; + return true; + } } #if defined(QT_USE_MMAP) diff --git a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp index ef84dfd07e..6ce223cbd2 100644 --- a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp +++ b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp @@ -48,6 +48,7 @@ protected: bool eventFilter(QObject *obj, QEvent *event) override; private slots: void initTestCase(); + void init(); void load_data(); void load(); @@ -75,6 +76,10 @@ void tst_QTranslator::initTestCase() { dataDir = QEXTRACTTESTDATA(QStringLiteral("/tst_qtranslator")); QVERIFY2(!dataDir.isNull(), qPrintable("Could not extract test data")); +} + +void tst_QTranslator::init() +{ QVERIFY2(QDir::setCurrent(dataDir->path()), qPrintable("Could not chdir to " + dataDir->path())); } @@ -355,6 +360,15 @@ void tst_QTranslator::dependencies() QVERIFY(!tor.isEmpty()); QCOMPARE(tor.translate("QPushButton", "Hello world!"), QLatin1String("Hallo Welt!")); } + + { + // Test resolution of paths relative to main file + const QString absoluteFile = QFileInfo("dependencies_la").absoluteFilePath(); + QDir::setCurrent(QDir::tempPath()); + QTranslator tor; + QVERIFY(tor.load(absoluteFile)); + QVERIFY(!tor.isEmpty()); + } } struct TranslateThread : public QThread