0

I have a problem with translate standard buttons in QMessageBox. If I check the language, the buttons are translated very well, but if I don't check the language, the buttons are not translated. How can I get translated buttons without checking the language every time when I need to show QMessageBox?

#include "application.h"
#include "main_window.h"

#include <QTranslator>
#include <qlibraryinfo.h>

int main( int argc, char *argv[] )
{
  Application application( argc, argv );

  QString language = app()->settings().value("language").toString();

  if (language == "Russian") // Here I check the language
  {
    QTranslator translator_ru;
    
    if (translator_ru.load(QString("translations/qtbase_ru.qm")))
      application.installTranslator(&translator_ru);    

    if (QMessageBox::question(0, "Delete?", "First test") == QMessageBox::Yes) {} // In this message, the standard buttons are in Russian   
  }

  if (QMessageBox::question(0, "Delete?", "Second test") == QMessageBox::Yes) {} // In this message, the standard buttons are in English

  MainWindow window;
  window.show();
  return application.exec();
}
Vadim Kiselev
  • 1,030
  • 1
  • 8
  • 16
  • The code work as you write it. You enables translation only if `language == "Russian"` condition is true. If you want to have russian language as default, you should move `QTranslator translator_ru; if (translator_ru.load(QString("translations/qtbase_ru.qm"))) application.installTranslator(&translator_ru); ` out of conditional operator scope – Alexey May 05 '22 at 07:57
  • @Alexey, I don't want to have Russian language as default. I want to have language from my settings. In the second test I had installed qtbase_ru.qm, but despite it I had buttons in English. – Vadim Kiselev May 05 '22 at 08:22
  • Then you can use `QMessageBox::tr` or `Application::tr` (i suppose that Application class inherits QApplication publicly) method, like `QMessageBox::question(0, application.tr("Delete?"), application.tr("First test"))`. I did not try it myself, but i hope that helps. – Alexey May 05 '22 at 10:03
  • @Alexey, I use "tr" to translate text "Delete?", "First test, "Second test". There are no problems with it. I have problems only with tranlate text on buttons (standard buttons, i.e. OK, Cancel, Save). – Vadim Kiselev May 05 '22 at 10:41
  • Ok, than you can look at https://stackoverflow.com/questions/31533019/how-to-translate-the-buttons-in-qmessagebox – Alexey May 05 '22 at 11:05

1 Answers1

0

I've found the solution:

#include "application.h"
#include "main_window.h"

#include <QTranslator>
#include <qlibraryinfo.h>

int main( int argc, char *argv[] )
{
  Application application( argc, argv );
  QString language = app()->settings().value("language").toString();
  QString base_translate_file_name;
  if (language == "Russian") base_translate_file_name = "qtbase_ru.qm";       
  else base_translate_file_name = "qtbase_en.qm";

  QTranslator qtBaseTranslator;
  if (qtBaseTranslator.load(QString("translations/" + base_translate_file_name), application.applicationDirPath()))
  {
    application.installTranslator(&qtBaseTranslator);
  }
  MainWindow window;
  window.show();
  return application.exec();
}
Vadim Kiselev
  • 1,030
  • 1
  • 8
  • 16