PDA

View Full Version : translate on fly



Noxxik
23rd February 2009, 07:38
Hi I have some problem. I want to translate my programm on fly. I have this code

void MainWindow::changeLanguage(QAction *action)
{

if(action == engLAction){
QTranslator translator;
translator.load(QString("programm_en"), "." );
qApp->installTranslator(&translator);
}
else if(action == czeLAction){
QTranslator translator;
translator.load(QString("programm_cs"), "." );
qApp->installTranslator(&translator);
}

}before make I use "lupdate programm.pro" and "lrelease programm.pro" then I translate documents in *.ts and use "lrelease programm.pro" again. Then I use make.
But when I try to change language on fly nothing happens :( Could someone help me, where I make mistake? thanks to all

caduel
23rd February 2009, 08:42
your QTranslators get destroyed when they go out of scope. You are (un)lucky this code does not crash.

^NyAw^
23rd February 2009, 09:39
Hi,

Define the QTranslator as a member variable and load the proper language. Then, when "changeLanguage" is called, you have to set all the widget texts:


widget1.setText(tr("Hello")); //This will return your translation depending on language loaded

Noxxik
23rd February 2009, 10:51
Hi,

Define the QTranslator as a member variable and load the proper language. Then, when "changeLanguage" is called, you have to set all the widget texts:
thanks :) now it is OK, but I thought, that the translation will use *.ts or *.qm files, which I generate with lupdate, lrelease.
So I think, that i dont use this *.ts and *.qm files, isnt it? :)

jpn
23rd February 2009, 11:24
See Dynamic translation.

Bjoern
26th February 2009, 14:21
So I think, that i dont use this *.ts and *.qm files, isnt it? :)
You're still requiring the qm files to be loaded and do your stuff (with global member, of course) - but the translator can only work dynamically when you're following the guides from Wiki.
That's simply because usually you setup static text once (e.g. in constructor) - where the tr-function handles the translation.
Once your text is assigned, installing a new translator will only affect any text being processed by tr-function again :o