用C++写一个翻译程序
时间: 2023-12-27 09:24:52 浏览: 93
好的,我可以为你提供一个简单的 C++ 翻译程序的实现。
该程序使用 Google Translate API 来翻译输入的文本。在使用此程序之前,你需要先获取 Google Cloud Platform 的授权凭证。
以下是程序的实现:
```C++
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
using namespace std;
// 定义回调函数,用于获取 API 返回的数据
size_t WriteCallback(char* buf, size_t size, size_t nmemb, void* up)
{
((string*) up)->append(buf, size * nmemb);
return size * nmemb;
}
// 定义函数,调用 Google Translate API 进行翻译
string Translate(string text, string source, string target)
{
// 格式化 API 请求 URL
string url = "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY";
url += "&q=" + text;
url += "&source=" + source;
url += "&target=" + target;
// 初始化 libcurl
CURL* curl = curl_easy_init();
if (curl)
{
CURLcode res;
string response;
// 设置 API 请求参数
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// 发送 API 请求
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
cerr << "Error: " << curl_easy_strerror(res) << endl;
}
// 释放资源
curl_easy_cleanup(curl);
// 解析 API 响应数据
Json::Value root;
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();
if (reader->parse(response.c_str(), response.c_str() + response.size(), &root, nullptr))
{
// 返回翻译结果
return root["data"]["translations"][0]["translatedText"].asString();
}
else
{
cerr << "Error: Failed to parse API response." << endl;
}
}
return "";
}
int main()
{
string text, source, target;
cout << "Enter the text to translate: ";
getline(cin, text);
cout << "Enter the source language (e.g. en): ";
getline(cin, source);
cout << "Enter the target language (e.g. zh-CN): ";
getline(cin, target);
// 调用翻译函数进行翻译
string translation = Translate(text, source, target);
if (translation.empty())
{
cerr << "Error: Failed to translate text." << endl;
}
else
{
cout << "Translation: " << translation << endl;
}
return 0;
}
```
这个程序可以接受用户输入的文本、源语言和目标语言,并使用 Google Translate API 将文本翻译成指定的语言。你需要将 `YOUR_API_KEY` 替换成你的 Google Cloud Platform API 服务的授权凭证。
希望这个程序能帮到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)