Anda di halaman 1dari 3

Log in Join

English

Design

Develop

Distribute

Devices

Resources

Community

Search
Share

iki W
Create New Wiki Page Article Comment Read View source History
Categories: Qt | How To | Code Examples

Wiki Resources
Wiki Home Help Etiquette Featured Articles Featured Videos Templates Glossary Feedback

rt lsr elGQ o nT gt aa o nitdtI o crn u o


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

Contents
Introduction Prerequisites Steps
Step 01: Step 02: Step 03: Step 04: Step 05:

stisiurr e qe eP
Qt 4.6.2 or above (tested ok w ith 4.7 too) Qt Nokia SDK w ith latest Update w ill do just fine A Qt & Data enabled device [Optional] w e w ill use Qt Nokia SDK's Simulator.

Step 06: Step 07: Step 08:

Development Frameworks Platforms Web Development Mobile Design Knowledge Base Tools & Resources Natural Languages Toolbox

Result

st p e S
0S 1 :e p t
Create a QHttp member to transact w ith Google server.

Article Metadata
Tested w ith
Devices(s): Emulator

Compatibility
Platform(s): S60 5th Edition

Article
Keywords: QHttp

http = new QHttp(this); http->setHost("www.google.com");

Created: skumar_rao (21 Nov 2010) Last edited: hamishwillee (24 Jun 2011)

0S 2 :e p t
Connect QHttp::done() member to our ow n class slot to grab transaction data from Google server.

connect(http, SIGNAL(done(bool)), this, SLOT(transaction_done()));

0S 3 :e p t
Prepare google translator service Url w ith to & from languages.

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

0S 4 :e p t
Prepare text to be added to service.

QByteArray textByteArray("text="); textByteArray.append( text.toUtf8() );

0S 5 :e p t
converted by Web2PDFConvert.com

Prepare Request header

QHttpRequestHeader header = QHttpRequestHeader("POST", url, 1, 1); header.setValue("Host", "www.google.com"); header.setValue("User-Agent", "Mozilla/5.0"); // ToDo: change to a value specific to device header.setValue("Accept-Encoding", "deflate"); header.setContentLength(text.length()); header.setValue("Connection", "Close");

0S 6 :e p t
Invoke Http request w ith header and binary request

http->request(header,textByteArray);

0S 7 :e p t
Once the HTTP gets the response from the service read the http reply to a string

QString replyText; replyText = replyText.fromUtf8( http->readAll() );

Note: This data w ill be in HTML / Url Encoded data

0S 8 :e p t
No w e 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(translatedList.at(i)!="") { if(j==0) { QString translation = translatedList.at(i); translation = translation.replace("\"",""); translation = translation.replace(",",""); replyText.append( translation ); replyText.append( "\n\n") ; } else { QString translation = translatedList.at(i); QStringList translations = translation.split(","); for(int y=0;y<translations.count();y++) { translation = translations.at(y); translation = translation.replace("\"",""); if(y==0) { replyText.append( QString( translation + ": ") ); } else { replyText.append( QString( "\t" + translation + "\n" ) ); } } replyText.append( "\n" ); } j++; } }

converted by Web2PDFConvert.com

tlu s e R
Now the replyText w ill contain formated text that w e can use in QTextEdit --skumar_rao 04:16, 21 November 2010 (UTC) for display

m Ct on se m
(no comments yet)

Log in or Join to leave comments.

This page w as last modified on 24 June 2011, at 09:25. 107 page view s in the last 30 days.

Featured links
Event calendar Contact and feedback Support

Distrib utio n
Store Statistics Publisher Guide

Co mmunity
Discussions Wiki Groups Projects Champions Knowledge base

R eso urces
Documentation Tools Code examples Developer Programs

Blo gs
Nokia Developer Blog Nokia Publisher Blog Qt Blog

Develo p
Windows Phone Qt Series 40

Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

Site Map

Terms & Conditions

Privacy Policy

Copyright Nokia 2011 All rights reserved

converted by Web2PDFConvert.com