Using MS Traslator API,Google Translator API and Google Calender API

Hi Guys, few weeks back one of my friends asked me on how to access the Google Calender api using Java. First i was stuck at many levels and came across excellent Google API which is a java wrapper guys below is the code snippet which is almost self explanatory. Follow the code comments.

/**
*
*/
package com.spark.google.calender;

import java.net.URL;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.calendar.ColorProperty;
import com.google.gdata.data.calendar.SelectedProperty;

/**
* @author Mantha Pavan Kumar
*
*/
public class AccessGoogleCalender {

/**
* @param args
*/
public static void main(String[] args) {
try {

// Add a title to calender
CalendarService myService = new CalendarService(
“exampleCo-exampleApp-1.0”);
URL feedUrl = new URL(
http://www.google.com/calendar/feeds/default/allcalendars/full”);
myService.setUserCredentials(“XXX@gmail.com”,
“your pwd”);

CalendarFeed resultFeed = myService.getFeed(feedUrl,
CalendarFeed.class);
CalendarEntry calendar = resultFeed.getEntries().get(0);
calendar.setTitle(new PlainTextConstruct(“New title”));
calendar.setColor(new ColorProperty(“#A32929”));
calendar.setSelected(SelectedProperty.TRUE);
CalendarEntry returnedCalendar = calendar.update();

// retrieve the response from the feed url

resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println(“Your calendars:”);
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEntry entry = resultFeed.getEntries().get(i);
System.out.println(“\t” + entry.getTitle().getPlainText());
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

After this i was self researching on all google APIs such as YouTube API,Google Translator API and lot more. but the translator api’s are paid one they only allow few Translation transactions as free(for test purpose). For Translator api’s to work we need to create a Google API Key, and below is the code to use Google Translator API. Follow the code comments carefully

/**
*
*/
package com.spark.google.transulator;

import com.google.api.GoogleAPI;
import com.google.api.GoogleAPIException;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

/**
* @author Mantha Pavan Kumar
*
*/
public class GoogleTransulator {

/**
* @param args
* @throws GoogleAPIException
*/
public static void main(String[] args) throws GoogleAPIException {
// Set the HTTP referrer to your website address.
GoogleAPI.setHttpReferrer(“/* your site address */”);

// Set the Google Translate API key
// See: http://code.google.com/apis/language/translate/v2/getting_started.html
GoogleAPI.setKey(“/* your secret google api key */”);

String translatedText = Translate.DEFAULT.execute(“Bonjour le monde”, Language.FRENCH, Language.ENGLISH);

System.out.println(translatedText);

}

}

Since Google Provides very less Translation Transactions i have searched for more Api’s which do the job for me and i came across Microsoft Translation Java wrapper Api which is paid but Translation Transactions are more so you can test for quite more than Google Api and the code is as below. For working with Microsoft Api we need to create ClientId and ClientSecret at MicrosoftsoftAzure Market Place. Follow the Code Comments carefully.

/**
*
*/
package com.spark.ms.transulater;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

/**
* @author Sony
*
*/
public class MSTransulator {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Set your Windows Azure Marketplace client info – See
// http://msdn.microsoft.com/en-us/library/hh454950.aspx
Translate.setClientId(“your Client ID”);
Translate
.setClientSecret(“Your Client Secret”);

String translatedText = null;
try {
translatedText = Translate.execute(“Bonjour le monde”,
Language.FRENCH, Language.ITALIAN);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(translatedText);

// Create your array of texts to be translated
// NOTE: The source language of all texts must be the same. For
// instance, I cannot translate
// a Spanish string, French string, and English string into German
// during a single call
String[] sourceTexts = { “This is a sentence, translate me.”,
“I would like to be translated”, “How are you doing today?” };

// Call the translate.execute method, passing an array of source texts
String[] translatedTexts = null;
try {
translatedTexts = Translate.execute(sourceTexts, Language.ENGLISH,
Language.FRENCH);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Print the results!
for (String text : translatedTexts) {
System.out.println(text);
}
}
}

Advertisements

One thought on “Using MS Traslator API,Google Translator API and Google Calender API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s