カットの下のコード、参照してください...
これまでのところ、あなたはhabrakatの下を見ていない...私はすぐに謝罪します、コードはそれほど大きくありませんが、まだレイアウトされています、あなたは私のブログから翻訳者のソースコードとともにそれをダウンロードできます。
あまりscられないでください。 私は自分のブログに投稿し、別の場所に置きますが、意味がないと思います。
googleproto.cpp
#include "googleproto.h"
GoogleProto::GoogleProto()
{
http = new QHttp( this );
version = new QString( "0.9.5" );
setTranslationArrays();
connect( http, SIGNAL(done( bool )), this , SLOT(textTranslated()) );
}
int GoogleProto::setProxy( const QString & host, int port, const QString & username = QString(), const QString & password = QString() )
{
return http->setProxy( host, port, username, password );
}
void GoogleProto::suggestTranslation( int from , int to, QString original, QString gtrans, QString suggest )
{
QString url = QString( "/translate_suggestion" );
QString langpair( "|" );
langpair.append( translatesShort.at(to) );
langpair.prepend( translatesShort.at( from ) );
QByteArray ba( "" );
ba.append( QString( "gtrans=%1&langpair=%2&oe=UTF-8&text=%3&utrans=%4" ).arg(gtrans, langpair, original, suggest).toUtf8() );
headers = QHttpRequestHeader( "POST" , url, 1, 1);
headers.setValue( "Host" , "www.google.com" );
headers.setValue( "User-Agent" , "Mozilla/5.0 (ASTranslator)" );
headers.setValue( "Accept-Encoding" , "deflate" );
headers.setContentLength( ba.length() );
headers.setValue( "Connection" , "Close" );
http->setHost( "translate.google.com" );
http->request( headers, ba );
}
void GoogleProto::startTranslation( int from , int to, QString text )
{
QString url = QString( "/translate_a/t?client=t&sl=" + translatesShort.at( from ) + "&tl=" + translatesShort.at(to) );
QByteArray ba( "text=" );
ba.append( text.toUtf8() );
headers = QHttpRequestHeader( "POST" , url, 1, 1);
headers.setValue( "Host" , "www.google.com" );
headers.setValue( "User-Agent" , "Mozilla/5.0 (ASTranslator)" );
headers.setValue( "Accept-Encoding" , "deflate" );
headers.setContentLength(text.length());
headers.setValue( "Connection" , "Close" );
http->setHost( "www.google.com" );
http->request( headers, ba );
}
void GoogleProto::textTranslated()
{
QString text;
text = text.fromUtf8( http->readAll() );
text = text.replace(QString( "\\\"" ),QString( "\"" ));
text = text.replace(QString( "\\n" ),QString( "\n" ));
text = text.replace(QString( "\n " ),QString( "\n" ));
text = text.replace(QString( "\\x3c" ),QString( "<" ));
text = text.replace(QString( "\\x3e" ),QString( ">" ));
if ( text.startsWith( QString( "\"" ) ) ) {
// This is a text
text = text.remove( text.length()-1, 1).remove(0,1);
} else if ( text.startsWith( QString( "[" ) ) && text.endsWith( QString( "]" ) ) ) {
// This is a word
// Need dictionary mode
QStringList tra;
//tra = text.split(QRegExp(QString("\"(.*)\"")));
text = text.replace(QString( "]" ),QString( "" ));
tra = text.split(QString( "[" ));
text = QString( "" );
for ( int i=0,j=0;i<tra.count();i++) {
if (tra.at(i)!= "" ) {
if (j==0) {
QString translation = tra.at(i);
translation = translation.replace( "\"" , "" );
translation = translation.replace( "," , "" );
text.append( translation );
text.append( "\n\n" ) ;
} else {
QString translation = tra.at(i);
QStringList translations = translation.split( "," );
for ( int y=0;y<translations.count();y++) {
translation = translations.at(y);
translation = translation.replace( "\"" , "" );
if (y==0) {
text.append( QString( translation + ": " ) );
} else {
text.append( QString( "\t" + translation + "\n" ) );
}
}
text.append( "\n" );
}
j++;
}
}
}
emit( translationComplete( text ) );
}
void GoogleProto::setTranslationArrays()
{
translatesShort
<< "sq" << "ar" << "bg" << "ca"
<< "zh-CN" << "zh-TW" << "hr"
<< "cs" << "da" << "nl" << "en"
<< "et" << "tl" << "fi" << "fr"
<< "gl" << "de" << "el" << "iw"
<< "hi" << "hu" << "id" << "it"
<< "ja" << "ko" << "lv" << "lt"
<< "mt" << "no" << "pl" << "pt"
<< "ro" << "ru" << "sr" << "sk"
<< "sl" << "es" << "sv" << "th"
<< "tr" << "uk" << "vi" ;
translatesFull
<< "Albanian" << "Arabic" << "Bulgarian" << "Catalan"
<< "Chinese (Simplified)" << "Chinese (Traditional)" << "Croatian"
<< "Czech" << "Danish" << "Dutch" << "English"
<< "Estonian" << "Filipino" << "Finnish" << "French"
<< "Galician" << "German" << "Greek" << "Hebrew"
<< "Hindi" << "Hungarian" << "Indonesian" << "Italian"
<< "Japanese" << "Korean" << "Latvian" << "Lithuanian"
<< "Maltese" << "Norwegian" << "Polish" << "Portuguese"
<< "Romanian" << "Russian" << "Serbian" << "Slovak"
<< "Slovenian" << "Spanish" << "Swedish" << "Thai"
<< "Turkish" << "Ukrainian" << "Vietnamese" ;
}
* This source code was highlighted with Source Code Highlighter .
googleproto.h
#ifndef GOOGLEPROTO_H
#define GOOGLEPROTO_H
#include <QObject>
#include <QtNetwork>
class QHttp;
class GoogleProto : public QObject
{
Q_OBJECT
public :
GoogleProto();
int setProxy( const QString & host, int port, const QString & username, const QString & password );
void suggestTranslation( int from , int to, QString original, QString gtrans, QString suggest );
QStringList translatesShort;
QStringList translatesFull;
public slots:
void textTranslated();
void startTranslation( int from , int to, QString text );
signals:
QString translationComplete( QString text );
private :
QHttp *http;
QHttpRequestHeader headers;
void setTranslationArrays();
QString *version;
};
#endif // GOOGLEPROTO_H
* This source code was highlighted with Source Code Highlighter .
そして、小さなリクエスト...それを使用してください...どこでも私にリンクを投げてください...これは必要ではありませんが、私は満足します=)
さて、皆、仕事の週の良いスタートです;)