Using Translation Services within ASP.NET MVC 3 and jQuery
Posted by: Malcolm Sheridan
in Category ASP.NET MVC
Abstract: The following article demonstrates how to use Microsoft’s Translation service within your ASP.NET MVC application using jQuery.
Every once in a while, you have text that you need to translate. I ran into this recently where the user would login, go to a specific page and be able to type in text and have it translated on the fly. After some research I found that Microsoft had a service called Microsoft Translation Service. This is an online service which can be accessed via AJAX, HTTP or SOAP. Because I wanted this to run on the client, I stuck with AJAX. I’m sure there are other services out there which can do this just as good, but I used Microsoft’s product and found it to be good. And here what I found.
Creating Your AppID
Before you can do anything you’ll need to create a Bing API AppID. The ID is passed as a parameter to the translation service to identity you. Without it you won’t be able to do anything. You’ll need to visit this site and follow the instructions to create the AppID.
Let’s get started. I’m using MVC 3 in this demo but there’s nothing stopping you taking the same code and dropping it into a Web Forms application.
The parameters that you pass to the service are outlined below.
text – the text to be translated
to – the target language
appid – your AppID to uniquely identify yourself
Once the DOM has finished loading, I’m setting up the functions to handle the event when the user submits the form. I’m doing this as I want to send the form via Ajax and want to avoid the page refresh.
Once the service has finished, the injected script tag is removed from the DOM and the oncomplete function is invoked and the results are displayed to the user.
It didn’t take long to get this working. This was an easy way for the users to translate text when they needed it on the fly. Thanks Microsoft!!
The entire source code of this article can be downloaded over here