[Qt-interest] How to change the language at run time in the Application

Thomas Dähling t.daehling at googlemail.com
Tue May 19 19:01:00 CEST 2009


Hi,

The website I linked ships with an example application that does exactly
what you want. So with the .pro / source files you have shown you want
to do the following refactorings:

1) Create a "translate" (or similarly named) function in your widgets
that does nothing other than calling setText / setTitle functions on the
used controls.
2) On application start up, create a list of available translations
3) Create a way to select the language to use from the available
languages (the example does it with a menu, but you could also use a
dropdown box or something).
4) When the user selects a language different from the currently
selected one, you removed the currently installed translator, install
the translator for the newly selected language and call the "translate"
function again.

/Thomas

Am Dienstag, den 19.05.2009, 19:26 +0530 schrieb Santhosh Y:
> Hi Thomas,
> 
> Thanks for the information. I have gone through it, but I am having a 
> doubt that how
> we will change a particular string in to a different language at run time.
> 
> Following is the example I am trying for. qMainwindow has some action 
> "Language 1"
> When the user clicks it in the run time, I want to change Qpushbutton 
> text to the "Language 1" equivalent.
> 
> #My project file::
> SOURCES = main.cpp
> 
> TRANSLATIONS = hellotr_lan1.ts 
> 
> 
> MainWindow :: MainWindow ()
> {
>     QMenuBar* b = menuBar ();
>     QMenu* m = b->addMenu ("Languages");
> 
>     myLan1 = new QAction(QAction::tr("Language 1", m));
>     connect(myLan1, SIGNAL(triggered () ), this, SLOT (Lang1() ));
>     m->addAction (myLan1);
> 
>     // button
>     QPushButton* hello = new QPushButton(QPushButton::tr("Hello world!"));
> 
>     setCentralWidget (hello);
> }
> 
> void
> MainWindow::Lang1 ()
> {
>     QTranslator t;
>     t.load("hellotr_lan1");
>     qApp->installTranslator(&t);
> }
> 
> 
> Please help in this regard
> 
> Santhosh.
> 
> Thomas Dähling wrote:
> > You'll have to re-implement the changeEvent() function.
> >
> > See also:
> >
> > http://wiki.qtcentre.org/index.php?title=Dynamic_translation_in_Qt4_applications
> >
> > /Thomas
> >
> > Am Dienstag, den 19.05.2009, 18:11 +0530 schrieb Santhosh Y:
> >   
> >> Hi,
> >>
> >> How to change dynamically laguage of QAction/QPushButton  items
> >>
> >> I have gone through the example linguist/hellotr/main.cpp present in 
> >> Assistant.
> >> I am able to understand the same and I able to run the example given here.
> >>
> >> My requirement is that, I should be able to change the language at run 
> >> time in a application.
> >>
> >>     
> >
> >   
> 
> 




More information about the Qt-interest-old mailing list