PHP Auto Translate Text via Google Translate

15 May 2007 | Content Generation, Tools

The Kaptain wrote this little script to allow him to translate some pages on the fly and thought you might enjoy it as well. You will need a copy of the Snoopy class in order for this to work.

If you make any additions or changes to the code let the Kaptain know and he will post your new version and tell the world how great you are.

1
2
3
4
5
6
7
8
9
10
11
12
13
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];
}

Happy Translating!


Leave a Reply

You must be logged in to post a comment.