Friday, August 28, 2009

Google Translate: Web Services written in languages other than English

A utility function that can be used when dealing with web services written in languages other than English that returns value in foreign language like french, even the variable names are in that language and the error messages returned. With this utility function it's very easy to debug the application. Just add this function in one of the utility class in yur project.

public
string TranslateText(


string input,


string languagePair)

{

string url =

String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}",

input, languagePair);

WebClient webClient = new
WebClient();

webClient.Encoding = System.Text.Encoding.UTF8;

string result = webClient.DownloadString(url);

result = result.Substring(result.IndexOf("id=result_box") + 24);

result = result.Substring(0, result.IndexOf("</div"));

return result;

}

Usage: you can call this function like this

TransalteText(inputString, "ar|en")

It will convert the inputString from arabic to english and return the english text

It will convert the inputString from arabic to english and return the english text