Wednesday, May 11, 2011

Google Translate API

Google Translate








In this post, we are going to learn about Google translate API and  its usages.



A Brief







Google Translate is a free translation service that provides instant translations between 57 different languages. It can translate words, sentences and web pages between any combination of  languages supported by Google. The Google translate java API uses these service to translate input text into different languages.




Sample Code







This java snippet is used to convert a text to English language.


package com.sarf.translator;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class TranslateText {
    public static void main(String[] args)
    {
        String translatedText = null;
        String strText ="Ik ben een domme.";
        Translate.setHttpReferrer("www.myweb.com");
        try {
            translatedText = 

            Translate.execute(strText,Language.AUTO_DETECT, Language.ENGLISH);
            System.out.println("Translated Text ");
            System.out.println(translatedText);
          } catch (Exception e) {
            System.out.println(e.getMessage());
           }
    }
}


Language Detector








We can also use google translate API to detect  language of the input text.

package com.sarf.translator;
import com.google.api.detect.Detect;
import com.google.api.detect.DetectResult;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class LanguageDetector {
    public static void main(String[] args) {
        String translatedText = null;
        String strText ="Ik ben een domme.";
        Translate.setHttpReferrer("www.myweb.com");
        DetectResult dr = null;
        Language lg     = null;
        try {
            dr = Detect.execute(strText);
            lg = dr.getLanguage();
            System.out.println(lg.name());
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
}










Google Translate API (jar) : Click Here
Google Translate API (Source Code)  : Click here
Documentation : Click here



Bad News

Google announced Thursday it will shut down its Translate API entirely later this year.
Click Here