user1960072
user1960072

Reputation:

getting the translated language using google translator in winform apps

I am trying to translate a string from "English to Bangla" in my WinForms app. I tried this code:

string input = "i eat rice";
string languagePair = "en|bn";

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("<span title=\"") + "<span title=\"".Length);
result = result.Substring(result.IndexOf(">") + 1);
result = result.Substring(0, result.IndexOf("</span>"));
MessageBox.Show(result.Trim());

But what I got is:
&#2438 &#2478 &#2495 &#2477 &#2494 &#2468 &#2454 &#2494 &#2439
But if I put it in Google's search box then it shows my translated language into the search box. How can I get the translated language to show in my WinForm? N.B: I don't want to use google translator API.

Upvotes: -1

Views: 1772

Answers (1)

Richard Schneider
Richard Schneider

Reputation: 35477

The result you are getting, &#..., is the HTML entity encoding of each UTF-16 character. You can use HttpUtility.HtmlDecode or WebUtility.HtmlDecode to get the actual unicode string.

result = HttpUtitlityDecode(result.Trim());
MessageBox.Show(result);

See Decoding all HTML Entities for more details.

Upvotes: 3

Related Questions