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();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…