Why do I need a QTranslator for localized button texts in a QMessageBox on Windows?

193 Views Asked by At

I'm developing a Qt application that runs on Linux and Windows. I'm on a German locale.

When building it on Linux, I get the correct translations for the standard buttons displayed by e. g. a QMessageBox::question with QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel buttons set ("Ja", "Nein" and "Abbrechen" in my case).

If I build the code on Windows however, I only get the C locale (English) texts for those buttons ("Yes", "No" and "Cancel").

After some searching, I found a globally working solution by adding

#include <QTranslator>
#include <QLibraryInfo>

and

QTranslator qtTranslator;
if (qtTranslator.load(QLocale::system(), QString::fromUtf8("qt"), QString::fromUtf8("_"),
                      QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
    app.installTranslator(&qtTranslator);
}

to my main.cpp.

Using this, I also get the translated strings on Windows. On Linux however, the qtTranslator.load call fails and thus, nothing happens to the state I had before, so I ended up putting that code inside an #ifdef Q_OS_WIN block.

This works, but seems a bit hacky to me. Plus, I don't understand why I get the translated strings on Linux by default and not on Windows.

Is the way I do it the correct solution? And if so, why do I need that additional code on Windows?

1

There are 1 best solutions below

5
On

Try to replace your code on this and tested.

static QTranslator qtTr;
qtTr.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
installTranslator(&qtTr);