Using Google's Translation API from .NET

Tags: Google, API, Translation

Today I experienced what lazy translators think they can get away with because their clients can't speak a language (which was what prompted the need for a translator). I thought to myself that present day translation technology was far enough advanced to do it better. I had a look at Google's translation API today. As usual Google rocks, so why shouldnt their translation API?

The API is focused on JavaScript, but there is a REST interface for it which you can implement in your own .NET application. The response is JSON formatted, but with the JavaScriptSerializer from ajax.asp.net you can easily deserialize this response into your own .NET class.

In the code sample below you will see that I have created two classes to received the response. The container class is a generic class that can take different types of responses. All of Googles AJAX APIs return a response where the object property is responseData, so I created the generic class to have this property, and then you can specify what type the responseData actually is. Since this example uses the translation API I created the TranslationResponse class to hold the response values.

C#

public class GoogleAjaxResponse
{
	public T responseData = default(T);
}

public class TranslationResponse
{
	public string translatedText = string.Empty;

	public object responseDetails = null;

	public HttpStatusCode responseStatus = HttpStatusCode.OK;
}

Now that we have these two classes we can make a web request to get the response and deserialize it using the JavaScriptSerializer, and now you have the JSON response from the Google API ready to use in your .NET application.

C#

public class Translator { private static JavaScriptSerializer serializer = new JavaScriptSerializer(); public static string TranslateText(string inputText, string fromLanguage, stringtoLanguage, string referrer, string key) { string requestUrl =string.Format("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={0}&langpair={1}|{2}&key={3}", System.Web.HttpUtility.UrlEncode(inputText), fromLanguage.ToLowerInvariant(), toLanguage.ToLowerInvariant(), key); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestUrl); req.Referer = referrer; try { HttpWebResponse res = (HttpWebResponse)req.GetResponse(); string responseJson = new StreamReader(res.GetResponseStream()).ReadToEnd(); GoogleAjaxResponse translation = serializer.Deserialize>(responseJson); if (translation != null && translation.responseData != null && translation.responseData.responseStatus == HttpStatusCode.OK) { return translation.responseData.translatedText; } else { return string.Empty; } } catch { //TODO: Add error handling code here. return string.Empty; } } public static string TranslateText(string inputText, CultureInfo fromLanguage,CultureInfo toLanguage, string referrer, string key) { return TranslateText(inputText, fromLanguage.TwoLetterISOLanguageName, toLanguage.TwoLetterISOLanguageName, referrer, key); } }

You will notice that I have exposed the fields of the classes as public, which is not according to best practice, but this is just a demonstration. I will probably write them up nicely later.

Latest Tweets