.NET Extension Methods For Microsoft Translator API
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.
York Xyander, Bodo Junglas Jul 31, 2015