如何使用 Google Translate API 与 c++
我是 C++ 初学者,正在使用 Google 任务 API。
如何编写一个 C++ 程序来接受一个单词,然后调用 Google Translate 将其从英语翻译成法语,然后将结果页面保存到本地文件?
例如,如果用户输入“river”,程序应调用Google Translate将其翻译为法语,结果页面为: http://translate.google.com/#en|fr|River%0A 应保存此页面。
我完整阅读了官方文档:http://code.google.com /apis/language/translate/v2/getting_started.html 但我不明白如何使用 REST,也不熟悉 JSON 或 AJAX。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能直接在 C++ 中使用 JSON 对象。
JSON = JavaScript 对象表示法
您需要生成/创建可以解析此类对象的东西。例如,我在 Qt(C++ 中)中使用 QNetworkManager 来发送构建 javascript 并向 google API 发送请求。结果将是一个 JSON 对象,我必须对其进行解析并在浏览器中显示(我用 C++ 制作)或将结果解析为 C++ 类并以不同的方式处理它(用它进行一些计算)。
如果您只需要数据,您可以请求 XML 而不是 JSON,然后您可以使用标准 XML 解析器来提取翻译后的单词。
编辑 1:
Google 在他们的示例中使用:
https://www.googleapis.com/language/translate/v2?key=YOUR-API-KEY&source=en&target=de&q=words+to+translate
这个从英语 (en) 翻译为德语 (de)。要翻译的单词是:“要翻译的单词”。
现在用 C++ 构建一个 HTTP 请求,并将该 URL 作为数据。通过一些网络管理器或套接字发送该信息并等待回复。谷歌将向您返还数据。
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.
我在下面的某个地方看到过这段代码,但我不记得在哪里,无论如何试试这个:
I seen this codes below somewhere but I don't remember where, anyway try this:
有人建议我使用libcurl,我会尝试看看。
someone advise me to use libcurl, I will try and see.
您需要某种 HTTP 库。我同意 Adrian 所说的大部分内容,但这里有一些(不完整的)Qt 代码,应该可以给您一个想法。
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.