Inversus
Inversus

Reputation: 63

Translate text using google without API

I am trying to write a simple free translator (QT widget) using the online translation service. The idea is to send the standard get request to an online translator, and then parse the response. But the reply does not contain the translated text ! I guess this is because the service uses AJAX. In the example, I am using the google translator, but I get similar results with other translators (yandex, deepl). I know that there is a way to use the shareware API, but since the project is not commercial at the moment, I do not want to register a bank card. Is there a browser-like way to get translation without the API and use it for free ?

I have searched for any information, but to my surprise, it was outdated and irrelevant at the moment (since Google closed the free service).

And one more question. When I tried to cast the result to a QString (QString s = reply->readAll().toString() or QString s = reply->readAll().toStdString().c_str()), I got a distorted htlm code (a lot of NUL characters at the beginning of the file). I assume that this is due to a misinterpretation of the escape sequences, but how then to cast the result correctly ? Even in the current version, there is some garbage at the beginning of the file (NUL NUL Уi).

The code I use is:

void getTranslate() {
    QNetworkAccessManager manager;
    QUrl url("https://translate.google.com/#view=home&op=translate&sl=en&tl=ru&text=Hello%2C%20World%20!");
    QNetworkRequest request(url);
    QNetworkReply *reply = manager.get(request);

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

    QFile html("out.html");
    if (html.open(QIODevice::ReadWrite)) {
        QDataStream out(&html);
        out <<  reply->readAll();
    }

    reply->close();

    delete reply;
}

Upvotes: 3

Views: 3621

Answers (2)

user6057915
user6057915

Reputation:

looking at Google Translate it uses AJAX Request, to get the translation. You could try to change the URL to something like this (this is where the ajax request goes to):

https://translate.google.de/translate_a/single?client=webapp&sl=auto&tl=en&hl=de&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=sos&dt=ss&dt=t&dt=gt&otf=2&ssel=0&tsel=0&xid=45662847&kc=1&tk=656516.836633&q=dies%20ist%20ein%20test

This request returns JSON data, which should be easy to parse.

I am not sure what all the parameters are for, but maybe this information is helpful for you.

Upvotes: 3

AziMez
AziMez

Reputation: 2072

I belive your problems with screen-scraping approach may be that the translate application uses Ajax to call the server-side and retrieve the translation. The page you get when downloading using QNetworkRequest is merely the JS application, it doesn't actually contain the translation. That doesn't get filled in until after a call has been made from the page to the server. And that is why it isn't working. Perhaps you could get it working somehow, so let us know how you do it :-)

Upvotes: 0

Related Questions