|
/**
|
|
*
|
|
* Description : A simple snippet to demonstrate how to use Google AJAX API in Vala programming language
|
|
* Vala version : 0.7.8
|
|
* Developed by : Emad Al-Bloushi
|
|
* Date : Thu 12 Nov, 2009
|
|
* Compile with : valac --thread --pkg libsoup-2.4 --pkg json-glib-1.0 GoogleTranslator.vala
|
|
* Refer to : http://code.google.com/apis/ajaxlanguage/documentation/#fonje
|
|
*
|
|
**/
|
|
|
|
using Soup;
|
|
using Json;
|
|
|
|
class GoogleTranslator {
|
|
|
|
public static string execute ( string text , string il , string ol) throws GLib.Error {
|
|
|
|
string uri = "http://ajax.googleapis.com/ajax/services/language/translate?";
|
|
string version = "1.0";
|
|
string input_language = il;
|
|
string output_language = ol;
|
|
string full_uri = uri + "v=" + version + "&q=" + URI.encode (text, null) + "&langpair=" + input_language + "|" + output_language;
|
|
|
|
stdout.printf("full uri: %s\n", full_uri);
|
|
var parser = new Parser ();
|
|
var session = new SessionAsync ();
|
|
var message = new Message ("GET", full_uri);
|
|
|
|
session.send_message (message);
|
|
parser.load_from_data ( (string) message.response_body.data, (int) message.response_body.length);
|
|
|
|
unowned Json.Node root = parser.get_root ();
|
|
unowned Json.Node translated_text = root.get_object ().get_member ("responseData").get_object ().get_member("translatedText");
|
|
// Here you can get the value of responseDetails
|
|
//unowned Json.Node response_details = root.get_object ().get_member ("responseDetails");
|
|
// Here you can get the value of responseStatus
|
|
//unowned Json.Node response_status = root.get_object ().get_member ("responseStatus");
|
|
return translated_text.get_string ();
|
|
}
|
|
|
|
public static void main (string[] args) {
|
|
|
|
try {
|
|
string original_text = "Hello World";
|
|
string translated_text = GoogleTranslator.execute (original_text,"en","it");
|
|
stdout.printf ("Translated Text : %s\n" , translated_text);
|
|
|
|
} catch (Error e) {
|
|
stdout.printf ("I think there is something wrong !");
|
|
}
|
|
|
|
}
|
|
|
|
}
|