CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   google translate (no payed api) (http://www.codingforums.com/showthread.php?t=276812)

patryk 10-13-2012 04:28 PM

google translate (no payed api)
 
i was going trough googlt translator's client side code and came up with this:
PHP Code:

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:1.9.0.3) 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;
    
$DOM->loadHTML($data);
    
$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;)
usage:
Code:

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
PHP Code:

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 ;)


All times are GMT +1. The time now is 07:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.