Posted by: regev | July 27, 2009

PDA Translator with Bing

Recently I’ve recommended Maarten Struys’ video on how to use Bing search in mobile device applications, and today I’d like to show how easy it is to use Bing for translating text. The following sample is a device application project but the same code can be used in desktop applications or any other for that matter.
Here are the simple steps to create a Translator application:

1) Create a new device application.
2) Drop two text-boxes, one button, and one combo-box on the form (see GUI design below).
3) Now let’s name some names: txtTraslateFrom, txtTranslatedText, btnTranslateTo, and cmbTargetLanguage (accordingly).
4) Set the Multiline property of the text-boxes to True and clear their Text.
5) Set the button’s Text to: “Translate To:”.
6) Add three items (target languages) to the combo-box: “Japanese”, “English” and “Hebrew”.
7) Now the fun part: add a web reference to:
http://api.microsofttranslator.com/V1/SOAP.svc

8) Add the following member to the form’s class:

com.microsofttranslator.api.Soap translatSrv = new
       com.microsofttranslator.api.Soap();

9) Double-click the button and add the following code to the Click event handler:

private void btnTranslateTo_Click(object sender, EventArgs e)
{
 try
 {
   txtTranslatedText.Text = "";
   string toLang;
   if (cmbTargetLanguage.SelectedIndex == 0)
      toLang = "ja-JP";
   else if (cmbTargetLanguage.SelectedIndex == 1)
      toLang = "en-US";
   else
      toLang = "he-IL";
   txtTranslatedText.Text = translatSrv.Translate(
      "384136B94C04C644CAB3949013FBFAC36615CA2D",
      txtTraslateFrom.Text, "", toLang);
   }
 catch (Exception ex)
 {
     MessageBox.Show("Error during translation:\n" + ex.Message,
       "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand,
       MessageBoxDefaultButton.Button1);
 }
}

And Bing: there you have it!

RegevsTranslator

(*) Two things to note here:

1) The first parameter passed to the Translate method is my application’s unique ID. To create your own AppID, go to the Bing Developer Center at:  http://www.bing.com/developers/createapp.aspx.
2) In order to see different languages on your PDA, you’ll need to have the appropriate fonts installed.

The project’s source code can be downloaded from here.


Leave a response

Your response:

Categories