Sunday, October 12, 2008

Google Translation API: Translate on server side using PHP

Hi Guys,

As you know, there is google translate ajax api which can be used on client side translation using it's javascript interface. But how do we do translation on server side languages such as PHP, Java, ASP.NET or Rails. There is a task for us to translate multiple records on the database into 6 different languages as cron job. Similar problems may face you. So here is the solutions that you can use for your servers-side google translations.


function translateTexts($src_texts = array(), $src_lang, $dest_lang){
//setting language pair
$lang_pair = $src_lang.'|'.$dest_lang;

$src_texts_query = "";
foreach ($src_texts as $src_text){
$src_texts_query .= "&q=".urlencode($src_text);
}

$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0".$src_texts_query."&langpair=".urlencode($lang_pair);

// sendRequest
// note how referer is set manually

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.YOURWEBSITE.com");
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body, true);

if ($json['responseStatus'] != 200){
return false;
}


$results = $json['responseData'];

$return_array = array();

foreach ($results as $result){
if ($result['responseStatus'] == 200){
$return_array[] = $result['responseData']['translatedText'];
} else {
$return_array[] = false;
}
}

//return translated text
return $return_array;
}


// translate multiple texts
print_r(translateText(array('hello world', 'italian'), 'en', 'it'));

?>

18 comments:

David Monaghan said...

Hey thanks! I stumbled upon this while looking for the Google Translate API and it saved me some time; I created a little program to help me make some background images: If you're interested you can read about it at my blog (source included).

I did edit your source a bit just to make it work with single words/phrases. Other than that it was perfect for my needs. One last thing, I noticed the source you posted is missing the initial <? so you may want to edit that in there.

The program itself is hosted at http://nnectt.com/writeWorldWords.php.

Anonymous said...

I have developed Online Translation Service page which uses Google AJAX Language API. I also used Google AJAX Language API to translate the user interface of Online Translation Service to the following languages: English, Arabic, Bulgarian, Chinese, Croatian, Czech, Danish, Dutch, Finnish, French, German, Greek, Hindi, Italian, Japanese, Korean, Norwegian, Polish, Portuguese, Romanian, Russian, Spanish, Swedish.

Anonymous said...

Thanks lots. It needed some tweeking to suit my needs but it works great.

Anonymous said...

Thank you, this script has no problem with the Czech wedges.

Anonymous said...

Hi
Love the widget, but i dont know PHP.

I want to completely translate my ASP pages server side.

So when someone visits mysite.com they get the english version, when the visit mysite.ru they get the russian version.

How can i do that?

Frankbasti@Hotmail.com

אראל סגל said...

Thank you very much! It was very helpful.

Erel

Anonymous said...

API uses GET which has size limitations. If I put twenty sentences in array that is pretty long for get, any way to do it with POST

beurette said...

You can cut your text in 500 chara strings and make a loop with this script (I do this).

fdslkjf said...

Hi,

How can you use the Google Translation API using the method POST ?

Is it possible ?

Thanks

ali said...

tnx,very god

ali said...

tnx man .

Anonymous said...

Is there any limitation from Google in terms of number of queries ?

Nighthawk said...

This function saved me A LOT of searching, reading and programming.
The most important thing about it is that since it's a server side function all my translations are indexed by search engines while those made with Javascript funcs in Google API examples aren't.
For enyone using PHP - don't look any further - this little baby does it !
Thank you! Keep up the good work!

Unknown said...

There is a PHP library on Google Code:
http://code.google.com/p/php-language-api/

Anonymous said...

How to get started in learning about forex ?
Forex tutorials and news

Anonymous said...

When I submit my whole Homepage via output buffer i get an empty page back.

ob_start('postproduction')

function postproduction($nb){
$nb=translateText(array($nb, 'german'), 'de', 'en');
$nb=$nb[0];
return ($nb);
}


can anyone help me?

gallery 802 said...

Yeah..i go through this blog and it is a nice work showing to get more information and knowledge.

Kitchen Sinks said...

Yeah...that is proper information posted...please update it with some more information regarding this.