Compartilhar via


Silverlight 4: How to integrate Microsoft Translator Service into Silverlight and Windows Phone

A few weeks ago, I found the Microsoft Translator Interactive SDK, and I recognized that it is very easy to use in .NET applications, like Silverlight and Windows Phone.

So I started a sample application. After a while I got the idea to build a “UserControl” out of this feature. Why? Because it is “pickable” in the UI for a developer or Designer. The control itself is not visible at runtime, it only appears at designtime, as an ugly looking red box with its name on it.

You can see the App here in action.

Sample of the control in Silverlight 4 App

image

Sample of the control in Windows Phone 7 Application

 image

Why it is so interesting for me? Because you can reuse it very easy, without knowledge of the internas. It has just a few properties for configuration and one Load-Method.

It just has one important property you have to set, the Application Id of Microsoft Translator. You can get one here (https://developer.live.com).

image

The control has two events.

image

To translate text from one language (de-de = Germany) into one other (en-us = English US) is extremly simple. Problems like synchronizing the background thread into the UI-Thread is hidden in the control.

The following code demonstrates the easiness of using.

 private void MicrosoftTranslator_Translated(object sender, TheOliver.Controls.TranslatorEventArgs args)
{
    _out.Text =args.TranslatedText;
}

private void MicrosoftTranslator_TranslationError(object sender, TheOliver.Controls.TranslatorErrorEventArgs args)
{
    _out.Text= args.Error.Message;
}

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
    _mts.Translate(_in.Text, "de-de", "en-us");
}

Here is the complete code of the Microsoft Translator Service:

 // Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TheOliver.Controls
{
    public class MicrosoftTranslator : UserControl, INotifyPropertyChanged
    {
        private string _appId;
        public string AppId
        {
            get { return _appId; }
            set { _appId = value; }
        }

        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
                this.IsIdle = !_isBusy;
            }
        }

        private bool _isIdle = true;
        public bool IsIdle
        {
            get { return _isIdle; }
            set
            {
                _isIdle = value;
                OnPropertyChanged("IsIdle");
            }
        }

        private byte[] _bytes;

        public MicrosoftTranslator()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                StackPanel sp = new StackPanel();
                sp.Background = new SolidColorBrush(Colors.Red);

                TextBlock tb = new TextBlock();
                tb.Text = "[Microsoft Translator Service]";

                sp.Children.Add(tb);
                this.Content = sp;
            }
        }

        public void Translate(string sourceText, string sourceLanguage, string targetLanguage)
        {
            if (this.AppId == string.Empty || this.AppId == null)
            {
                MessageBox.Show("No AppId for Translator service defined. You can get an AppId at https://dev.live.com.");
                return;
            }

            this.IsBusy = true;

            // Creating a string with the request URL
            // The URL includes the Bing developer appId and in this case hardcoded to translate from 
            // English to Spanish
            string translateUri =
                "https://api.microsofttranslator.com/V1/Http.svc/Translate?appId=" + _appId
                + "&from=" + sourceLanguage
                + "&to=" + targetLanguage;

            // Create a web request to the URL
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(translateUri);

            // Set the request method to POST
            httpWebRequest.Method = "POST";

            // Set the content type (needs to be text/plain for POST methods)
            httpWebRequest.ContentType = "text/plain";

            // Encode the txtInput value bytes for the POST
            _bytes = Encoding.UTF8.GetBytes(sourceText);

            // Create a stream for the request
            httpWebRequest.BeginGetRequestStream(new AsyncCallback(OnGetRequestStream), httpWebRequest);
        }

        private void OnGetRequestStream(IAsyncResult iar)
        {
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)iar.AsyncState;
                Stream os = httpWebRequest.EndGetRequestStream(iar);

                os.Write(_bytes, 0, _bytes.Length);

                if (os != null)
                {
                    // Close the stream when finished
                    os.Close();
                }

                // Send the request and get the response
                httpWebRequest.BeginGetResponse(new AsyncCallback(OnGetResponse), httpWebRequest);
            }
            catch (Exception exc)
            {
                RaiseError(exc);
            }
        }

        private void OnGetResponse(IAsyncResult iar)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)iar.AsyncState;

                if (!iar.IsCompleted)
                {
                    Debug.WriteLine("Not completed");
                }
                WebResponse response = request.EndGetResponse(iar);

                // Open a stream for the response
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);

                // Create a string for the contents of the web response
                string output = reader.ReadToEnd();

                // Return the output string
                OnTranslated(output);
            }
            catch (Exception exc)
            {
                RaiseError(exc);
            }
        }

        public event TranslatorEventHandler Translated;
        public event TranslatorErrorEventHandler TranslationError;

        private void OnTranslated(string output)
        {
            this.IsBusy = false;
            if (Translated != null)
            {
                Dispatcher.BeginInvoke(() =>
                    {
                        TranslatorEventArgs args = new TranslatorEventArgs();
                        args.TranslatedText = output;
                        Translated(this, args);
                    });
            }
        }

        private void RaiseError(Exception exc)
        {
            this.IsBusy = false;
            if (TranslationError != null)
            {

                Dispatcher.BeginInvoke(() =>
                    {
                        TranslatorErrorEventArgs args = new TranslatorErrorEventArgs();
                        args.Error = exc;
                        TranslationError(this, args);
                    });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                Dispatcher.BeginInvoke(() =>
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    });
            }
        }
    }

    public delegate void TranslatorEventHandler(object sender, TranslatorEventArgs args);

    public class TranslatorEventArgs : EventArgs
    {
        public string TranslatedText { get; set; }
    }

    public delegate void TranslatorErrorEventHandler(object sender, TranslatorErrorEventArgs args);

    public class TranslatorErrorEventArgs
    {
        public Exception Error { get; set; }
    }
}

I hope you like this sample and the translator app.

Best regards,

The-Oliver