PHP代码:
复制代码 代码如下:
#!/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 "-----------翻译结果--------------/n";
echo iconv('GBK','UTF-8',$g->out);
echo "/n";
?>
2、将以上内容保存名为“gtranslate”的文件中。
3、给gtranslate添加执行权限
chmod a+x gtranslate
4、创建软连接
ln -s /yourpath/gtranslate /usr/bin/gtranslate
5、输入测试词汇:
gtranslate Hello World
-----------翻译结果--------------
世界您好
>
6、做了个中英文互译的版本。
用 gtranslate China ,英译汉
用 gtranslate -r 中国 ,汉译英
>
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】