如何翻译 Qt 中的标准按钮?

How to translate standard buttons in Qt?

我在翻译 QMessageBox 中的标准按钮时遇到问题。如果我检查语言,按钮翻译得很好,但如果我不检查语言,按钮就不会翻译。当我需要显示 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();
}

我找到了解决方案:

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