Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Qt Google Translator

From Wiki
Jump to: navigation, search

This article shows how to use non-Ajax version of the google translator method to get our text translated.

Article Metadata
Tested with
Devices(s): Emulator
Platform(s): S60 5th Edition, Qt 4.6.2, Qt 4.7
Keywords: QHttp
Created: skumar_rao (21 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


  1. Create a QHttp member to transact with Google server.
    	http = new QHttp(this);
  2. Connect QHttp::done() member to our own class slot to grab transaction data from Google server.
    	connect(http, SIGNAL(done(bool)), this, SLOT(transaction_done()));
  3. Prepare google translator service Url with to & from languages.
    	QString url = QString("/translate_a/t?client=t&sl=" + fro + "&tl=" + to);
  4. Prepare text to be added to service.
    	QByteArray textByteArray("text=");
    textByteArray.append( text.toUtf8() );
  5. Prepare Request header
    	QHttpRequestHeader header = QHttpRequestHeader("POST", url, 1, 1);
    header.setValue("Host", "");
    header.setValue("User-Agent", "Mozilla/5.0"); // ToDo: change to a value specific to device
    header.setValue("Accept-Encoding", "deflate");
    header.setValue("Connection", "Close");
  6. Invoke Http request with header and binary request
  7. Once the HTTP gets the response from the service read the http reply to a string
    QString replyText;
    replyText = replyText.fromUtf8( http->readAll() );

    Note.pngNote: This data will be in HTML / Url Encoded data

  8. Now we have to format this string to get the properly format data
        replyText = replyText.replace(QString("\\\""),QString("\""));
    replyText = replyText.replace(QString("\\n"),QString("\n"));
    replyText = replyText.replace(QString("\n "),QString("\n"));
    replyText = replyText.replace(QString("\\x3c"),QString("<"));
    replyText = replyText.replace(QString("\\x3e"),QString(">"));
    if( replyText.startsWith( QString("\"") ) ) {
    replyText = replyText.remove( replyText.length()-1, 1).remove(0,1);
    } else if( replyText.startsWith( QString("[") ) && replyText.endsWith( QString("]") ) ) {
    QStringList translatedList;
    replyText = replyText.replace(QString("]"),QString(""));
    translatedList = replyText.split(QString("["));
    replyText = QString("");
    for(int i=0,j=0;i<translatedList.count();i++) {
    if(!="") {
    if(j==0) {
    QString translation =;
    translation = translation.replace("\"","");
    translation = translation.replace(",","");
    replyText.append( translation );
    replyText.append( "\n\n") ;
    } else {
    QString translation =;
    QStringList translations = translation.split(",");
    for(int y=0;y<translations.count();y++) {
    translation =;
    translation = translation.replace("\"","");
    if(y==0) {
    replyText.append( QString( translation + ": ") );
    } else {
    replyText.append( QString( "\t" + translation + "\n" ) );
    replyText.append( "\n" );


Now replyText will contain formatted text that we can use in QTextEdit for display.

This page was last modified on 11 October 2012, at 01:18.
105 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.