Thursday, August 27, 2009

Language Translation API ASP.NET

private string translateme(string stringToTranslate, string fromLanguage, string toLanguage)

{

const int bufSizeMax = 65536;

const int bufSizeMin = 8192;

try

{

// by default format? is text.

// so we don't need to send a format? key

string requestUri = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + stringToTranslate + "&langpair=" + fromLanguage + "%7C" + toLanguage;

// execute the request and get the response stream

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();

// get the length of the content returned by the request

int length = (int)response.ContentLength;

int bufSize = bufSizeMin;

if (length > bufSize)

bufSize = length > bufSizeMax ? bufSizeMax : length;

// allocate buffer and StringBuilder for reading response

byte[] buf = new byte[bufSize];

StringBuilder sb = new StringBuilder(bufSize);

// read the whole response

while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)

{

sb.Append(Encoding.UTF8.GetString(buf, 0, length));

}

// the format of the response is like this

// {"responseData": {"translatedText":"¿Cómo estás?"}, "responseDetails": null, "responseStatus": 200}

// so now let's clean up the response by manipulating the string

string translatedText = sb.Remove(0, 36).ToString();

return translatedText.Substring(0, translatedText.IndexOf("\"},"));

}

catch

{

return "";

}

}

No comments:

Post a Comment