Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2012, 04:28 PM   PM User | #1
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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..
patryk is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:39 AM.


Advertisement
Log in to turn off these ads.