Vincent Leung .NET Tech Clips

The latest tech clips from the .NET community

Calling Google AJAX Language API in C# .NET


 image

Calling AJAX Language API from Non-Javascript environments, the API exposes a simple RESTful interface. In all cases, the method supported is GET and 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.

 

Developer’s Guide – Google AJAX Language API – Google Code

June 14, 2008 - Posted by Vincent Leung | Google | | 7 Comments

7 Comments »

  1. 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?

    Comment by henrique | June 27, 2008 | Reply

  2. You can also use GAPI.Net

    Comment by liam | August 10, 2008 | Reply

  3. 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.

    Comment by Quick Online Translator | September 21, 2008 | Reply

  4. 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.

    Comment by Joseph Johnson | November 9, 2008 | Reply

  5. 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#.

    Comment by vipin | September 19, 2009 | Reply

  6. 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

    Comment by vipin | September 24, 2009 | Reply

  7. 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

    Comment by Paul k | November 8, 2009 | Reply


Leave a comment