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



  • Hi :-)

    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 (QLibraryInfo::location(QLibraryInfo::TranslationsPath) gives me /usr/share/qt5/translations, which does not even exist) 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?

    Thanks for all answers!



  • @tleupold why don't you ship your translation files with your application?
    Use one of the QStandardPaths and load the appropriate translation file accordingly. That way its the same across all platforms.

    Also, that way your target System won't require a pre installed QtCreator/libary.



  • This post is deleted!


  • @J.Hilk This would of course be the normal way to go – in this case, the program is quite a niche solution, and for that reason I didn't write the default messages in English (as usual), but directly in German, as this program will not only be only used in Germany, but only in a very small part of it – and will definitely never have an international userbase.

    Well, probably, I'll change that nevertheless for the sake of professionality.

    I only really wonder why I get the correctly localized default texts on Linux, but not on Windows, although the correct locale is set on both systems (and e. g. a file dialog does appear localized with the Windows build).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.