PDA

View Full Version : Need help on 'google translator' code



rishiraj
6th March 2009, 09:12
(am using Qt/Linux fedora 8)
Hi,
I found this great application in qt-apps.org on translating languages using google translator.
I tried running it in my system but it didn't show me the translated text(though it worked for many of the others who used it).
Also,I couldn't understand the necessity of a portion of the code specially in the received ("done()" slot ,regarding the slashes(///) and the ']' )so,I modified the code a bit to see if it would work without that section...well,the modified code isn't showing anything either.[ I used a couple of Message boxes to give me the values,in the 'request id' it pops up a message box twice showing '0' and in the done slot(),the Message box shows a blank]
So,would anyone please test out the applications and tell me if they are showing any result in your system?I am giving the link to the original(exceeds size limit even after i zip it up)and am attaching the modified version,please check out both and in case the original works but not the modified one,I would appreciate it, if you could point out what I am doing wrong and where.
Thanks in advance for any help.
The link to the original application is :
http://www.qt-apps.org/content/show.php/Google+Translate?content=100039
:)

rishiraj
9th March 2009, 08:44
Hi everyone,
No replies yet,so, thought I would raise my query again.
Is the code below correct?It needs to send data to google translate and then set the retrieved translated text to a text edit box.It's not showing anything my side but,I was told that it might also be due to settings issues my side.So,I need someone to go over the code and tell me if it's ok.Please check the first post to get a better idea.
Thanks.


/*Sending the text which needs to be translated*/
void myQtApp::translate()
{
QString from, to, text;
from = langs->at(langfrom->currentIndex() );
to = langs->at( langto->currentIndex() );
text =text_from->toPlainText();

QByteArray ba("text=");
ba.append( text.toUtf8() );
QMessageBox::information(NULL,"outgoing data",QString(ba));

QString url=QString("http://translate.google.com/translate_a/t?Client=t&sl=" + from + "&tl=" +to);
QMessageBox::information(NULL,"url",url);

http->setHost("www.google.com");
http->post(url,ba);
}
/*The translated text is set in the 'to' text edit box.*/
void myQtApp::done()
{
QMessageBox::information(NULL,"reached","entered done loop");
QString transtext;
QByteArray ba1;
ba1=http->readAll();
QString text1;
transtext=text1.fromUtf8(ba1);
QMessageBox::information(NULL,"received",transtext);
text_to->setText(transtext);
}
/*When the data is sent to google,the requestStarted(int) signal is generated which calls this slot*/
void myQtApp::started()
{
int id1=http->currentId();
QMessageBox::information(NULL,"process no.",QString(id1));
}

spirit
9th March 2009, 09:03
take a look at this article (http://alexsnet.ru/en/2009/02/google-translate-non-ajax-api/). maybe it will help you.

rishiraj
9th March 2009, 09:16
Hi Spirt,
thanks for the reply.
I did check the page you mentioned.Alex had put the google translate application in qtapps.org.I downloaded it but, it didn't work in my system and he said that it might be due to some problem with Qhttp etc.I modified some of the commands in the code and removed some segfments which I didn't understand properly and tried running the modified version but ,it's not showing anything either.So,if anyone would go over the code and tell me if it looks ok and should work,I can conclude that it's a problem with my system and not the code(the original one by Alex worked for others so,I think it works properly)I need someone to test out the modified one and tell me if it shows any output.

rishiraj
9th March 2009, 09:18
sorry,message got posted twice....pls check the above post.

spirit
9th March 2009, 09:20
Alex's code works fine for me.

rishiraj
9th March 2009, 09:24
what about the modified one?does it show anything? ( i doubt it will,but,better to make sure:)

spirit
9th March 2009, 09:31
seems like your example doesn't work, see attach.
PS. file has been packed using 7z.

rishiraj
9th March 2009, 09:43
Thanks for the info Spirit.

spirit
9th March 2009, 10:02
try to change this line


QString url=QString("http://translate.google.com/translate_a/t?Client=t&sl=" + from + "&tl=" +to);

om this one


QString url=QString("/translate_a/t?client=t&sl=" + from + "&tl=" + to);

looks like this code works.

rishiraj
9th March 2009, 10:37
hi,
did you mean that making the above changes in the modified code makes it work?
Problem is none of the application shows anything my side....neither the original by Alex nor this modified one...so,I have to get the info from you.Thanks

spirit
9th March 2009, 10:41
yes, if you applay changes (that I described abow) in your app version then it starts working.

rishiraj
9th March 2009, 10:45
Hi Spirit,
Thank you so much for taking the time to go over the code and for rectifying the mistakes.