【问题标题】:Cannot use Google Translate API because of missing namespaces由于缺少命名空间,无法使用 Google Translate API
【发布时间】:2012-05-09 01:57:11
【问题描述】:

以下是我使用谷歌翻译的代码。我添加了一个 dll 作为参考:Google.Apis.Translate.V2
我还购买了 Google Translate API 密钥。

我有 5 个错误,因为我不知道我还需要什么 dll:这些对象不存在缺少命名空间:DataContractJsonSerializer、TranslationRootObject、TranslationRootObject

这些命名空间需要哪些 dll 引用?

这是我的代码,没有我的 API 密钥:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;

namespace Google_Translate
{
    public partial class Form1 : Form
    {
        static string apiKey = "";

        static string texttotranslate = "hello world";
        string text;
        static String apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
            static String url = String.Format(apiUrl, apiKey, "en", "ge", texttotranslate);
            Stream outputStream = null;

        byte[] bytes = Encoding.ASCII.GetBytes(url);

        // create the http web request
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);



        public Form1()
        {
            InitializeComponent();

            webRequest.KeepAlive = true;
            webRequest.Method = "POST";
            // Overrride the GET method as documented on Google's docu.
            webRequest.Headers.Add("X-HTTP-Method-Override: GET");
            webRequest.ContentType = "application/x-www-form-urlencoded";
            // send POST
            try
            {
                webRequest.ContentLength = bytes.Length;
                outputStream = webRequest.GetRequestStream();
                outputStream.Write(bytes, 0, bytes.Length);
                outputStream.Close();
            }
            catch (HttpListenerException e)
            {
                /*...*/
            }

            translate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private string translate()
        {
             try
        {
            // get the response
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            if (webResponse.StatusCode == HttpStatusCode.OK && webRequest != null)
            {
                // read response stream 
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    string lista = sr.ReadToEnd();

                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TranslationRootObject));
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(lista));
                    TranslationRootObject tRootObject = (TranslationRootObject)serializer.ReadObject(stream);
                    string previousTranslation = string.Empty;

                    //deserialize
                    for (int i = 0; i < tRootObject.Data.Detections.Count; i++)
                    {
                        string translatedText = tRootObject.Data.Detections[i].TranslatedText.ToString();
                        if (i == 0)
                        {
                            text = translatedText;
                        }
                        else
                        {
                            if (!text.Contains(translatedText))
                            {
                                text = text + " " + translatedText;
                            }
                        }
                    }
                    return text;
                }
            }
        }
        catch (HttpListenerException e)
        {
            /*...*/
        }

        return text;



        }
    }
}

有人可以修复我的代码或告诉我有什么问题吗?

我需要翻译 29-33kb 大小的文本文件,我想知道在使用谷歌翻译网站时是否可以像在线翻译一样快。

我还发现了这个链接Google Translate V2 cannot hanlde large text translations from C#,有人说翻译无法翻译大文件,所以我想知道 29-33kb 的文件是否算大?如果是这样,也许有人可以查看链接并根据链接中的答案修复我的代码,我现在尝试了很多但并没有真正理解它。但首先我需要找出为什么我的原始代码在这里不起作用。

【问题讨论】:

    标签: c# google-translate


    【解决方案1】:

    在您的项目中,添加对此程序集的引用:

    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

    可能还有其他你需要的,我刚查了这个。

    【讨论】:

    • 找不到其他命名空间 dll 可能与 java 脚本有关,我不确定。
    • 史蒂夫感谢链接中的 dll 修复了数据合同,但我仍然找不到如何修复 TranslationRootObject
    • 似乎是其中之一:使用 Google.Apis.Translate.v2;使用 Google.Apis.Translate.v2.Data;使用 Google.Apis.Translate.v2.Data.TranslationsResource;它们可以在一个程序集或多个程序集中。
    猜你喜欢
    • 2020-02-15
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多