<? header("Cache-Control: no-cache, must-revalidate") ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<?php
class Google_API_translator {
public $opts = array("text" => "", "language_pair" => "en|it");
public $out = "";
function __construct() {
//echo "Google Translator API\n(c) 2007 Involutive snc http://www.involutive.com\n";
//echo "Author: Paolo Ardoino <
paolo@involutive.com >";
}
function setOpts($opts) {
if($opts["text"] != "") $this->opts["text"] = $opts["text"];
if($opts["language_pair"] != "") $this->opts["language_pair"] = $opts["language_pair"];
}
function is_utf8($str) {
$c=0; $b=0;
$bits=0;
$len=strlen($str);
for($i=0; $i<$len; $i++){
$c=ord($str[$i]);
if($c > 128){
if(($c >= 254)) return false;
elseif($c >= 252) $bits=6;
elseif($c >= 248) $bits=5;
elseif($c >= 240) $bits=4;
elseif($c >= 224) $bits=3;
elseif($c >= 192) $bits=2;
else return false;
if(($i+$bits) > $len) return false;
while($bits > 1){
$i++;
$b=ord($str[$i]);
if($b < 128 || $b > 191) return false;
$bits--;
}
}
}
return true;
}
function translate() {
$this->out = "";
$google_translator_url = "
http://translate.google.com/translate_t ... ge_pair"])."&";
$google_translator_data .= "text=".urlencode($this->opts["text"]);
$gphtml = $this->postPage(array("url" => $google_translator_url, "data" => $google_translator_data));
$out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=\"ltr\">"));
$out = substr($out, 29);
$out = substr($out, 0, strpos($out, "</div>"));
//print_r($out);
echo '<br>';
$this->out = html_entity_decode(utf8_encode($out));
//if($this->is_utf8($this->out))
//{
//echo 'ok';
//}
//print_r( $this->out);
return $this->out;
}
// post form data to a given url using curl libs
function postPage($opts) {
$html = "";
if($opts["url"] != "" && $opts["data"] != "") {
$ch = curl_init($opts["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);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $opts["data"]);
$html = curl_exec($ch);
print_r($html);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
?>
<?php
echo $text = 'hotel new york|new york hotel|new york new york hotel';
$g = new Google_API_translator();
$g->setOpts(array("text" => stripslashes($text), "language_pair" => "en|hi"));
$g->translate();
//echo $g->out;
?>
</body>
</html>