How to implement multiple Languages on an application from inside the application?
Hello every one.
I'm very new to this Qt environment. I have made one mobile application for Symbian device which implements languages from out side of the application. That means when the application will start it will see the OS language and according to that it will select the appropriate ".qm" file and install it for the application. But my requirement is to select the language from inside of the application and load it throughout the application.
My question is, Is it possible in Qt? And if How?
Thanks in advance for your support.
Re: How to implement multiple Languages on an application from inside the application
Yes. You do it exactly the same way but rather than use QLocale to detect the language, you ask the user to choose from the list of available translations.
Re: How to implement multiple Languages on an application from inside the application
I'm trying the same thing, means I tried to select a language from a combo box. It is returning me a file name. I have coded to install it on qApp object. I've written following code segments to work this. It is not working for me. So please check and rectify the logic.
Code:
[QUOTE]main.cpp[/QUOTE]
//installing the current language of the device before starting the application.
app.installTranslator(&curTranslator);
MainWindow mainWindow;//main application starts from here
mainWindow.showFullScreen();
return app.exec();
[QUOTE]Mainwindow.cpp[/QUOTE]
void MainWindow::on_Language_cb_currentIndexChanged(int index)
{
switch(index)
{
case 0: langAbbr="en";break;
case 1: langAbbr="ar";break;
case 2: langAbbr="fr";break;
default: langAbbr="en";
}
qApp->removeTranslator(&curTranslator);
qDebug() << "current translation file"<<tempStr; //shows the name of the file.
curTranslator.load(tempStr);// not sure that whether it is loading the file or not...
qApp->installTranslator(&curTranslator); //it should install the selected language on the whole application..
}
Re: How to implement multiple Languages on an application from inside the application
What is "not working"? What does QTranslator::load() return?
Re: How to implement multiple Languages on an application from inside the application
Code:
curTranslator.load(tempStr);// not sure that whether it is loading the file or not...
It was giving 'false' as no path was there.
Now I modified that segment to something like following code
Code:
void MainWindow::on_Language_cb_currentIndexChanged(int index)
{
switch(index)
{
case 0: langAbbr="en";break;
case 1: langAbbr="ar";break;
case 2: langAbbr="fr";break;
default: langAbbr="en";
}
bool loader=curTranslator.load(tempStr,"D:/Test-to-device/17-Jul-2012/proj");
qDebug() << "current translation file"<<tempStr<<"file loaded"<<loader;
qApp->installTranslator(&curTranslator);//till this line it is working fine. and showing me the correct language file is loading
ui->retranslateUi(this);//if I include this line then out put is coming like the followings
}
Quote:
current translation file "proj_fr.qm" file loaded true
`current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false `
/*these lines between back quotes are repeated more than 30 times*/
The program has unexpectedly finished.
D:\Test-to-device\17-jul-2012\debug\proj.exe exited with code -1073741571
Re: How to implement multiple Languages on an application from inside the application
Re: How to implement multiple Languages on an application from inside the application
wysota, My problem is that while selecting the language "french", the qDebug() should show me only one line
Quote:
current translation file "proj_fr.qm" file loaded true
not anything else.
And while I'm writing code segment
Code:
qApp->installTranslator(&curTranslator);
ui->retranslateUi(this);
It should show me the effect of the language change on my UI which is not happening. Instead qDebug() is showing
Quote:
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
current translation file "proj_en.qm" file loaded true
current translation file "proj_.qm" file loaded false
These lines are continuing for 30 times or 40 times.
And then the program is crashing with following message
Quote:
The program has unexpectedly finished.
D:\Test-to-device\17-jul-2012\debug\proj.exe exited with code -1073741571
Re: How to implement multiple Languages on an application from inside the application
Your combobox that keeps the list of languages is being retranslated, causing the signal to be emitted again, which results in trying to load a new translation, calling retranslateUi() and the whole situation repeats until you exhaust your stack space and the program crashes. Make your translation loading slot smarter to prevent such situations.
Re: How to implement multiple Languages on an application from inside the application
First install the translator i.e.;
Code:
qApp->installTranslator(&trans);
use changeEvent() for changing the language of every source text. In change event check for the condition that is:-
Code:
if(event
->type
()==QEvent::LanguageChange) {
retranslateUi();
}
Re: How to implement multiple Languages on an application from inside the application
Thank you very much wysota :D
you made my day.
I just added a user defined slot to signal from combo Box and pasted my code there.
Now I'm running good.
Thank you. :D:D:D