Google Translate Class (Qt, C ++)

    So, I decided to lay out a protocol separately, more precisely, the protocol class, Google Translate, according to which my translator works . At the moment, the protocol has the ability to set proxies, make a translation (of course) and, what everyone asked for, Google tips ... That is, if you did not 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 - a featurerekvest and I will add!

    The code is under the cut, look ...

    Until you have looked at the habrakat ... I apologize right away, the code is not so big, but still laid out directly, you can download it along with the source code of the translator from my blog.
    Do not scold much. I post it on my personal blog, put it somewhere else, I think it makes no sense.


    #include "googleproto.h"

      http = new QHttp(this);
      version = new QString("0.9.5");


      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(  );
      langpair.prepend( );

      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", "");
      headers.setValue("User-Agent", "Mozilla/5.0 (ASTranslator)" );
      headers.setValue("Accept-Encoding", "deflate");
      headers.setContentLength( ba.length() );
      headers.setValue("Connection", "Close");

      http->request( headers, ba );
    void GoogleProto::startTranslation( int from, int to, QString text )
      QString url = QString("/translate_a/t?client=t&sl=" + + "&tl=" + );

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

      headers = QHttpRequestHeader("POST", url, 1, 1);
      headers.setValue("Host", "");
      headers.setValue("User-Agent", "Mozilla/5.0 (ASTranslator)" );
      headers.setValue("Accept-Encoding", "deflate");
      headers.setValue("Connection", "Close");

      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(!="") {
            if(j==0) {
              QString translation =;
              translation = translation.replace("\"","");
              translation = translation.replace(",","");
              text.append( translation );
              text.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) {
                  text.append( QString( translation + ": ") );
                } else {
                  text.append( QString( "\t" + translation + "\n" ) );
              text.append( "\n" );
      emit( translationComplete( text ) );

    void GoogleProto::setTranslationArrays()
      <<"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";
      <<"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.


    #ifndef GOOGLEPROTO_H
    #define GOOGLEPROTO_H

    #include <QObject>
    #include <QtNetwork>

    class QHttp;

    class GoogleProto : public QObject

      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 );

      QString translationComplete( QString text );

      QHttp *http;
      QHttpRequestHeader headers;

      void setTranslationArrays();

      QString *version;

    #endif // GOOGLEPROTO_H

    * This source code was highlighted with Source Code Highlighter.

    And a small request ... Use it ... Throw a link at me, anywhere ... This is not necessary, but I will be pleased =) Still not bad if you write me directly that you use it in such and such an application.

    Well, everyone a nice start to the work week;)

    Also popular now: