1. 程式人生 > >呼叫Google的API實現簡單的翻譯功能

呼叫Google的API實現簡單的翻譯功能

本例項只是簡單的實現了漢譯英的功能,Google為我們提供了多種語言的互譯功能,新建類之前須下載GoogleAPI的jar包:google-api-transtate-java-0.92.jar。簡單實現程式碼如下:package google;import com.google.api.translate.Language;import com.google.api.translate.Translate;public class google {/** * @param args */@SuppressWarnings("deprecation")public static void main(String[] args) {// TODO Auto-generated method stubString trans=null;Translate.setHttpReferrer("http://translate.google.cn");try { trans = Translate.translate("你好", Language.CHINESE,Language.ENGLISH); //漢譯英//trans = Translate.translate("hello", Language.ENGLISH, Language.CHINESE);  System.out.println(trans);//             英譯漢} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}