Google Translation - Java Task

Mirjana's picture
Java Code for Google Translation
 
Code does not have to be defined as a class or a method in a class. It is a pure java code, similar to content of a method. Imports must be included to resolve all the types used in the code.
 
********************************************
 
import com.sterlingcommerce.woodstock.workflow.Document;
 
import java.io.InputStream;
import java.io.InputStreamReader;
 
import java.io.BufferedReader;
 
import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
 
 
                   Document document = new Document();
         
                   Document srcDoc = wfc.getPrimaryDocument();
                   InputStream is = srcDoc.getInputStream();
                  
                   BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                               
                   String sourceLanguage = "ENGLISH";
                   String targetLanguage = "CROATIAN";
                  
                   String text = null;
                   String translatedText = null;       
                   StringBuffer contents = new StringBuffer();
 
                   GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
 
                   // repeat until all lines is read
                         
                   while ((text = br.readLine()) != null) {
                            
                            translatedText = Translate.execute(text, Language.valueOf(sourceLanguage), Language.valueOf(targetLanguage) );
                            contents.append(translatedText + "\n");
                                 
                   }
                       
    br.close();
                   
    document = wfc.newDocument();
                   document.setBody(contents.toString().getBytes());
                                               
                   wfc.setAdvancedStatus("Translated!");
                   wfc.setBasicStatus(000);
                            
                   wfc.putPrimaryDocument(document);
                   return "000";
 
********************************************
 
Java Task Service configuration
 
Java code is added as Inline, it can also be referenced to a file!
 
 
Business Process
 
 
Input File
 
Welcome to the World of Integration.
Wish you a nice day!
 
Result after BP execution:
 
 
Note: Special characters are not shown properly, but an input is translated from English to Croatian!!