BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

.NET Extension Methods For Microsoft Translator API

by Roopesh Shenoy on Aug 29, 2011 |

 Microsoft Translator API exposes Translation services for developers using REST-based and SOAP based end-points. An open source project, Bing (Microsoft) Translator .NET, creates a wrapper around these APIs by using extension methods, making it much easier to build .NET applications that can use translation.

The Microsoft Translator API provides a lot of features such as -

  • Detecting a language
  • Translation text from one language to another
  • Text-to-speech conversion for a selected set of languages
  • Reformatting text, such as breaking text into a set of sentences

When using these features in a .NET application though, they have to be called using HttpWebRequest object, which is not very intuitive and also quite verbose. What the Bing .NET Extensions library does, is provide extension methods to .NET objects, to make these calls much simpler.

For example to translate a text input in English to Arabic, instead of

string uri = http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=+  appId + "&text=" + 
  tobetranslated + "&from=" + fromLang +  "&to=Arabic”;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
WebResponse response = request.GetResponse();
Stream strm = response.GetResponseStream();
StreamReader reader = new System.IO.StreamReader(strm);
translatedText = reader.ReadToEnd();

using the Translator .NET library, you can now just write

translatedtext = tobetranslated.BingTranslate(BingLanguage.English,BingLanguage.Arabic)

In fact, you can even use the language detection feature to avoid mentioning the source language -

translatedText =  tobetranslated.BingTranslate(BingLanguage.Arabic);

There are other extension methods provided for the other Bing Translation API features listed above. A full description of how to use these features is mentioned in Mohammed’s blog post.

To learn more about Microsoft Translator APIs, you can refer the developer documentation

Extension methods is a NET feature introduced in .NET Framework 3.0 that allows you to add methods to existing types, without creating a new derived type, recompiling or otherwise modifying the original type. 

 

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT