How to translate language in Qt on the fly

2019-01-25 19:56发布

I am using Translator and linguist to translate language in my application in Qt symbain, But I want to do this on the fly (on run time), I have tried the solution given by the developer network but it is not working for me

 #include "languagetranslator.h"
 #include "constants.h"
 #include <QSettings>
 #include <HelperClasses/Strings.h>
 #include <QTranslator>
#include <AppStatus/appstatus.h>
#include <QApplication>

QTranslator* LanguageTranslator::current = 0;

LanguageTranslator::LanguageTranslator(int& argc, char* argv[]) : QApplication(argc, argv)
{

}


LanguageTranslator::~LanguageTranslator()
{
}

void LanguageTranslator::loadTranslation()
{
    QString language;
    language = AppStatus::getCurrentLanguage();
    loadTranslation(language);
    qDebug("language from laodtranslation = %s",language.toLatin1().data());
    //qDebug(language.toLatin1());
}

void LanguageTranslator::loadTranslation(QString language)
{
    //QApplication app;
    QTranslator* translator = new QTranslator(instance());
    translator->load(language);
    installTranslator(&translator);

    //exec();

}

It only works when I declared it in main.cpp and restart the application, I am also calling this method after selecting the new language is there any thing that I am missing. I am following the following link http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications

Do I have to call the void MainWindow::changeEvent(QEvent *event) when setting new language

this is my changeEvent() method which I had declared in mainWindow from here I am calling setLanguage();

void MainWindow::changeEvent(QEvent *event)
{
    if(event->type() == QEvent::LanguageChange)
    {
        ui->retranslateUi(this);
       // retranslate();
        setLanguage();
    }
    QMainWindow::changeEvent(event);
}



void MainWindow::setLanguage()
{
    QString myLanguage;
    int iIndex =  AppStatus::getSettingsData().at(3);
    switch(iIndex)
    {
    case ENGLISH:
        myLanguage = "language_en";
        break;
    case SPANISH:
        myLanguage = "language_es";
        break;
    }
    AppStatus::saveCurrentLanguage(myLanguage);

LanguageTranslator::loadTranslation(myLanguage);
}

from setLangauge() I am calling the languageTranslator() IS there anything that I am missing; This is my main class

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

    LanguageTranslator app(argc, argv);
    QString lang = AppStatus::getCurrentLanguage();
    app.installTranslator(&translator);
    MainWindow mainWindow;
    mainWindow.setOrientation(MainWindow::ScreenOrientationLockPortrait);

    //About mainWindow;

#if defined(Q_OS_SYMBIAN)
    mainWindow.showMaximized();
#else
    mainWindow.show();

#endif

   // mainWindow.showFullScreen();
    app.connect(&app,SIGNAL(lastWindowClosed()),&app,SLOT(quit()));
    return app.exec();
}

标签: qt qt4 symbian
1条回答
可以哭但决不认输i
2楼-- · 2019-01-25 20:30

The link you gave states:

However, the basic principle is to override QWidget::changeEvent() in every widget which has to be aware of dynamic language changes.

This is required as installTranslator() will create a change event which will be sent to every single widget. This means, if you want your MainWindow to be retranslated, you have to do:

void MainWindow::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        // retranslate designer form (single inheritance approach)
        ui.retranslateUi(this);
    }

    // remember to call base class implementation
    QMainWindow::changeEvent(event);
}

You have to do this for every widget which has a translatable GUI

[Edit - Added comment as it was the actual answer to the question]

Don't call setLanguage() from within changeEvent()! You should call setLanguage() from wherever you can change the language. setLanguage() calls LanguageTranslator::loadTranslation() which calls QApplication::installTranslator() which creates a LanguageChange Event which is then caught in MainWindow::changeEvent

查看更多
登录 后发表回答