Monday, 25 February 2013

language translation using Bing API


protected void Button1_Click(object sender, EventArgs e)
  {
                // Bing ApI Key
            string applicationid = "68D088969D79A8B23AF8585CC83EBA2A05A97651";           
            string fromlanguage = "en";  // From language
            string translatedText = "";
            string texttotranslate = txtFrom.Text; // Text to translate
            string tolanguage = "hi";//To language?
           
            //preparing url with all four parameter
            string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" + applicationid
                       + "&text=" + texttotranslate + "&from=" + fromlanguage + "&to=" + tolanguage;           
            //making web request to url
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            //getting response from api
            WebResponse response = request.GetResponse();

            Stream strm = response.GetResponseStream();

            StreamReader reader = new System.IO.StreamReader(strm);
            //reading result
            translatedText = reader.ReadToEnd();           
            dvTo.InnerHtml = translatedText;           
            response.Close();           
   }


    <div>
        <asp:TextBox ID="txtFrom" runat="server" TextMode="MultiLine" Height="88px"   
Width="235px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <div id="dvTo" runat="server" > </div>
    </div>

No comments:

Post a Comment