DCSIMG
Google Translator service for Windows Phone 7 - Vangos Pterneas blog - Site Root - StudentGuru

Google Translator service for Windows Phone 7

In my previous blog post, Bing Translator for Windows Phone, I explained how you can develop a simple translation service using Bing's SOAP API. This time, I'll demonstrate how you can develop the same service using Google's REST API.

Download the application source code and read the following tutorial to learn how to develop it yourself.

Note: REST is an HTTP-based architectural style, used to send and receive data. It is generally more light-weight than SOAP, but it does not expose methods. As a result, we need to handle the data ourselves in order to extract the desired values. Data can be serialized either as XML or JSON (Google Translate uses JSON format).

Windows Phone 7 Google Translate UI

Step 1: The user interface

The user interface is exactly the same as the one we created for Bing. Launch Visual Studio, create a new Windows Phone 7 portrait application, name it "WindowsPhoneGoogleTranslate" and start dragging-n-dropping the appropriate controls: Two list boxes (for language selection), two text boxes (for input and output text) and a button (for translation). Here is the corresponding XAML code:

Step 2: Creating the languages source

Copying from my previews post, each language has two properties: A name (such as "English", "French", "Greek", etc) and a unique code (such as "en", "fr", "el" respectively). An object containing both properties is necessary, so create the following class and place it in a Language.cs file:

The user needs to see the language name only - and not the code. So, when retrieving the appropriate data, only the Name property will be displayed in the UI. Modify the DataTemplate of the list boxes to achieve this:

Now navigate to MainPage.xaml.cs and create a list to store the languages:

Unfortunately, the Google API offers no means to retrieve the available languages programmatically. As a result, I had to hard-code them in an XML file named Languages.xml. Its structure is the following (you can easily add more languages if Google ever extends its API):

The above languages ought to be displayed when MainPage.xaml.cs finishes loading. I have used a simple LINQ-to-XML query, but you can use whichever retrieval method you are comfortable with. The query selects the values of the XML file's nodes and then populates our Language list. Finally, the list is binded to the proper list boxes.

Step 3: Using Google REST services

Unlike SOAP, REST does not require a service reference addition. The results are generated according to the query-string we provide. Here is an example of a valid Google Translate query-string which translates "Hello world" from English (en) to French (fr):

https://www.googleapis.com/language/translate/v2?key=YOUR-API-KEY&source=en&target=fr&q=Hello world

Firstly, obtain a free Google application key and paste it in the above URL replacing "YOUR-API-KEY" parameter. Then, type the URL in your browser address bar and hit enter. The browser should respond the following:

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}}

This is nothing but a simple JSON response containing the text value we want! Let's find out how we can get that value programmatically.

We need a pattern for the Google API URL, specifying the proper parameters (key, source, target and q). Trying to keep the code snippets simple, I consider the key parameter as not a subject to change.

Someone has to manage the web requests and web responses now. Fortunately, Silverlight supports WebClient class for this work, making our life easier!

WebClient generates a proxy able to create an HTTP request and then download its response in plain text format. We only need to handle DownloadStringCompleted event handler (declare it in MainPage constructor):

DownloadStringCompleted will be called right after proxy's DownloadStringAsync method makes the HTTP request and finishes downloading the response. So, let's navigate to our button's event handler and specify the source, target and q parameters in SERVICE_URL according to the user's choices:

Handling DownloadStringCompleted is pretty straightforward. It only extracts the desired text from the JSON response and assigns it to the output text box. e.Result contains the whole JSON response. Note: For simplicity, I have used a short regular expression in order to parse JSON. Windows Phone supports DataContractJsonSerializer, which is the appropriate way for object serialization and deserialization.

We are done! Windows Phone translates text using Google API! Once again, here is the application (download source code):

Windows Phone 7 Google Translate

Resources

Compare this post with Bing Translator service for Windows Phone 7.

  • Anonymous
    Anonymous

    Thanks for an idea, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

  • Anonymous
    Anonymous

    I have an error, when i am click on translate button "The remote server returned an error: NotFound."

    PLease Help.