Home > Net >  How to translate standard buttons in Qt?
How to translate standard buttons in Qt?

Time:05-11

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();
}

CodePudding user response:

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();
}
  • Related