SIGN UP MEMBER LOGIN:    
ARTICLE

Using the Bing API to Translate Text in ASP.NET

Posted by Krishna Garad Articles | ASP.NET Programming January 31, 2012
In this article we will see how to use the Bing translation API to translate our text from one language to another language.
Reader Level:
Download Files:
 

Introduction

In this article we will see how to use the Bing translation API to translate our text from one language to another language. As all of you know the Google API is no longer available free so our next choice must be Bing. So in this article we will see how to use the Bing translation API.

In our application we need to provide a translation facility to the user. Consider you are building a website in which you can help two people speaking two different languages, say one is French and the second knows English so we can translate their conversation with their respective languages.

Pre-requirements:

To use Bing translation in your application, first you need an API key which you can create here. Login using your Windows Live id and password and create the application id which we need in our application.

Step 1:

First create one ASP.Net web site and design it to take input text from the user and their language of choice. For that you can creaste one textbox to take input text and one dropdownlist to take language choice but in this dropdown you have to keep the value as a language code, like:

For Hindi- hi-IN

For French- fr

For Spanish- es etc

Step 2:

We will make a web request to the Bing translation service from our application by passing the parameters. This Bing translation service takes four parameter as:

  1. Text To Convert

  2. Application Id

  3. From Language

  4. To Language

Prepare the one method which will call the Bing translation service available here. With all required parameter like below.

private void TranslateText()
    {
        string applicationid = "<your Bing appId>";
        string fromlanguage = "en";//from language you can change this as your requirement
        string translatedText = "";//collect result here
        string texttotranslate = txttext.Text;//what to be translated?
        string tolanguage = ddltolang.SelectedValue.ToString();//in which 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();

        Response.Write("Converted Texts Are: " + translatedText);
        response.Close();
    }

In the above code we are making one web request to the Bing translation service with application id, text to translate, from language and to language and simply reading the response given by Bing translation service.

Step 3:

In the button click event, call the above method and run your application and see the result like below.

result.bmp
 

Conclusion

Using simple steps we can implement a translation service using Bing in our ASP.Net web application.

Login to add your contents and source code to this article
share this article :
post comment
 

hiiiii sir i tried searching in net for the code word for kannada..but i din get it so pls help me out to find code for kannada language sir plss....thanks in advance

Posted by sagar jjjjj Mar 31, 2012

You can show the returned result in literal control or you may try the string's split function to get the exact result. If you need to convert in kannad language then first find out the language code of kannad and pass it instead of hindi.

Posted by Krishna Garad Mar 30, 2012

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">????</string> ,,,sir i m getting this output when i gave input text as Sagar .....so plss help me out to get exact output...what i should i do if i wanna translate to kannada...pls help thanks

Posted by sagar jjjjj Mar 30, 2012

hiiiiiii sir nice post but i got error as "The remote server returned an error: (400) Bad Request." in this particular line WebResponse response = request.GetResponse();... so pls help me out sir thanks in advance

Posted by sagar jjjjj Mar 30, 2012

hi sir nice post but i got this error "The remote server returned an error: (400) Bad Request." in this particular line "WebResponse response = request.GetResponse();" can u pls help me out sir thank u in advance

Posted by sagar jjjjj Mar 30, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor