在winform应用程序中使用谷歌翻译获得翻译后的语言

本文关键字:翻译 语言 谷歌 应用程序 winform | 更新日期: 2023-09-27 17:51:07

我正试图在我的WinForms应用程序中翻译从"英语到孟加拉语"的字符串。我试过这个代码:

string input = "i eat rice";
string languagePair = "en|bn";
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string result = webClient.DownloadString(url);
result = result.Substring(result.IndexOf("<span title='"") + "<span title='"".Length);
result = result.Substring(result.IndexOf(">") + 1);
result = result.Substring(0, result.IndexOf("</span>"));
MessageBox.Show(result.Trim());

但是我得到的是:
&#2438 &#2478 &#2495 &#2477 &#2494 &#2468 &#2454 &#2494 &#2439
但如果我把它放在谷歌的搜索框中,它就会在搜索框中显示我翻译的语言。我怎样才能使翻译后的语言显示在我的WinForm中?注:我不想使用谷歌翻译API。

在winform应用程序中使用谷歌翻译获得翻译后的语言

您得到的结果,&#...,是每个UTF-16字符的HTML实体编码。您可以使用HttpUtility.HtmlDecodeWebUtility.HtmlDecode来获得实际的unicode字符串。

result = HttpUtitlityDecode(result.Trim());
MessageBox.Show(result);