【问题标题】:Getting wrong translation from Google translate using C#使用 C# 从 Google 翻译中得到错误的翻译
【发布时间】:2019-05-01 19:31:44
【问题描述】:

我正在使用这种方法通过谷歌翻译从我的程序中翻译一些文本,直到本周它都运行良好:

public string TranslateText(string input, string languagePair)
        {
            string r = WebUtility.HtmlDecode(input);
            r = WebUtility.UrlEncode(r);
            string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", r, languagePair);
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.GetEncoding("Windows-1252");
            byte[] resultbyte = webClient.DownloadData(url);
            string result = Encoding.Default.GetString(resultbyte);
            result = result.Substring(result.IndexOf("TRANSLATED_TEXT=") + 16);
            result = result.Replace("\\x26", "&");
            result = result.Replace("\\x3d", "=");
            result = WebUtility.HtmlDecode(result);
            result = result.Remove(result.IndexOf(";"));
            result = result.Replace("'", string.Empty);

            return result;

        }

但现在我像往常一样运行程序并且总是得到这个翻译:

<html lang="en"> <head> <style>@import url(https://fonts.googleapis.com/css?lang=en&family=Product+Sans|Roboto:400,700)

我不知道会发生什么。有谁知道是什么问题?

【问题讨论】:

  • Google 可能更改了响应的格式

标签: c# google-translate translate


【解决方案1】:

快速搜索一下 Google 意味着 Google Translate API 已经有一段时间没有被设计成这样工作了,它持续了这么长时间对你来说可能是纯粹的运气。

他们的条款不允许您使用 Google 翻译工具的方式(本质上是屏幕抓取他们的免费网络工具)。你应该和他们一起apply for an account 并期望pay,尽管如果你只是翻译一点点文本,数量很少。您可以通过修改您的 URL 和网页抓取代码(如果您尚未被阻止)来绕过它,但您不能在这里寻求帮助以规避法律协议。

如果您决定走合法路线,一旦您启动并使用帐户运行,您就可以使用您的 API 密钥/令牌直接访问 API。详情请见quickstart guide

【讨论】:

    猜你喜欢
    • 2014-07-14
    • 2018-11-02
    • 2019-02-27
    • 2014-03-10
    • 2010-11-16
    • 2013-08-07
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多