[Solved] QMessageBox standard buttons untranslated, context problem?

  • I am using code from Qt's own application example for a MaybeSave dialog, which actually is a QMessageBox inheriting QDialog. The button set is defined using QMessageBox.Discard, Save, etc. enum values. The buttons are not translated into Spanish on OSX. Should they be? That is, is this a bug, or is it well known that you must setText(tr("foo")) on buttons so that they are translated?

    Looking in qt_es.ts, there are translations in the QDialog context for what I suppose are the texts for the buttons (e.g. Save, Don't Save, ...) Would these translations not be used because the context is actually QMessageBox? That is, contexts don't inherit?

    On the other hand, an enum defines a symbol in the program language, not a text to be displayed. Somewhere Qt must be converting enums into texts for buttons, and thats probably in QDialog. I suppose I should look at the code.

    I'm using PyQt, Qt5.3, OSX 10.9.

  • Reading the code, it seems like standard button texts are obtained from (delegated to) the theme in QApplication's style.

    So the problem might be that translators are not installed before QApplication translates button texts. My subclass of QApplication first calls super(), then installs translators, then instantiates widgets. It turns out this is fine, since QApplication doesn't create a static translation for standard button texts when it is initialized, but dynamically translates standard button texts later, as the buttons are created.

    The problem seems to be that QApplication translates the standard button texts in the context "QPlatformTheme", which is simply missing from qt_es.ts (Spanish), and the qt_xx.ts files for many languages.

    So one fix is simply to include a context "QPlatformTheme" in your application's .ts file, and in that context, translate all the standard buttons that your app uses. But you need to know Qt's standard text to be translated, e.g. "Discard". And you need to chunk this into your app's .ts file as part of the build, since there is no way to do it within the code and lupdate doesn't find them for you.

    Its not clear how Qt translations work if different platforms have HIG's that specify different standard button texts. For example IF the Discard standard button is labeled "Discard" according to the Win HIG but "Don't Save" according to the OSX HIG (I'm not sure that is a true example.) I suppose Qt might have a different .ts file for every ( language x platform)? Maybe those differences only exist in English, e.g. in Japanese, both OSX and Win use "gedelete" to label the Discard button.

    So probably the best solution is indeed to explicitly button.setText(tr"foo") on standard buttons in your dialogs ( a workaround suggested elsewhere.) Then you don't need to append to the .ts after you run lupdate.

    It would be nice if the Qt translations were better maintained. If you cd to qttranslations directory in the source and grep for "QPlatformTheme", you see that only a few languages: ja, ru, de and a few others are found. That seems to indicate which countries are really using Qt (where to expend marketing effort for your app.)

    Maybe its an ownership issue, and the KDE project maintains Qt translations separately?

  • Lifetime Qt Champion


    Sounds like you might need "QTranslator":http://qt-project.org/doc/qt-5/qtranslator.html

    Hope it helps

Log in to reply

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