Recommended

Skip Navigation LinksHome / Articles / View Article

Silverlight 3 with Bing Translator Services

+ Add to SilverlightShow Favorites
2 comments   /   posted by Thanigainathan Siranjeevi on Jun 19, 2009
(1 votes)

I was working with one of the web application localization. I was searching with couple of translator's available with the web. The most popular of them are

1. Google Translation Service

2. Bing Translator Service

I tried using the Google API for the web and it was pretty good. Then in recent times I came across Bing translator and tools link in them gave me the way to implement the Bing API. Bing is making a revolution in the search engines and so I liked to use that here.

Following URL's are referred for this.

  1. http://www.bing.com/developers/appids.aspx
  2. http://msdn.microsoft.com/en-us/library/dd576292.aspx
  3. http://msdn.microsoft.com/en-us/library/dd877917.aspx
  4. http://www.bing.com/developers/appids.aspx

     

Application can be downloaded from here.

Bing API:

Bing API is one of the new and cool features from Microsoft. We can use those API's for implementing the customization of search in our Application. There are three kinds of services available for us to implement.

  1. AJAX Interface
  2. HTTP
  3. SOAP

We can use the Ajax interface for our web applications. For Silverlight application it will not be so best to use them. Instead we can use the SOAP reference which will be pretty much easy. I will explain those steps below.

Create a new "Silverlight Business Application" from the templates available or a Normal application if you don't want any prebuilt customizations.

The next step is the SOAP service reference. The SOAP API path for Bing is as follows.

http://api.microsofttranslator.com/V1/SOAP.svc

Just refer this in the silverlight Application and not in the test web application which comes with the Silverlight ptoject.

Adding this creates all the proxy files needed to work with. The next thing is the Application key. This we can get free from the Bing developer website which is given below. One needs to have a Hotmail ID to get this done.

http://www.bing.com/developers/appids.aspx

Through this link we have to create an AppId which we will be using all around the SOAP calls as this is used to give sandboxing security. Since we have taken the Silverlight Navigation Application we will use the HomePage.Xaml to give all the updates and testing. I will explain the cod below.

 

TranslatorService.LanguageServiceClient ls=new LanguageServiceClient();// Creating Bing SOAP Ref object

string AppId = "8ABC484831F80ABC46C07CBFF2C34FF7884A4168";// This AppId is created for my name

System.Collections.ObjectModel.ObservableCollection<string> results;

System.Collections.ObjectModel.ObservableCollection<string> langcodes;

 

This code block has to be declared at the page level .LanguageServiceClient is the Class which has all the methods needed to do the translation. Hence we will create instance at the page level and use that wherever needed. The ApplicationId will be used in all the calls. We can configure this at the configuration level. For now I have given this at the Page level. The result groups that are returned from the SOAP calls are in the form of ObservableCollection.

The XAML structure I very simple for now. We have source language and destination language which we can populate with the GeLangaugeNames method from SOAP calls. The textboxes are used to display source text and destination text.

<StackPanel Style="{StaticResource ContentTextPanelStyle}">

<controls:Label Content="Language Source"></controls:Label>

<ComboBox x:Name="src"></ComboBox>

<controls:Label Content="Destination"></controls:Label>

<ComboBox x:Name="dest"></ComboBox>

</StackPanel>

<StackPanel Style="{StaticResource ContentTextPanelStyle}">

<TextBlock>Text to translate</TextBlock>

<TextBox x:Name="srcTxt" Width="100"></TextBox>

<TextBox x:Name="destTxt" Width="100"></TextBox>

<Button x:Name="cmdSubmit" Click="cmdSubmit_Click" Width="50" Content="Submit"></Button>

</StackPanel>

 

The silverlight calls the webservices in Asynchronous Modes only. Hence we need to handle the events to fetch the values. We have to get the results inside those event definitions. Following are the delegate for event's which we can give in the Page load event. Also we have to call the dropdown load events to load the languages.

//Event Handlers

ls.TranslateCompleted +=new EventHandler<TranslateCompletedEventArgs>(ls_TranslateCompleted);

ls.GetLanguageNamesCompleted +=new EventHandler<GetLanguageNamesCompletedEventArgs>(ls_GetLanguageNamesCompleted);

ls.GetLanguagesCompleted +=new EventHandler<GetLanguagesCompletedEventArgs>(ls_GetLanguagesCompleted);

//For filling the dropdown's and Language codes

ls.GetLanguageNamesAsync(AppId,this.Language.IetfLanguageTag);

ls.GetLanguagesAsync(AppId);

 

//Get the translated text

protected void ls_TranslateCompleted(Object sender,TranslateCompletedEventArgs e){

if(e.Error == null){

destTxt.Text = e.Result;

}

}

 

//Get Languages

protected void ls_GetLanguagesCompleted(object sender, GetLanguagesCompletedEventArgs e) {

if (e.Error == null) {

langcodes = e.Result;

}

}

 

//Fill the dropdown's

protected void ls_GetLanguageNamesCompleted(Object sender, GetLanguageNamesCompletedEventArgs e)

{

if (e.Error == null)

{

results = e.Result;

this.src.ItemsSource = results;

this.dest.ItemsSource = results;

}

}

 

private void PerformTranslation() {

if (src.SelectedItem != null && dest.SelectedItem != null)

{

ls.TranslateAsync(AppId, srcTxt.Text, (langcodes[src.SelectedIndex].ToString()), (langcodes[dest.SelectedIndex].ToString()));

}

}

//Translate on button click

private void cmdSubmit_Click(object sender, RoutedEventArgs e)

{

PerformTranslation();

}

 

The final output will be like as follows.

Application can be downloaded from here.

Conclusion:

The translation so far tried is working fine. The Google service is providing more languages that this. In few months Microsoft will release those language features also. We can expect that. There is one more feature called Transliteration available in Google service. I Bing also give that then it will be so nice. Readers can also refer to how the Ajax and HTTP are used. The same code may also work for web application other than Asynchronous mode which is for Silverlight.

Share


Comments

Comments RSS RSS
  • RE: Silverlight 3 with Bing Translator Services  

    posted by Sandeep Aparajit on Sep 25, 2009 20:04
    Good one!Thanks!

    Sandeep Aparajit

    http://sandeep-aparajit.blogspot.com


  • RE: Silverlight 3 with Bing Translator Services  

    posted by Michael on Mar 14, 2010 11:07
    A very nice service.

    But is there any way to use just AJAX ? Or basic sockets ? Without SOAP (I'm not confortable with SOAP :P).

    Michael, from Sécurité Informatique and official ;)

Add Comment

 
 

   
  
  
   
Please add 1 and 6 and type the answer here:

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)