【问题标题】:getting the translated language using google translator in winform apps在 winform 应用程序中使用谷歌翻译器获取翻译语言
【发布时间】:2020-06-15 17:42: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());

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

【问题讨论】:

  • 任何带有完整源代码示例的最终解决方案?

标签: c# winforms


【解决方案1】:

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

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

更多详情请见Decoding all HTML Entities

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多