2012-04-28 91 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