2012-04-28 52 views
0

以下是我使用Google翻译的代码。我有一个DLL,我添加为参考:Google.Apis.Translate.V2
我也买了谷歌翻译API密钥。因缺少命名空间而无法使用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翻译网站时尽可能快地翻译它。

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

回答

0

在项目中,添加一个引用到这个组件:

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

可能还有其他你需要,我只是抬头这一项。

+0

找不到其他命名空间的DLL可能它的事情与Java脚本im不知道。 – user1363119 2012-04-28 17:50:56

+0

史蒂夫感谢链接中的DLL固定datacontract,但我仍然无法找到如何解决TranslationRootObject – user1363119 2012-04-28 17:51:52

+0

看起来像它是其中之一:使用Google.Apis.Translate.v2; using Google.Apis.Translate.v2.Data; 使用Google.Apis.Translate.v2.Data.TranslationsResource;他们可以是一个或多个组合。 – 2012-04-28 21:42:48

相关问题