PDA

View Full Version : How to implement multiple Languages on an application from inside the application?



satya@sipl
17th July 2012, 15:55
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.

wysota
17th July 2012, 19:10
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.

satya@sipl
18th July 2012, 07:41
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.



main.cpp

QTranslator curTranslator;

QApplication app(argc, argv);

//installing the current language of the device before starting the application.
curTranslator.load(QString("proj_") + QLocale::system().name() );

app.installTranslator(&curTranslator);

MainWindow mainWindow;//main application starts from here

mainWindow.showFullScreen();

return app.exec();


Mainwindow.cpp

void MainWindow::on_Language_cb_currentIndexChanged(int index)
{
QString langAbbr;

switch(index)
{
case 0: langAbbr="en";break;

case 1: langAbbr="ar";break;

case 2: langAbbr="fr";break;

default: langAbbr="en";
}

qApp->removeTranslator(&curTranslator);

QString tempStr=QString("proj_%1.qm").arg(langAbbr);

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..
}

wysota
18th July 2012, 08:09
What is "not working"? What does QTranslator::load() return?

satya@sipl
18th July 2012, 08:44
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


void MainWindow::on_Language_cb_currentIndexChanged(int index)
{
QString langAbbr;
switch(index)
{
case 0: langAbbr="en";break;
case 1: langAbbr="ar";break;
case 2: langAbbr="fr";break;
default: langAbbr="en";
}
QString tempStr=QString("proj_%1.qm").arg(langAbbr);

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
}


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

wysota
18th July 2012, 09:41
And is that good or bad?

satya@sipl
18th July 2012, 10:24
wysota, My problem is that while selecting the language "french", the qDebug() should show me only one line

current translation file "proj_fr.qm" file loaded true
not anything else.
And while I'm writing code segment



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

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

The program has unexpectedly finished.
D:\Test-to-device\17-jul-2012\debug\proj.exe exited with code -1073741571

wysota
18th July 2012, 10:49
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.

sonulohani
18th July 2012, 11:26
First install the translator i.e.;


qApp->installTranslator(&trans);


use changeEvent() for changing the language of every source text. In change event check for the condition that is:-



if(event->type()==QEvent::LanguageChange)
{
retranslateUi();
}

satya@sipl
18th July 2012, 12:45
Thank you very much wysota :D

you made my day.

I just added a user defined slot to signal
activated(int) from combo Box and pasted my code there.

Now I'm running good.

Thank you. :D:D:D