Google翻譯介面應用

來源:互聯網
上載者:User
Google翻譯介面應用 

       最近做一翻譯公司網站,要求加一線上翻譯。之前他們要的是一個外連結,我在想要是有現成的翻譯網站介面可以調用的話,這個連結也就沒必要了。在網上查了下,google的線上翻譯可以用,找到了相關的代碼,我自己添加了一些,測試可以用。相關代碼如下:

view plain
copy to clipboard
print
?
  1. <?php     
  2.     /* Google翻譯PHP介面  
     
  3.    / * 注意:如果翻譯文本為UTF-8編碼,則要刪去mb_convert_encoding函數  
     
  4.      */
        
  5.           
  6.     class
     Google_API_translator {      
  7.         public
     
    $url
     = 
    "http://translate.google.com/translate_t"
    ;     
  8.         public
     
    $text
     = 
    ""
    ;
    //翻譯文本   
      
  9.          public
     
    $out
     = 
    ""

    //翻譯輸出 
      
  10.          public
     
    $lang_src
     = 
    ""
    ;
    //要翻譯的語言
      
  11.     public
     
    $lang_des
     = 
    ""
    ;
    //翻譯成的語言
      
  12.              
  13.         function
     setText(
    $text
    ){     
  14.             $this
    ->text = 
    $text
    ;     
  15.         }     
  16.           
  17.         function
     setLang(
    $lang_src
    ,
    $lang_des
    ){  
  18.             $this
    ->lang_src = 
    $lang_src
    ;  
  19.             $this
    ->lang_des = 
    $lang_des
    ;  
  20.         }  
  21.              
  22.         function
     translate() {      
  23.             $this
    ->out = 
    ""
    ;      
  24.                  
  25.             $gphtml
     = 
    $this
    ->postPage(
    $this
    ->url, 
    $this
    ->text,
    $this
    ->lang_src,
    $this
    ->lang_des);      
  26.             $out
     = 
    substr
    (
    $gphtml

    strpos
    (
    $gphtml

    "<div id=result_box dir=/"ltr/">"
    ));     
  27.             $out
     = 
    substr
    (
    $out
    , 29);     
  28.             $out
     = 
    substr
    (
    $out
    , 0, 
    strpos
    (
    $out

    "</div>"
    ));     
  29.                  
  30.             $this
    ->out = 
    $out
    ;     
  31.             return
     
    $this
    ->out;      
  32.         }      
  33.              
  34.         function
     postPage(
    $url

    $text
    ,
    $lang_src
    ,
    $lang_des
    ) {      
  35.             $html
     =
    ''
    ;      
  36.                  
  37.             if
    (
    $url
     != 
    ""
     && 
    $text
     != 
    ""
    &&
    $lang_src
     != 
    ""
    &&
    $lang_des
     != 
    ""
    ) {      
  38.                 $ch
     = curl_init(
    $url
    );      
  39.                 curl_setopt($ch
    , CURLOPT_RETURNTRANSFER, 1);      
  40.                 curl_setopt($ch
    , CURLOPT_HEADER, 1);      
  41.                 curl_setopt($ch
    , CURLOPT_FOLLOWLOCATION, 1);      
  42.                 curl_setopt($ch
    , CURLOPT_TIMEOUT, 15);      
  43.                      
  44.                 $fields
     = 
    array
    (
    'hl=zh-CN'

    'langpair='
    .
    $lang_src
    .
    '|'
    .
    $lang_des

    'ie=UTF-8'
    ,
    'text='
    .urlencode(mb_convert_encoding(
    $text

    'UTF-8'

    'GB2312'
    )));     
  45.                 curl_setopt($ch
    , CURLOPT_POST, 1);     
  46.                 curl_setopt($ch
    , CURLOPT_POSTFIELDS, implode(
    '&'

    $fields
    ));                                                          
  47.                      
  48.                 $html
     = curl_exec(
    $ch
    );      
  49.                 if
    (curl_errno(
    $ch
    )) 
    $html
     = 
    ""
    ;      
  50.                 curl_close ($ch
    );      
  51.             }      
  52.             return
     
    $html
    ;      
  53.         }      
  54.     }      
  55.          
  56.      
  57.    ?>  

<?php<br />/* Google翻譯PHP介面 / * 注意:如果翻譯文本為UTF-8編碼,則要刪去mb_convert_encoding函數 */<br />class Google_API_translator { public $url =<br />"http://translate.google.com/translate_t"; public $text = "";//翻譯文本<br />public $out = ""; //翻譯輸出 public $lang_src = "";//要翻譯的語言 public<br />$lang_des = "";//翻譯成的語言 function setText($text){ $this->text =<br />$text; } function setLang($lang_src,$lang_des){ $this->lang_src =<br />$lang_src; $this->lang_des = $lang_des; } function translate() {<br />$this->out = ""; $gphtml = $this->postPage($this->url,<br />$this->text,$this->lang_src,$this->lang_des); $out =<br />substr($gphtml, strpos($gphtml, "<div id=result_box<br />dir=/"ltr/">")); $out = substr($out, 29); $out = substr($out, 0,<br />strpos($out, "</div>")); $this->out = $out; return<br />$this->out; } function postPage($url, $text,$lang_src,$lang_des) {<br />$html =''; if($url != "" && $text != ""&&$lang_src !=<br />""&&$lang_des != "") { $ch = curl_init($url); curl_setopt($ch,<br />CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1);<br />curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch,<br />CURLOPT_TIMEOUT, 15); $fields = array('hl=zh-CN',<br />'langpair='.$lang_src.'|'.$lang_des,<br />'ie=UTF-8','text='.urlencode(mb_convert_encoding($text, 'UTF-8',<br />'GB2312'))); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch,<br />CURLOPT_POSTFIELDS, implode('&', $fields)); $html = curl_exec($ch);<br />if(curl_errno($ch)) $html = ""; curl_close ($ch); } return $html; } }<br />?>

原始代碼地址:http://blog.csdn.net/aprin

測試結果:

顯示結果

 

 

 

 

 

 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
      <title>Google AJAX Language API - Basic Translation</title> 
    </head> 
    <body>

     <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
       <script type="text/javascript"> 
   
      google.load("language", "1"); 
 
     function initialize() { 
       google.language.translate("dddd", "zh-CN", "en", function(result) { 
        if (!result.error) { 
          var container = document.getElementById("translation"); 
          container.innerHTML = result.translation; 
         } 
       }); 
      } 
     google.setOnLoadCallback(initialize); 
  
    </script> 
 
     <div id="translation"></div> 
  </body> 
 </html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.