Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why do I need a QTranslator for localized button texts in a QMessageBox on Windows?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    J.HilkJ 1 Reply Last reply
    0
    • ? A Former User

      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!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      ? 1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @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.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @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).

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved