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.

Saturday, April 9, 2011

When not to use Count in C#

During my daily development I have to deal with IEnumerables quite often. Old habits takes time to go but now I try to use LINQ to Objects as much as possible in my code while dealing with IEnumerables. LINQ to Objects or LINQ in general is one of the best thing that have been added in C# in past couple of years in my personal opinion.

As I said, old habits take time to go, one habit which I am trying to get rid of these days is to avoid using Count property. At countless places in my code I have used this piece of code and I am sure many of you would have done same

   1:  if(SomeCollection.Count == 0)
   2:      DoSomething();
   3:  else
   4:      DoSomethingElse();
In above piece of code you just want to know whether the collection contains any item or not and based on that you will perform some operation. Problem is that to find this yes or no you are traversing the whole list when you use Count. Let's say your collection contain 1000 element. You are traversing whole list just to find out whether there is any item in the list or not. Which doesn't make any sense at all. Instead what we can use is a simple LINQ method 'Any'.

   1:  if(SomeCollection.Any())
   2:      DoSomething();
   3:  else
   4:      DoSomethingElse();
Use of 'Any' will make your code much faster because instead of traversing whole collection 'Any' will return as soon as it finds that there is an element in your collection or even in case there isn't any at all. This is just one example I am sure there will be many more examples in my daily development which can be simplified using LINQ to Objects.

Note that "if you are starting with something that has a .Length or .Count (such as ICollection<T>, IList<T>, List<T>, etc) - then this will be the fastest option, since it doesn't need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any() to check for a non-empty IEnumerable<T> sequence. For just IEnumerable<T>, then Any() will generally be quicker, as it only has to look at one iteration. However, note that the LINQ-to-Objects implementation of Count() does check for ICollection<T> (using .Count as an optimisation) - so if your underlying data-source is directly a list/collection, there won't be a huge difference. In general with IEnumerable<T> : stick with Any()." Taken this paragraph from here

Saturday, April 2, 2011

How to find is DataGrid have Scroll Bars in WPF?

One of my WPF application was using a DataGrid and I needed to find out at a certain point of time whether the Scroll Bar (vertical or horizontal) is visible or not. If you ever run into scenario like this you can find this out using code given below.

   1:  ScrollViewer scrollViewer = FindVisualChild<ScrollViewer>(dataGrid);
   2:  Visibility verticalBarVisibility = scrollViewer.ComputedVerticalScrollBarVisibility;
   3:  Visibility horizontalBarVisibility = scrollViewer.ComputedHorizontalScrollBarVisibility;
In code snippet above 'dataGrid' is my DataGrid.

   1:  private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
   2:  {
   3:       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
   4:       {
   5:            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
   6:            if (child != null && child is childItem)
   7:                 return (childItem)child;
   8:            else
   9:            {
  10:                childItem childOfChild = FindVisualChild<childItem>(child);
  11:                if (childOfChild != null)
  12:                     return childOfChild;
  13:            }
  14:       }
  15:  return null;
  16:  }
After running this piece of code in 'verticalBarVisibility' and 'horizontalBarVisibility' you will have the exact state of DataGrid's vertical and horizontal scroll bars.