Skip to content

Instantly share code, notes, and snippets.

@binod
Created November 27, 2009 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binod/244094 to your computer and use it in GitHub Desktop.
Save binod/244094 to your computer and use it in GitHub Desktop.
package my.test;
import org.glassfish.cafe.api.*;
import org.glassfish.cafe.api.bean.*;
import org.glassfish.cafe.api.bean.CommunicationEvent.Type;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
@CommunicationBean
public class ExampleBean {
@Context
CommunicationContext<IMConversation, UserParticipant, TextMessage> ctx;
@CommunicationEvent(type=Type.MESSAGEARRIVED)
public void handleMessageArrival() {
Participant fromParty = ctx.getParticipant();
Participant toParty = ctx.getCommunication().getInitiator();
if (toParty.equals(fromParty)) {
toParty = ctx.getCommunication().getParticipant();
}
TextMessage msg = ctx.getMessage();
String fromString = ExampleServlet.getLanguage(fromParty.getName());
String toString = ExampleServlet.getLanguage(toParty.getName());
Translate.setHttpReferrer("localhost");
Language from = Language.fromString(fromString);
Language to = Language.fromString(toString);
try {
//Here the message is translated using Google Translate API.
String tranlatedMessage = Translate.execute(msg.getText(), from, to);
//The translated text is set on the message.
msg.setText(tranlatedMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment