日期:2014-05-16  浏览次数:20577 次

调用 Google 翻译的地址格式:

http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + 原始文本 + "&langpair=" + 原语言 + "%7C" + 目标语言


利用Google提供的插件来翻译

详细内容可以参考:http://code.google.com/p/google-api-translate-java/

http://code.google.com/p/google-api-translate-java/downloads/detail?name=google-api-translate-java-0.92.jar


返回的数据格式如下, 可以用 responseData.translatedText 简单获取:

{
  "responseData" : {"translatedText" : "返回的文本"}, 
  "responseDetails" : null, 
  "responseStatus" : 200
}


$ dropjava -user user/password@tns  google-api-translate-java-0.92.jar
$ loadjava -user user/password@tns -force -resolve -verbose google-api-translate-java-0.92.jar 

create or replace and compile java source named gtranslate  as

import java.net.URLEncoder;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class gtranslate  {
  private static final String CHARSET = "UTF-8";
  public static void main(String[] args) throws Exception {
    /* Enter the URL of your site here */  
    Translate.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");  
    /** 
     * 第一个参数:要翻译的内容 
     * 第二个参数:翻译内容原始的语言 
     * 第三个参数:翻译内容要得到结果的语言 
     */  
    String translatedText = Translate.execute("试验", Language.CHINESE, Language.ENGLISH);       
    System.out.println(translatedText);   
  }
  
  public static void run() throws Exception {
    // Set the HTTP referrer to your website address.
    Translate.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
    String translatedText = Translate.execute("试验", Language.CHINESE, Language.ENGLISH); 
    System.out.println(translatedText);
  }
  
  public static String UTF8Encode (String s) throws Exception {
    String utf8encode = URLEncoder.encode(s, CHARSET);
    return utf8encode;
  }
  
}

create or replace package pkg_translate is

  function utf8encode(p_text in varchar2) return varchar2;

end pkg_translate;
/
create or replace package body pkg_translate is

  function utf8encode(p_text in varchar2) return varchar2
  is language java name 'gtranslate.UTF8Encode(java.lang.String) return java.lang.String';
end pkg_translate;
/

create or replace procedure pro_gtranslate 
as language java
name 'gtranslate.run()';
/

begin
  -- Call the function
  :result := pkg_translate.utf8encode(p_text => :p_text);
end;
/

--待解决
SQL> execute pro_gtranslate;
 Exception in thread "Root Thread" java.lang.Exception: [google-api-translate-java] Error retrieving translation.
	at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:123)
	at com.google.api.translate.Translate.execute(Translate.java:69)
	at TestTranslate.db_run(TESTTRANSLATE:23)
Caused by: java.net.UnknownHostException: ajax.googleapis.com
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
	at java.net.Socket.connect(Socket.java:519)
	at java.net.Socket.connect(Socket.java:469)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:382)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:494)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:231)
	at sun.net.www.http.HttpClient.New(HttpClient.java:304)
	at sun.net.www.http.HttpClient.New(HttpClient.java:316)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:817)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:769)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:694)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:861)
	at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:107)
	... 2 more
 
begin pro_gtranslate; end;
 
ORA-29532: Java call terminated by uncaught Java exception: java.lang.Exception: [google-api-translate-java] Error retrieving translation.
ORA-06512: at "GTRANSLATE.PRO_GTRANSLATE", line 1
ORA-06512: at line 2