📜 ⬆️ ⬇️

Google Translate Class (Qt, C ++)

So, I decided to lay out a separate protocol, or rather a protocol class, Google Translate, through which my translator works. At the moment, the protocol has the ability to install a proxy, make a translation (of course) and what everyone asked for, Google tips ... That is, if you don’t like the translation of something you can tell Google how it will be better translated in your opinion. Well, in my opinion more is not necessary. And if necessary - ficrequest and I will add!

Code under the cut, see ...

While you have not yet looked under the habrakat ... Immediately I apologize, the code is not so big, but it is still laid out directly, you can download it together with the source code of the translator from my blog.
Do not scold. I post it on a personal blog, I think it makes no sense to put it somewhere else.

')

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 .


And a small request ... Wake up to use ... Throw a link to me, anywhere ... This is not necessary, but I will be pleased =) It's also good if you write to me directly that you use it in this and such an application.

Well, all the rest of the working week;)

Source: https://habr.com/ru/post/62577/


All Articles