Search code examples
c++buttonlocalizationqt5qt5.4

Qt5 QDialogButtonBox localized values aren't found, though they exist in qt_xx.qm and were found in Qt4


Upgrading my application from Qt 4.8 to Qt 5.4.1, I notice that when running my application in non-English, the translated items in the "QDialogButtonBox" section of qt_xx.ts are not found. Other items, such as those in the "QWizard" section, are found and displayed correctly.

How can I get my QMessageBox's to have their buttons use the translated strings in qt_xx.qm?

I used French to demonstrate this issue (code below):

I deleted all the sections in the qt_fr.ts file except for "QDialogButtonBox" and "QWizard". I edited the translation of "Cancel" in the "QWizard" section to be sure that qt_fr.qm is used at all. Then I ran lrelease to create my qt_fr.qm.

On a Windows 7 box, in the Region and Language dialog:

  • in the Formats tab, I set the Format to "French (France)"
  • in the Keyboards and Languages tab, I set the Display Language to French.

I run my sample project with the .exe and qt_fr.qm in the same folder. In that folder I also have a copy of the necessary Qt5 dll's (can copy them from a Windows build of Qt, in qtbase\bin).

Results:

When I click the "QMessageBox" button I see a QMessageBox, with the buttons NOT translated. When I click the "QWizard" button I see a QWizard, and the Cancel button DOES have my edited translation.

The QMessageBox buttons are translated in the Qt 4 version of my application. What changed? How do I get it to work in Qt 5?

Sample project code:

btn_localize_bug757.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = btn_localize_bug757
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

main.cpp

#include "mainwindow.h"
#include <QtWidgets/QApplication>
#include <QtCore/QTranslator>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  QTranslator qtTranslator;
  qtTranslator.load("qt_fr");
  a.installTranslator(&qtTranslator);

  MainWindow w;
  w.show();

  return a.exec();
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets/QMainWindow>

#include <QtWidgets/QPushButton>

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  MainWindow(QWidget *parent = 0);
  ~MainWindow();

private slots:
  void showMessage();
  void showWizard();

private:
  QPushButton *showMessageButton;
  QPushButton *showWizardButton;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"

#include <QtWidgets/QMessageBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWizard>

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
{
  setWindowTitle("test");

  QPushButton *showMessageButton = new QPushButton("QMessageBox", this);
  connect(showMessageButton, SIGNAL(clicked()), SLOT(showMessage()));

  QPushButton *showWizardButton = new QPushButton("QWizard", this);
  connect(showWizardButton, SIGNAL(clicked()), SLOT(showWizard()));

  QVBoxLayout *layout = new QVBoxLayout;
  layout->addWidget(showMessageButton);
  layout->addWidget(showWizardButton);

  QWidget *window = new QWidget();
  window->setLayout(layout);

  setCentralWidget(window);
}

MainWindow::~MainWindow()
{

}

void MainWindow::showMessage()
{
  QMessageBox msg(this);
  msg.setWindowModality(Qt::WindowModal);
  msg.setIcon(QMessageBox::Question);
  msg.setText("test");
  msg.setStandardButtons(QMessageBox::Save |
                                         QMessageBox::Discard |
                                         QMessageBox::Cancel);
  msg.setDefaultButton(QMessageBox::Save);

  msg.exec();
}

void MainWindow::showWizard()
{
  QWizard *wiz = new QWizard();
  wiz->show();
}

qt_fr.ts:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="fr">
<context>
    <name>QDialogButtonBox</name>
    <message>
        <source>OK</source>
        <translation>OK</translation>
    </message>
    <message>
        <source>Save</source>
        <translation>Enregistrer</translation>
    </message>
    <message>
        <source>&amp;Save</source>
        <translation>Enregi&amp;strer</translation>
    </message>
    <message>
        <source>Open</source>
        <translation>Ouvrir</translation>
    </message>
    <message>
        <source>Cancel</source>
        <translation>Annuler</translation>
    </message>
    <message>
        <source>&amp;Cancel</source>
        <translation>&amp;Annuler</translation>
    </message>
    <message>
        <source>Close</source>
        <translation>Fermer</translation>
    </message>
    <message>
        <source>&amp;Close</source>
        <translation>&amp;Fermer</translation>
    </message>
    <message>
        <source>Apply</source>
        <translation>Appliquer</translation>
    </message>
    <message>
        <source>Reset</source>
        <translation>Réinitialiser</translation>
    </message>
    <message>
        <source>Help</source>
        <translation>Aide</translation>
    </message>
    <message>
        <source>Don&apos;t Save</source>
        <translation>Ne pas enregistrer</translation>
    </message>
    <message>
        <source>Discard</source>
        <translation>Ne pas enregistrer</translation>
    </message>
    <message>
        <source>&amp;Yes</source>
        <translation>&amp;Oui</translation>
    </message>
    <message>
        <source>Yes to &amp;All</source>
        <translation>Oui à &amp;tout</translation>
    </message>
    <message>
        <source>&amp;No</source>
        <translation>&amp;Non</translation>
    </message>
    <message>
        <source>N&amp;o to All</source>
        <translation>Non à to&amp;ut</translation>
    </message>
    <message>
        <source>Save All</source>
        <translation>Tout Enregistrer</translation>
    </message>
    <message>
        <source>Abort</source>
        <translation>Abandonner</translation>
    </message>
    <message>
        <source>Retry</source>
        <translation>Réessayer</translation>
    </message>
    <message>
        <source>Ignore</source>
        <translation>Ignorer</translation>
    </message>
    <message>
        <source>Restore Defaults</source>
        <translation>Restaurer les valeurs par défaut</translation>
    </message>
    <message>
        <source>Close without Saving</source>
        <translation>Fermer sans enregistrer</translation>
    </message>
    <message>
        <source>&amp;OK</source>
        <translation>&amp;OK</translation>
    </message>
</context>
<context>
    <name>QWizard</name>
    <message>
        <source>Go Back</source>
        <translation>Précédent</translation>
    </message>
    <message>
        <source>Continue</source>
        <translation>Continuer</translation>
    </message>
    <message>
        <source>Commit</source>
        <translatorcomment>si il s&apos;agit de commit au même sens que git... (en même temps se marier en cliquant... ?!!?!)</translatorcomment>
        <translation>Soumettre</translation>
    </message>
    <message>
        <source>Done</source>
        <translation>Terminer</translation>
    </message>
    <message>
        <source>Quit</source>
        <translation type="obsolete">Quitter</translation>
    </message>
    <message>
        <source>Help</source>
        <translation>Aide</translation>
    </message>
    <message>
        <source>&lt; &amp;Back</source>
        <translation>&lt; &amp;Précédent</translation>
    </message>
    <message>
        <source>&amp;Finish</source>
        <translation>&amp;Terminer</translation>
    </message>
    <message>
        <source>Cancel</source>
        <translation>testAnnuler</translation>
    </message>
    <message>
        <source>&amp;Help</source>
        <translation>&amp;Aide</translation>
    </message>
    <message>
        <source>&amp;Next</source>
        <translation>&amp;Suivant &gt;</translation>
    </message>
    <message>
        <source>&amp;Next &gt;</source>
        <translation>&amp;Suivant &gt;</translation>
    </message>
</context>
</TS>

Solution

  • Qt translation files have been modified in previous versions of Qt, and nobody bothered updating french support. It is now done, and hopefully released in Qt 5.5.0 :

    https://codereview.qt-project.org/#/c/111230/

    Note that the qt_fr.qm is obsolete and empty, and you should load qtbase_fr.qm, following the new Qt5 repository architecture.

    qtcreator translation still needs work, you can contribute to its translation on Transifex, look for qttraductions project.