Reputation:
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:
আ ম ি ভ া ত খ া ই
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
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