A while back immediately after MIX10 I started messing with Microsoft Translator APIs for Silverlight applications. I also got some people asking about Windows Phone 7 stuff and messed around with that a bit. Here’s some post for reference:
- Make your Silverlight applications speak to you
- Using XNA libraries in Silverlight for Windows Phone 7 (with Translator as an example)
In talking with the Translator team following MIX (where they announced they were working on a Silverlight class library for the API. It was good to interact with their team to understand their direction and provide some feedback on how they were approaching it. In the meantime, with their direction, I had started working on a simple wrapper for myself while writing the Translator for Seesmic plugin I was writing. I’ve received a few inquiries on Translator so I thought I’d post my library here for you to see/use.
NOTE: This comes with a ‘works on my machine’ warranty – which means no warranty. There are some things that should be done to make this a more proper async API (noted below). You will also need your own Microsoft Translator application ID (API key) in order to use it.
The API is fairly simple and maps to some of the functions of the Translator HTTP-based API. The following methods are implemented:
You’ll notice that not all the API endpoints are implemented. Honestly I picked what I was using myself but also what I think would be most useful to application developers.
Because this is a service, the wrapper implements the above functions as asynchronous methods, so you will see:
Clever naming, huh? It’s very simple to use and here’s a snippet of a BASIC translation implementation:
1: using System;
2: using System.Windows;
3: using System.Windows.Controls;
4: using TimHeuer.Silverlight;
6: namespace SilverlightApplication147
8: public partial class MainPage : UserControl
10: TranslatorClient _translator;
12: public MainPage()
16: _translator = new TranslatorClient("YOUR_APP_ID");
17: _translator.TranslateCompleted += new EventHandler<TranslateCompletedEventArgs>(OnTranslateCompleted);
20: void OnTranslateCompleted(object sender, TranslateCompletedEventArgs e)
22: Dispatcher.BeginInvoke(() =>
28: private void TranslateButton_Click(object sender, RoutedEventArgs e)
30: // if you needed to detect the source language first you would run DetectAsync to get the Source Language
31: // below is an example of TranslateAsync("Du bist wie eine Blume", "de", "en")
32: _translator.TranslateAsync(TextToTranslateTextBox.Text, SourceLanguageTextBox.Text, TargetLanguageTextBox.Text);
So that’s it. What are the plans here? We have been exploring shipping an official translator extension as a part of the Silverlight Toolkit perhaps if folks find it useful. As I mentioned there are a few things that should probably be changed in this library here, namely making the event arguments be AsyncEventArgs to better match what they are with the Silverlight networking stacks, etc.
I also was messing around with the InstallShield Limited Edition that comes with Visual Studio 2010 so I wrapped all these into an installer for easier deployment (it also includes the source) that you can get here: TimHeuerTranslatorClientSetup.exe
Hope this helps and let me know if you have feedback!