Quick, simple, effective. You need the Snoopy class to make it work. Enjoy

include("Snoopy.class.php");
function translate($content,$inlang,$outlang){
$snoopy = new Snoopy;
//http://google.com/translate_t?hl=en&ie=UTF8&text=eat+pie&langpair=en%7Cit
$trans_url = "http://google.com/translate_t?hl=en&ie=UTF8&text=".urlencode($content)."&langpair=".$inlang."|".$outlang;
$page = $snoopy->fetch($trans_url);
$page = $snoopy->results;
//parse out the data we need
preg_match("/<div\s+id=result_box\s+dir=ltr>(.*?)<\/div>/i",$page,$match);
return $match[1];
}
Try this PHP class:
http://www.phpclasses.org/browse/package/3898.htmlOr maybe this code in PHP5 till Google API allows do that....
<?php
class Google_API_translator {
public $opts = array("text" => "", "language_pair" => "en|it");
public $out = "";
function __construct() {}
function setOpts($opts) {
if($opts["text"] != "") $this->opts["text"] = $opts["text"];
if($opts["language_pair"] != "") $this->opts["language_pair"] = $opts["language_pair"];
}
function translate() {
$this->out = "";
$google_translator_url = "
http://google.com/translate_t?langpair=";
$google_translator_url .= urlencode($this->opts["language_pair"])."&";
$google_translator_url .= "text=".urlencode($this->opts["text"]);
$gphtml = $this->getPage(array("url" => $google_translator_url));
preg_match('/(.*?)</div>/', $gphtml, $out);
$this->out = utf8_encode($out[0]);
return $this->out;
}
function getPage($opts) {
$html = "";
if($opts["url"] != "") {
$ch = curl_init($opts["url"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$html = curl_exec($ch);
if(curl_errno($ch)) {
$html = "";
}
curl_close ($ch);
}
return $html;
}
}
?>
Implementation Example:
<?php
$g = new Google_API_translator();
$g->setOpts(array("text" => "ciao", "language_pair" => "it|en"));
$g->translate();
echo $g->out;
?>
A good example it could be some others "and more effectives" translators, like Babefish from Y! and TranEx, Russian in translator.ru...
^_^ It could be interesting if someone have done the code.
Slime