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?
Try to replace your code on this and tested.