溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何利用谷歌Translate API制作自己的翻譯腳本

發(fā)布時間:2021-09-30 15:26:52 來源:億速云 閱讀:153 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何利用谷歌Translate API制作自己的翻譯腳本”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何利用谷歌Translate API制作自己的翻譯腳本”吧!

PHP代碼:

復(fù)制代碼 代碼如下:

#!/usr/bin/php -q
<?php
/**
 * PHP Script For Google Translate
 * @author:Yishan Wang
 * @version:1.0.0
 */
class Google_API_translator
{
 public $url = "http://translate.google.com/translate_t";
 public $text = "";
 public $out = "";
 public $ip = '';
 function setText($text){
  $this->text = $text;
 }
 function translate($from='auto',$to='zh-CN'){
  $this->out = "";
  $gphtml = $this->postPage($this->url, $this->text,$from,$to);
  preg_match_all('/<span/s+title/="[^>]+>([^<]+)<//span>/i',$gphtml,$res);
  $this->out = $res[1][0];
  return $this->out;
 }
 /*
 $from  需要翻譯的語言
 $to    翻譯的語言
 */
 function postPage($url, $text,$from='auto',$to='zh-CN'){
  $html ='';
  if($url != "" && $text != "") {
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   if(!empty($this->ip) && is_string($this->ip)){
    curl_setopt($ch, CURLOPT_INTERFACE,$this->ip);
   }
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT, 15);
   /*
   *hl - 界面語言,此處無用。
   *langpair - src lang to dest lang
   *ie - urlencode的編碼方式?
   *text - 要翻譯的文本
   */
   $fields = array('hl=zh-CN', 'langpair='.$from.'|'.$to, 'ie=UTF-8','text='.$text);
   $fields = implode('&', $fields);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
   $html = curl_exec($ch);
   if(curl_errno($ch)) $html = "";
   curl_close ($ch);
  }
  return $html;
 }
}
 $from = !empty($_REQUEST['fromlan'])?$_REQUEST['fromlan']:'en';
 $to = !empty($_REQUEST['tolan'])?$_REQUEST['tolan']:'zh-CN';
 $keywords  = "";
 for($i=1;$i<$argc;$i++){
  $keywords .= $argv[$i]." "; 
 }
 $article = !empty($_REQUEST['article'])?$_REQUEST['article']:$keywords;
 $g = new Google_API_translator();
 if(isset($_REQUEST['ip']) && !empty($_REQUEST['ip']))
 {
 $g -> ip = $_REQUEST['ip'];
 }
 $article = iconv('GBK','UTF-8',$article);
 $article = str_replace('{enter}',"/r/n",$article);
 $g->setText($article);
 $g->translate($from,$to);
 echo "-----------翻譯結(jié)果--------------/n";
 echo iconv('GBK','UTF-8',$g->out);
 echo "/n";
?>

2、將以上內(nèi)容保存名為“gtranslate”的文件中。

3、給gtranslate添加執(zhí)行權(quán)限

    chmod a+x gtranslate

4、創(chuàng)建軟連接

    ln -s /yourpath/gtranslate /usr/bin/gtranslate

5、輸入測試詞匯:

    gtranslate Hello World


    -----------翻譯結(jié)果--------------
    世界您好

>>>

6、做了個中英文互譯的版本。

用 gtranslate China ,英譯漢

用 gtranslate -r 中國 ,漢譯英

>>>

到此,相信大家對“如何利用谷歌Translate API制作自己的翻譯腳本”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI