【问题标题】:Google Translate C#谷歌翻译 C#
【发布时间】:2012-09-14 00:00:16
【问题描述】:

我正在开发一个自然语言处理程序,我正在尝试在其中实施 Google 翻译。在寻找在 Assembly 中实现 Google 翻译的方法时,我遇到了以下代码段:

public static string Translate(string input, string languagePair, Encoding encoding)
{
    string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text=       {0}&langpair={1}", input, languagePair);
    string result = String.Empty;

    using (WebClient webClient = new WebClient())
    {
        webClient.Encoding = encoding;
        result = webClient.DownloadString(url);
    }

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    return doc.DocumentNode.SelectSingleNode("//textarea[@name='utrans']").InnerText;
}

对C#比较陌生,主要是用Java,对隐式参数不清楚

public static string Translate(string input, string languagePair, Encoding encoding)

当我查看 Encoder 的 C# API 时,有一些关于如何使用 Encoding 类的示例:(链接:http://msdn.microsoft.com/en-us/library/h5y3703w(v=vs.71).aspx

Byte[] thirdcharNoFlush = new Byte[encoder.GetByteCount(chars, 2, 1, bFlushState)];
    encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState);

我应该在参数中输入什么才能使用 Google 翻译将短语(例如“你好吗?”)翻译成西班牙语。非常感谢您对此事的任何帮助!

【问题讨论】:

  • 试试Translate("how are you","enes",System.Text.Encoding.UTF8)。但是我不知道你是否能用这个 sn-p 抓取结果,因为我认为它早于谷歌的新 UI 翻译。
  • 密钥对应该用“|”分割。
  • @DarrenDavies 我不能再编辑我的评论了,但你是对的

标签: c# .net google-translate


【解决方案1】:

这应该可行:

var result = Translate("How are you?", "es|en", Encoding.UTF8);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2011-12-26
    • 2015-05-19
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多