0

I have a simple app that first asks for language (I have already done the qm and ts files) and it works:

//main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator T;
    QStringList langs;
    langs << "Espanol" << "English" << "Deutsch";
    const QString lang = QInputDialog::getItem(NULL, "Language", "Select a language", langs);
    if (lang == "English"){
        T.load(":/ingles.qm");
    } else if (lang == "Deutsch"){
        T.load(":/aleman.qm");
    }
    if (lang != "Espanol"){
        a.installTranslator(&T);
    }

    a.setStyle("fusion");
    MainWindow w;
    w.show();

    return a.exec();
}

But I want to change the language with radioButtons inside the MainWindow, is it posible for example to do it in the mainwindow.cpp in?:

void MainWindow::on_botoncambiaridioma_clicked()
{
    if (ui->radioButton_2->isChecked()){
      
    }

    if (ui->radioButton_3->isChecked()){
       
    }
}

1 Answers1

0

First, you should remove the previous language, and then install the current language

void CTest::ChangeLanguage(const QString& language)
{
    QString qmPath;

    if (0 == QString::compare("English", language, Qt::CaseInsensitive))
    {
        qmPath = qApp->applicationDirPath() + "/translations/translation_english.qm";
    }
    else if (0 == QString::compare("Deutsch", language, Qt::CaseInsensitive))
    {
        qmPath = qApp->applicationDirPath() + "/translations/translation_deutsch.qm";
    }
    else if (0 == QString::compare("Espanol", language, Qt::CaseInsensitive))
    {
        qmPath = qApp->applicationDirPath() + "/translations/translation_espanol.qm";
    }

    if (!m_pTranslator.isNull())
    {
        qDebug() << "remove" << qApp->removeTranslator(m_pTranslator.data()) << QDateTime::currentDateTime();
    }

    m_pTranslator = QSharedPointer<QTranslator>(new QTranslator());
    qDebug() << qmPath << "load" << m_pTranslator->load(qmPath);
    qDebug() << "install" << qApp->installTranslator(m_pTranslator.data()) << QDateTime::currentDateTime();
}

Then, you should also respond to the corresponding event

void MainWindow::changeEvent(QEvent* event)
{
    switch(event->type())
    {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
    }

    return QMainWindow::changeEvent(event);
}