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 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(“”);

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


9 thoughts on “Calling Google AJAX Language API in C# .NET

  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?

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

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

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

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s