Calling Google AJAX Language API in C# .NET
Calling AJAX Language API from Non-Javascript environments, the API exposes a simple RESTful interface. In all cases, the method supported is
GETand the response format is a JSON encoded result with embedded status codes. Details here.Example in javascript.
google.language.translate(“Hello world”, “”, “it”, function(result) {
if (!result.error) {
var container = document.getElementById(“translation”);
container.innerHTML = result.translation;
}
});
And now the same code in C#:
WebRequest request = WebRequest.Create(“http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=%7Cit”);
// This command performs a Language Translation(
/ajax/services/language/translate), for Hello World (q=hello%20world)// from English to Italian (
langpair=en%7Cit). i.e. en|it . Or you can leave the source language blank if you want auto-detection & translation.WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadLine();JsonObject account;
JsonObject responseData;
JsonString responseDetails;
JsonNumber responseStatus;using (JsonParser parser = new JsonParser(new StringReader(json), true))
account = parser.ParseObject();//responseDetails = (JsonString)account["responseDetails"];
responseStatus = (JsonNumber)account["responseStatus"];
responseData = (JsonObject)account["responseData"];Console.WriteLine(“responseStatus: {0}”, responseStatus.Value);
Console.WriteLine(“translatedText: {0}”, responseData["translatedText"]);
Console.WriteLine(“detectedSourceLanguage: {0}”, responseData["detectedSourceLanguage"]);// You can get the JSON .NET 2.0 parser I use from here.
// Or if you have .NET 3.5 you can use DataContractJsonSerializer to serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects.

Hi Vincent,
Nice code, Im using it on my house and runs fine, but on my work Im getting this error:
System.Net.WebException: The request failed with HTTP status 403: Access Forbidden.
Could you help me?
You can also use GAPI.Net
I have developed Free Online Translation Service page which use Google AJAX Language API. I also used Google AJAX Language API to translate the user interface of Online Translation Service to the following languages: English, Arabic, Bulgarian, Chinese, Croatian, Czech, Danish, Dutch, Finnish, French, German, Greek, Hindi, Italian, Japanese, Korean, Norwegian, Polish, Portuguese, Romanian, Russian, Spanish, Swedish.
Thanks, this example helped me. I was working on a project in which I needed to parse some JSON data returned from the Google API so I appreciate this tip.
Hi
I am working in Asp.Net 2.0 and C#.
I want convert language English to Hindi using google api. I am facing problem .Plz tell me how to use google api in C#.
Hi
I am working in Asp.Net 2.0 and C#.
I want convert font of text and save in xml file with C# .Please help me how to work it.
thanks
As you mentioned, the DataContractJsonSerializer class in System.Runtime.Serialization.Json is a cleaner way of doing this. It’s pretty similar approach to XML Serialization, so you’re dealing with a nice set of data classes.
Good example of how to use it here:
http://www.ben-morris.com/google-search-api-deserialize-json-output-net