博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Google翻译接口应用
阅读量:4200 次
发布时间:2019-05-26

本文共 3759 字,大约阅读时间需要 12 分钟。

Google翻译接口应用 

       最近做一翻译公司网站,要求加一在线翻译。之前他们要的是一个外链接,我在想要是有现成的翻译网站接口可以调用的话,这个链接也就没必要了。在网上查了下,google的在线翻译可以用,找到了相关的代码,我自己添加了一些,测试可以用。相关代码如下:

  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 ( $gphtmlstrpos ( $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.    ?>  

原始代码地址:

测试结果:

显示结果

 

 

 

 

 

 

 <!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>

 

你可能感兴趣的文章
pyqt实现界面化编程
查看>>
qt写DLL文件并调用和出现的问题分析
查看>>
工厂模式(Factory)-设计模式(一)
查看>>
建造者模式(Builder)-设计模式(三)
查看>>
初学Java必备基础知识,编程领域你需要掌握的关键点!
查看>>
阿里五年Java程序员的总结,献给还在迷茫中的你!
查看>>
程序员身上有异味,同事为什么都不会直接告诉他?
查看>>
Java、C、C+ +、PHP、Python分别用来开发什么?一篇文章告诉你!
查看>>
Linux-SHELL常用命令
查看>>
Linux-网络运维基础
查看>>
Verilog编程网站学习——门电路、组合电路、时序电路
查看>>
android——学生信息显示和添加
查看>>
Android——ImageSwitcher轮流显示动画
查看>>
Android——利用手机端的文件存储和SQLite实现一个拍照图片管理系统
查看>>
图像调优1:清晰度相关参数MTF,SFR,MTF50,MTF50P 以及TVL的概念以及换算说明
查看>>
罗永浩欲直播带货,京东说可以帮忙联系
查看>>
B站,正在变成下一个“公众号”?
查看>>
小米启动安心服务月 手机家电产品可免费清洁保养
查看>>
刘作虎:一加新品将全系支持 5G
查看>>
滴滴顺风车上线新功能,特殊时期便捷出行
查看>>