如何使用Google Translate API使用c ++ [英] How to use Google Translate API With c++

查看:590
本文介绍了如何使用Google Translate API使用c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的初学者,使用Google任务API。

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

如何编写一个接受单词的C ++程序,然后调用Google翻译从英语到法语,然后将结果页面保存到本地文件?

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?

例如,如果用户输入river,程序应该调用Google翻译法语,结果页为:
http://translate.google.com /#en | fr | River%0A
此页面应该保存。

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.

我完全阅读官方文档: http://code.google.com/apis/language/translate/v2/getting_started.html
,但我不明白如何使用REST,我不熟悉JSON或AJAX。

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.

推荐答案

您不能在C ++中直接使用JSON对象。

JSON = JavaScript对象符号

JSON = JavaScript Object Notation

你需要生成/创建一些可以解析这些对象的东西。例如,我使用QNetworkManager在Qt(在C + +)发送构建javascript和发送请求到谷歌API。结果将是一个JSON对象,我不得不解析和显示在浏览器(我在c ++中做的)或解析结果到一个c ++类,并以不同的方式处理(用它做一些计算)。

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

如果只需要数据,可以请求XML而不是JSON,然后可以使用标准的XML解析器提取翻译的字。

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.

编辑1:

Google的示例用法:

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

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".

现在使用C ++以此网址作为数据创建HTTP请求。发送与一些网络管理器或套接字,并等待回复。 Google会回覆您的资料。

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.

这篇关于如何使用Google Translate API使用c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆