PHP Code:
Copy CodeThe code is as follows:
#!/usr/bin/php-q
/**
* 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 ('/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-Interface language, useless here.
*langpair-src Lang to dest Lang
How is the *ie-urlencode encoded?
*text-Text to translate
*/
$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 "-----------translation results--------------/n";
echo iconv (' GBK ', ' UTF-8 ', $g->out);
echo "/n";
?>
2. Save the above content in a file named "Gtranslate".
3. Add Execute permission to Gtranslate
chmod a+x Gtranslate
4. Create a soft connection
Ln-s/yourpath/gtranslate/usr/bin/gtranslate
5. Input Test Vocabulary:
Gtranslate Hello World
-----------Translation Results--------------
Hello World
>>>
6, has done the translation of the English version.
With Gtranslate China, English-Chinese translation
With Gtranslate-r China, Chinese to English
>>>
http://www.bkjia.com/PHPjc/779569.html www.bkjia.com true http://www.bkjia.com/PHPjc/779569.html techarticle PHP Code: Copy code code as follows: #!/usr/bin/php-q? PHP/** * php Script for Google Translate * @author: Yishan Wang * @version: 1.0.0 */class Google_api_translator {public ...