Using the Bing API to Translate Text in ASP.NET


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.


Similar Articles