Ales Rosina's blog

shelastyle.net
Subscribe

Translations in .NET Applications



 
{{ca|La pedra de Rosetta resolgué un problema ...

In developing applications we all came to the point, when it would be nice, to have some kind of translations, let it be user interface translations, maybe some user input data, that needs to be instantly translated, etc. There are millions of use cases how we could create better user experience with a “simple” translations. But this can become very complicated, if we need to translate some text on the fly and can even be very expensive to translate static text in application into, let’s say, 50 different languages.
But today’s machine translations are getting better and better, with a lot of interesting techniques on how to translate text. I will not go deep into that subject, since it’s too complex for just one blog post :)

There are a lot of free services for translations, some are good, some totally suck. The most famous (and probably the best free one) is Google Translate tool, which also allows developers to use it programmatically. The second one is by Microsoft, called Microsoft Translator (or Bing Translator). Big difference between Google and Bing is number of supported languages – Google have 51 languages, Bing has only 22. But for .NET developers, Bing is much easier to implement. So we are going to see, how to implement those two services into simple Console application.

Both have very good written documentation (Google’s documentation and Bing’s one), the only problem is, that Google’s service is made only for AJAX apps. With that in mind, we must know, that means parsing JSON in .NET. Luckily, we have a lot of parsers already written. I have chosen open source library called LitJSON. When we import this library into project, it’s very easy to use it. But for customizing it to Google Translate, we need to do some hacks, which is very well written in this blog post by Dan Matthews, so I’ll do just a quick overview. The most important line of code is

WebRequest oRequest = WebRequest.Create(
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
+ HttpUtility.UrlEncode(userInput) + "&langpair=%7Cen");

We have defined here, where is Google Translator located with all necessary attributes. Attribute langpair=sl|en defines from and to which language should service translate text (in this example, from Slovenian to English). If you leave the first language empty, than Google will try to detect the correct language. You should note here, that longer the text is, we have better chance of correct language detection. As I said, for more details check Dan’s blog post.

Now let’s take a look at Microsoft solution. As expected, they have a “true” SOAP service, which means, that you can implement this service with two clicks in Visual Studio. There is only one more step, before you can start using this service, registration of your application. Just go to this address and log in with your LiveID, fill in the form and you will get API ID. When you’re done with that, add a service reference – in Solution Explorer inside Visual Studio, right-click on References folder, and choose Add Service Reference, paste this http://api.microsofttranslator.com/V1/SOAP.svc URL inside box and press Go. Just name the namespace LiveTranslator for example, and you’re ready to use in it in code. Here is example code:

Console.Write("Input text to translate: ");
string userInput = Console.ReadLine();
//create web request to Microsoft Translator LiveTranslator.LanguageServiceClient cl =
new ConsoleApplication2.LiveTranslator.LanguageServiceClient(); //just for checking, let's write out, which language did service detect
//note: instead of myApiId you shoud write in your api id
string detectedLang = cl.Detect("myApiID", userInput); Console.WriteLine("Detected language: " + detectedLang); //write out transaltion Console.WriteLine(cl.Translate("myApiId", userInput, "", "en"));
That’s it! As we can see, code for Bing translator is much more clean, but we could also write a small wrapper for Google Translate, if we need to do a lot of translations inside our app.

You can also download example application on my SkyDrive. Any questions? Just leave a comment!
Reblog this post [with Zemanta]
   

author: Aleš Rosina | Comments: 2 |
09
January
2010

 
John Sheehan on 16. January 2010 at 22:35

For using the Google API, you should try restsharp.org

Aleš Rosina on 16. January 2010 at 23:09

Looks cool! I'll give it a try, thanks for the link.

Leave a Comment

Name:
Email:
I respect your privacy, so your email is never published.
Web site: