I wrote a metro style application to call bing translator, but it didnt work. I tried to debug this program, but the UI does not respond. I can't figure if it is the URI I am using, or some other problem.
Here my code :
public string GetTranslatedText(string textToTranslate, string fromLang, string toLang)
{
string translation;
if (fromLang != toLang)
{
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" +
appID + "&text=" + textToTranslate + "&from=" + fromLang + "&to=" + toLang;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
try
{
WebResponse response = request.GetResponse();
Stream strm = response.GetResponseStream();
StreamReader sr = new StreamReader(strm);
translation = sr.ReadToEnd();
response.Close();
sr.Close();
}
catch (WebException)
{
MessageBox.Show("Ensure that you are connected to the internet.",
"Translator", MessageBoxButton.OK, MessageBoxImage.Stop);
return (string.Empty);
}
}
else
{
MessageBox.Show("Will not translate to the same language.", "Translator",
MessageBoxButton.OK, MessageBoxImage.Exclamation);
return (string.Empty);
}
// Parse string into an XElement and get the XElement Value
// which is returned as the translated text.
return (XElement.Parse(translation).Value);
}