Sunday, April 10, 2011

How to Translate and Play/Speak using Microsoft Soap APIs

Some options that are provided by Microsoft to do translation in your application are
  • AJAX Interface
  • SOAP Interface
  • HTTP Interface
In this post I am going to explain translation using SOAP related APIs. You can download a ready to run code, which I have used in this post, from here. Note that in order to run the sample code you will have to provide your AppID(discussed below) in AppID variable.

As I mentioned above, first must thing you have to do in order to do translation in your application is get your AppID from Microsoft. Only after getting this AppID you will be able to use translation APIs. This ID is free of charge and you can get it from here. Do read terms and conditions about using free AppID.

In your .Net 3.5 or above application first of all add a service reference of Microsoft's Web Service that provides the translation related services. Web Service address is  I have given it name 'TranslationService' in my code.

   1:  TranslationService.LanguageServiceClient client = new TranslationService.LanguageServiceClient ();
   2:  //translating from "en" english to "de" german.
   3:  string germanString = client.Translate(AppID, "Hello, how are you", "en", "de", "text/plain", "general");
   4:  //translating back from "german" to "english"
   5:  string englishString = client.Translate(AppID, germanString, "de", "en", "text/plain", "general");
In code snippet above, I am first of all creating an instance of LanguageServiceClient. After that I am translating a "Hello, How are you" from English to German and then I am translating result back in to English. AppID in above code is the free AppID I got from Microsoft. Notice that I am passing "text/plain" in content type parameter which tells the translation service that it is a simple plain text. You can also translate HTML pages by passing "text/html" in content type.

Microsoft's translation service's also provide speaking related features. For example if you want to listen/play a string in English language you can do something like this.

   1:  //this method call will return me url of media file that speaks/plays "Hello, How are you"
   2:  string speakEnglishURL = client.Speak(AppID, "Hello, How are you", "en", "audio/wav");
In code snippet above, I am specifying that provide string "Hello, How are you" is in "en" or English. You can use the returned URL to play or listen to the actual sound.

Microsoft has provided a very simple API for programmers and developers for translation and speaking related services. Now its job of developers to come up with creative ideas which can make maximum use of these APIs and change the world :). In my opinion Windows Phone 7 developers can build very powerful apps using these APIs. You can download complete ready to run code from here. Don't forget to get AppID can put it in 'AppID' variable in sample code, otherwise it won't run.

No comments:

Post a Comment