Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts

    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
    Last edited by patryk; 10-13-2012 at 04:32 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •