ホームページ  >  記事  >  バックエンド開発  >  Google 翻訳 API を使用して独自の翻訳スクリプトを作成する_PHP チュートリアル

Google 翻訳 API を使用して独自の翻訳スクリプトを作成する_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:28:581190ブラウズ

PHP代:

复制代価代価如下:

#!/usr/bin/php -q
/**
* Google 翻訳用 PHP スクリプト
* @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 translation($from='auto',$to='zh-CN'){
$this->out = "";
$gphtml = $this->postPage( $this->url, $this->text,$from,$to);
preg_match_all('/]+>([^<] +)/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-&gt; ip)){
curl_setopt($ ch、curlopt_interface、$ this-&gt; ip); , 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
/*
*hl - 界面语言、此処無用。
*langpair - src lang から 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 $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、给g翻訳追加执行权制限

chmod a+x gtranslate

4、创建软接

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

5、输入测试词汇:

Hello World を翻訳します

-----------翻訳译结果--------------

世界您好

>>>>

6、中訳相互評価版を作成しました。

用 gtranslate 中国 ,英译汉

用 gtranslate -r 中国 ,汉译英

>>>

http://www.bkjia.com/PHPjc/779569.html

www.bkjia.com

tru​​ehttp://www.bkjia.com/PHPjc/779569.html技術記事 PHP代: 复制代码 代码如下: #!/usr/bin/php -q ?php /*** Google 翻訳用 PHP スクリプト * @author:Yishan Wang * @version:1.0.0*/ class Google_API_translator { public...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。