Click here to Skip to main content
11,790,613 members (49,253 online)
Click here to Skip to main content

Tagged as

Google Translator service for Windows Phone 7

, 20 Dec 2010 CPOL 13.5K 5
Rate this:
Please Sign up or sign in to vote.
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 tutori

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:

<StackPanel Orientation="Vertical">  
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">  
          <StackPanel Orientation="Vertical">  
               <ListBox Name="lbxFrom" Height="140"></ListBox>  
          <StackPanel Orientation="Vertical">  
               <ListBox Name="lbxTo" Height="140"></ListBox>  
     <TextBox Name="txtInput" Text="" Height="180" />  
     <Button Name="btnTranslate" Content="Translate"  
                 Click="btnTranslate_Click" />  
     <TextBox Name="txtOutput" Text="" Height="180" />  

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:

public class Language  
    public string Code { get; set; }  
    public string Name { get; set; }  

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:

<ListBox Name="lbxFrom" Height="140">  
               <TextBlock Text="{Binding Name}" FontSize="25" />  

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

private List<Language> _languages = new List<Language>();

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):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>  
  <!--<span class="code-comment"> More languages... --></span>  

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.

void MainPage_Loaded(object sender, RoutedEventArgs e)  
    XDocument langDocument = XDocument.Load("Languages.xml");  
    _languages = (from language in langDocument.Descendants("language")  
                  select new Language  
                      Code = language.Element("code").Value,  
                      Name = language.Element("name").Value  
    lbxFrom.ItemsSource = _languages;  
    lbxTo.ItemsSource = _languages;  

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): 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.

private readonly string SERVICE_URL =   
        "" +   

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

private WebClient  _proxy = new WebClient();

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):

_proxy.DownloadStringCompleted +=   
       new DownloadStringCompletedEventHandler(DownloadStringCompleted);

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:

private void btnTranslate_Click(object sender, RoutedEventArgs e)  
    Language from = lbxFrom.SelectedItem as Language;  
    Language to = lbxTo.SelectedItem as Language;  
    string googleTranslateUrl = string.Format(SERVICE_URL,   
                                from.Code, to.Code, txtInput.Text);  
    _proxy.DownloadStringAsync(new Uri(googleTranslateUrl));  

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.

void DownloadStringCompleted(object sender,  
                             DownloadStringCompletedEventArgs e)  
    string text = Regex.Match(e.Result,   
    txtOutput.Text = text;  

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

Windows Phone 7 Google Translate


Compare this post with Bing Translator service for Windows Phone 7


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vangos Pterneas
Product Manager LightBuzz
United Kingdom United Kingdom
Vangos Pterneas is a Microsoft Most Valuable Professional in the Kinect technology. He helps companies from all over the world grow their revenue by creating profitable software products. Vangos is the owner of LightBuzz Software agency and author of two technical books.

You may also be interested in...

Comments and Discussions

QuestionHelp me Pin
Member 103198069-Oct-13 18:05
memberMember 103198069-Oct-13 18:05 
QuestionPlease help me! Pin
Member 103198069-Oct-13 16:17
memberMember 103198069-Oct-13 16:17 
QuestionService URL error Pin
Robinson jac12-Dec-11 18:54
memberRobinson jac12-Dec-11 18:54 
Questiongetting notfound error Pin
geron.gil8-May-11 22:27
membergeron.gil8-May-11 22:27 
BugRe: getting notfound error Pin
phanikrishnav30-Aug-11 0:41
memberphanikrishnav30-Aug-11 0:41 
GeneralMany thanks Pin
Doug Wesson7-Jan-11 19:17
memberDoug Wesson7-Jan-11 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 20 Dec 2010
Article Copyright 2010 by Vangos Pterneas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid