2

I'm a beginner in C++ and using Google task API.

How do I write a C++ program that accepts a word, then invokes Google Translate to translate it from English to French, then saves the resulted page to a local file?

For example, if the user inputs "river", the program should invoke Google Translate to translate into French, the resulting page is: http://translate.google.com/#en|fr|River%0A This page should be saved.

I read the official documentation through fully: http://code.google.com/apis/language/translate/v2/getting_started.html but I couldn't understand how to using REST and I'm not familiar with JSON or AJAX.

Ski
  • 14,197
  • 3
  • 54
  • 64
yara
  • 31
  • 1
  • 2

4 Answers4

2

You cannot use JSON objects straight in C++.

JSON = JavaScript Object Notation

You need to spawn/create something which can parse such objects. For example I've used QNetworkManager in Qt (in C++) to send build javascript and send requests to google APIs. The result would be a JSON object which I had to parse and either display in a browser (which I made in c++) or parse the results into a c++ class and process it differently (do some calculations with it).

If you just need the data, you can request XML instead of JSON and then you can use a standard XML parser to extract the translated word.

EDIT 1:

Google in their example uses:
https://www.googleapis.com/language/translate/v2?key=YOUR-API-KEY&source=en&target=de&q=words+to+translate

This translate from english (en) to german (de). The words to translate are: "words to translate".

Now build an HTTP request in C++ with this URL as the data. Send that with some network manager or sockets and wait for a reply back. Google will give you back data.

Adrian
  • 5,603
  • 8
  • 53
  • 85
  • Hi Adrian: could you explain more by demonstrate a code in c++ invoke Google API. – yara Dec 18 '11 at 06:29
  • I used Qt in c++, so not pure c++. C++ must have some sockets or browser that you can use to send the HTTP GET request to google API. I don't have code for that, only for Qt C++. – Adrian Dec 18 '11 at 06:31
  • @yara What you need to do first is sign up for the API and google will give you a key. You use the key to to send in your requests to the Google server. Basically from c++ you will make an HTTP request. Inside it, you will set the data to be this: https://www.googleapis.com/language/translate/v2/detect?key=INSERT-YOUR-KEY&q=words+to+translate – Adrian Dec 18 '11 at 06:34
  • I understand the google Key step but i couldn't imagine the call statement in the c++ program – yara Dec 18 '11 at 07:22
  • @yara you can't use call google API from c++ unless google provides a c++ package for this. I believe they do provide C# and java, so in C++ you send HTTP requests :) – Adrian Dec 18 '11 at 08:31
1

I seen this codes below somewhere but I don't remember where, anyway try this:

QString YourClass::translate(QString keyword, QString from, QString to)
{
    //Translate URL
    QString url = QString("http://translate.google.com/translate_a/t?client=t&text=%0&hl=%1&sl=%2&tl=%1&multires=1&prev=enter&oc=2&ssel=0&tsel=0&uptl=%1&sc=1").arg(keyword).arg(to).arg(from);

    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    QNetworkReply *reply = manager.get(request);

    //Get reply from Google
    do {
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    } while(!reply->isFinished());

    //Convert to string
    QString translation(reply->readAll());
    reply->close();

    //Free memory
    delete reply;

    //Remove [[[" from the beginning
    translation = translation.replace("[[[\"", "");

    //Extract final translated string
    translation = translation.mid(0, translation.indexOf(",\"") - 1);

    return translation;
}
Amir eas
  • 121
  • 2
  • 10
0

someone advise me to use libcurl, I will try and see.

yara
  • 31
  • 1
  • 2
0

You need some kind of HTTP library. I second most of what Adrian said, but here's some (incomplete) Qt code which should give you an idea.

namespace {
    const QString API_KEY = /* TODO */;
}

MyQObject :: MyQbject ()
: m_manager (new QNetworkAccessManager (this))
{
    connect(manager, SIGNAL (finished (QNetworkReply *)),
            this, SLOT (reply_finished (QNetworkReply *)));
}

void MyQObject :: fetch_translation (
     QString words,
     void (*on_receive)(const QString &))
{
    // Let's assume asynchronous but non-concurrent requests.
    m_on_receive = on_receive;

    QString request =
        "https://www.googleapis.com/language/translate/v2"
        "?key=%1&source=en&target=de&q=%2";

    // May want to url-encode 'words' first.

    m_manager -> get (QUrl (request .arg (API_KEY) .arg (words));
}

void MyQObject :: reply_finished (QNetworkReply * reply)
{
    m_on_receive (reply -> readAll ());
}

// ...
{
    my_q_object -> translate ("hello, world", [](const QString & s) {
        qWarning () << "translation is: " << s;
    });
}
spraff
  • 32,570
  • 22
  • 121
  • 229