PDA

View Full Version : Help with QTranslator



aarelovich
24th July 2009, 23:33
Hi:

I'm trying my hand at a little internationalization and I wanted to start with a simple app and two languages: english and spanish. I managed to succesfully load the spanish packet and saw everything in my application as it should. Next I wanted to implement a bit of code so that the user could choose the language at the program startup since changing it on the fly is apparently pretty complicated. Since I only had two options I tried this in main.cpp.



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LanguageDialog diag; \\
diag.exec(); \\
if (diag.shouldTranslate()){\\
QTranslator translator;
translator.load(QString("mastermind_es"));
a.installTranslator(&translator);
}\\
Mastermind w;
w.show();
return a.exec();
}


And it doesn't work. Nothing gets translated no matter what I choose and I've allready checked, I effectively enter the if sentence in spanish is chosen in the diag object.
If the lines with a \\ are commented then I get my app translated (everytime) obviously.

Can anyone tell me what I'm doing wrong?

Thanks for any help in advance.

aarelovich
24th July 2009, 23:38
So I've kind of answered my own question.

The problem is that in the code I wrote the translator object only exists in the scope of the if sentence so that when it exists it dissapears.

The problem was solved by simply declaring it like this:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LanguageDialog diag; \\
diag.exec(); \\
QTranslator translator;
if (diag.shouldTranslate()){\\
translator.load(QString("mastermind_es"));
a.installTranslator(&translator);
}\\
Mastermind w;
w.show();
return a.exec();
}