×
Namespaces

Variants
Actions

Archived:Using Google Translator API in Flash Lite

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

Article Metadata
Code ExampleArticle
Created: manikantan (28 Mar 2009)
Last edited: hamishwillee (14 May 2013)

Contents

Introduction

Google’s Translator is one of the most celebrated products from the Google’s Labs. This article describes how to use the Google’s API and create an application from Flash Lite that can be a user interface for the same. You can look at the API and documentation, link here.

Design

The architecture is pretty simple. Create a simple interface in Flash (essentially with an Input TextArea and two Dropdown menus for choice of language). You shall also need an output TextField to display the result. Apart from these, there are other items for the sake of design and looks that can be placed. Once the user enters a text string along with the languages of his choice (source and destination), you need to send these inputs to Google’s server which in turn gives back the results in no time. What needs to be understood is the QueryString that has to be passed. Google’s Translator does not host a web server that accepts web services, but provides access via QueryStrings. These querystrings have a specific format which you will understand soon.

Code Snippet

I shall explain excerpts from the code that will help you understand the structure of the application. Lets assume that we have an input TextArea (named source) and two dropdown menu items (named from, to). There should be a valid input in all these three UI items before sending the request to Google. To display error messages, I use a dynamic TextField called errors.

var lorem_lv:LoadVars = new LoadVars();
// Code in between not shown
 
// Code Block 1
// The following Code has to appear in KeyPress event or Button Click event
if (source.text != "") {
x1 = escape(source.text);
if ((from.selectedIndex == undefined) || (to.selectedIndex == undefined)) {
errors.text = "Select languages ";
errors.textColor = 0xAA0000;
return;
}
source_lang = determine(from.selectedIndex);
dest_lang = determine(to.selectedIndex);
from_to_pair = escape(source_lang+"|"+dest_lang);
lorem_lv.load("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="+x1+"&langpair="+from_to_pair);
errors.text = "Connecting ";
errors.textColor = 0x00AA00;
} else {
errors.text = "Enter input text ";
errors.textColor = 0xAA0000;
}
// Code Block 1 ends here
 
lorem_lv.onData = function(src:String):Void {
// Do the handling of the resultant string.
// Returns Unable to Load external data in case of Connection failure.
}

We use the escape() method to convert the string into URL-Encoded message format. What it essentially does is, convert non-alphanumeric characters to % hexadecimal sequences.

The determine () function is user defined function that returns the language code, as it has to be sent to Google’s server. The list of language pairs and language codes are available at Google’s site. For e.g., English is enumerated as “en” and French as “fr”. This is a paste from the application's source code, which briefs its functionality.

function determine(i:Number):String {
switch (i) {
case 0 :
return "en";
case 1 :
return "cs";
case 2 :
return "nl";
// More languages and their codes can be added here
}
}

Notes

  • Use of languages like Hindi or Chinese is not warranted as these fonts are not supported by Flash Lite Player 2.
  • There are cases where single quotes in certain in languages are not handled properly.

Download

The source code of the application can be downloaded here.

Screenshots

Ss1.jpgA (2).jpg

This page was last modified on 14 May 2013, at 02:56.
59 page views in the last 30 days.
×