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