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 = "".$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, "");
$body = curl_exec($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'));



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

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.

JersyWoo said...

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

frank said...

Love the widget, but i dont know PHP.

I want to completely translate my ASP pages server side.

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

How can i do that?

אראל סגל said...

Thank you very much! It was very helpful.


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

Ti-M said...


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

Is it possible ?


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!

bucabay said...

There is a PHP library on Google Code:

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.


function postproduction($nb){
$nb=translateText(array($nb, 'german'), 'de', 'en');
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.