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 that we need in our application.
Step 1. First, create one ASP.Net website and design it to take input text from the user and their language of choice. For that, you can create one textbox to take input text and one dropdown list 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 parameters.
- Text To Convert
- Application Id
- From Language
- To Language
Prepare the one method which will call the Bing translation service available here with all required parameters like below.
private void TranslateText()
{
string applicationid = "<your Bing appId>";
string fromlanguage = "en"; // From language, change as needed
string translatedText = ""; // Collect result here
string texttotranslate = txttext.Text; // Text to be translated
string tolanguage = ddltolang.SelectedValue.ToString(); // Target language
// Preparing URL with all four parameters
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 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 the application ID, text to translate, from language and to language and simply reading the response given by the Bing translation service.
Step 3. In the button click event, call the above method run your application, and see the result like below.
Conclusion
Using simple steps we can implement a translation service using Bing in our ASP.Net web application.