Google翻譯介面應用
最近做一翻譯公司網站,要求加一線上翻譯。之前他們要的是一個外連結,我在想要是有現成的翻譯網站介面可以調用的話,這個連結也就沒必要了。在網上查了下,google的線上翻譯可以用,找到了相關的代碼,我自己添加了一些,測試可以用。相關代碼如下:
view plain
copy to clipboard
print
?
- <?php
- /* Google翻譯PHP介面
- / * 注意:如果翻譯文本為UTF-8編碼,則要刪去mb_convert_encoding函數
- */
-
- class
Google_API_translator {
- public
$url
=
"http://translate.google.com/translate_t"
;
- public
$text
=
""
;
//翻譯文本
- public
$out
=
""
;
//翻譯輸出
- public
$lang_src
=
""
;
//要翻譯的語言
- public
$lang_des
=
""
;
//翻譯成的語言
-
- function
setText(
$text
){
- $this
->text =
$text
;
- }
-
- function
setLang(
$lang_src
,
$lang_des
){
- $this
->lang_src =
$lang_src
;
- $this
->lang_des =
$lang_des
;
- }
-
- function
translate() {
- $this
->out =
""
;
-
- $gphtml
=
$this
->postPage(
$this
->url,
$this
->text,
$this
->lang_src,
$this
->lang_des);
- $out
=
substr
(
$gphtml
,
strpos
(
$gphtml
,
"<div id=result_box dir=/"ltr/">"
));
- $out
=
substr
(
$out
, 29);
- $out
=
substr
(
$out
, 0,
strpos
(
$out
,
"</div>"
));
-
- $this
->out =
$out
;
- return
$this
->out;
- }
-
- function
postPage(
$url
,
$text
,
$lang_src
,
$lang_des
) {
- $html
=
''
;
-
- if
(
$url
!=
""
&&
$text
!=
""
&&
$lang_src
!=
""
&&
$lang_des
!=
""
) {
- $ch
= curl_init(
$url
);
- curl_setopt($ch
, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch
, CURLOPT_HEADER, 1);
- curl_setopt($ch
, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch
, CURLOPT_TIMEOUT, 15);
-
- $fields
=
array
(
'hl=zh-CN'
,
'langpair='
.
$lang_src
.
'|'
.
$lang_des
,
'ie=UTF-8'
,
'text='
.urlencode(mb_convert_encoding(
$text
,
'UTF-8'
,
'GB2312'
)));
- curl_setopt($ch
, CURLOPT_POST, 1);
- curl_setopt($ch
, CURLOPT_POSTFIELDS, implode(
'&'
,
$fields
));
-
- $html
= curl_exec(
$ch
);
- if
(curl_errno(
$ch
))
$html
=
""
;
- curl_close ($ch
);
- }
- return
$html
;
- }
- }
-
-
- ?>
<?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>