Created
November 27, 2009 16:19
-
-
Save binod/244094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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