Thursday, October 27, 2011

Bing Translator, make your own translation application in Visual Studio using C#

I had this task to build a custom translator application to practice a foreign language. Naturally I started to look at the Google Translate API, but found out that Google soon was going to charge money to use it!

So now what, how could I go forward from here? I came to think of Microsoft and found out that Bing also have this translation tools and API:s to use with it, Bing Translator/Bing Translate/Microsoft Translator.

Here is a brief example of how to use the Bing Translator API using Visual Studio 2010 with C#
What you need for this example:
  • First of all you have to obtain an Access Token from Microsoft. Its easy and quick done, so don't stop reading because of this (I know I easily do when I see the word register or something similar)! Getting Started with Microsoft Translator
  • Visual Studio, any version will do, but I use Visual Studio 2010 in this example
Once you got the key, just follow the example below.

Step 1: Create a new Windows Form Project
Create a new Windows Form Application in Visual Studio.
Bing Translator Project
I use .NET framework 3.5, but this example will work with lower versions as well.

Step 2: Create a service reference to the Bing SOAP service
The first thing to do is to add a reference to the Bing translator API. By adding this service reference, Visual Studio creates the necessary code stubs for us to use in our code.
Pick Add Service Reference...

Use the URL http://api.microsofttranslator.com/V2/Soap.svc in the Address field and pick a suitable namespace in the Namespace field, I'm using TranslatorService.

Step 3: Make a simple user interface to test the service
In this simple example we will only use the simplest of controls to test the functionality of the service. So I put one text field to write the word we would like to translate, one button the submit the request and a second text field to put the translation in.

Step 4: Write the code in the click button event
Double click the button you put on the form to get to the code view of the form. First of all we have to tell Visual Studio that we would like to use the service we created in step 2. This is done by adding a using statement at the top of the page:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BingTranslate.TranslatorService;



The last line showing the using statement to use if you have used the same names for the project and the service as I have in this example.

In the button click event we write the short code as follows:


        private void button1_Click(object sender, EventArgs e)
        {
            LanguageServiceClient client = new LanguageServiceClient();
            textBox2.Text = client.Translate("Your Application ID",   textBox1.Text, "en", "fr", "text/html", "general");
        }

The first parameter to client.Translate is the token you received from Microsoft. It is a 40 character word with both numbers and letters. The second parameter is the text we want to translate. The third parameter is the language we want to translate from and the forth is the language we want to translate to. To find all languages you can use, check the documentation Getting Started with Microsoft Translator.

We press F5 and run the application, put the text we want to translate in the first field and click the Translate button.

And there we go!

The code in your Form1.cs should look something like this now:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BingTranslate.TranslatorService;

namespace BingTranslate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LanguageServiceClient client = new LanguageServiceClient();
            textBox2.Text = client.Translate("Your Application ID", textBox1.Text, "en", "fr", "text/html", "general");
        }
    }
}


Conclusion
This example is of course just a pointer, not a "real" application, but from here you should be able to build your own translation application using Bing Translator and Visual Studio!