View Full Version : google translate (no payed api)

10-13-2012, 05:28 PM
i was going trough googlt translator's client side code and came up with this:

function google_translate($from, $to, $text){
//fake user-agent
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv: Gecko/2008092417 Firefox/3.0.3');
ini_set('default_charset', 'utf-8');//set encoding in case it's other than utf-8 (you might need to re-set it afterwords)
$get_string = 'hl=' . $from . '&tl=' . $to . '&q=' . urlencode($text);
$data = file_get_contents('http://translate.google.com/?' . $get_string);
$DOM = new DOMDocument;
$items = $DOM->getElementById('result_box');
return $items->nodeValue;

it's very simple function. instead of using regular API, it takes html document served by google translator, extracts translated words and returns them;)

google_translate($source_language, $target_language, $text);

for example: if you want to translate 'some text' from english to italian, and print result, it would be

echo google_translate('en', 'it', 'some text');
i've been looking for decent free translator with api for very long time with no luck, so i've decided to spare you guys some time ;)