Usage of Google Translator Api For Free !!!!!!!

Hello Friends,

I found a hack to use Google Translator services.

As I was working on my scrapper project, category titles were  needed to translated from Dutch to English and we do not wanted to use any paid service.

I have tracked the requests made From   translate.google.com  and gathered them, then fetched responses from my curl function which I have Posted before .

below is the final code. It is working perfectly. Please let the page loaded for proper source code view.

<?php

function curl($url,$params = array(),$is_coockie_set = false)
{

if(!$is_coockie_set){
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}

$str = ''; $str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);

/* STEP 3. visit cookiepage.php */

$Url = $url.$str;

$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec ($ch);
return $output;
}

function Translate($word,$conversion = 'hi_to_en')
{
$word = urlencode($word);
// dutch to english
if($conversion == 'nl_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';

// english to hindi
if($conversion == 'en_to_hi')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1';

// hindi to english
if($conversion == 'hi_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=hi&tl=en&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1';

//$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';

$name_en = curl($url);

$name_en = explode('"',$name_en);
return  $name_en[1];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo "

Hindi To English
";
echo  Translate('कानूनी नोटिस: यह गूगल के अनुवादक सेवाओं की एक दुरुपयोग है, आप इस के लिए भुगतान करना होगा.');
echo "

English To Hindi
";
echo  Translate('legal notice: This is an abuse of google translator services ,  you must pay for this.','en_to_hi');
echo "

Dutch To English
";
echo  Translate('Disclaimer: Dit is een misbruik van Google Translator diensten, moet u betalen.','nl_to_en');

echo "

Just Kidding ....... :)";
?>
</body>
</html>

Copy this code in php file and hit it.
You will need to enable curl for this code to work.

Currently It supports Three translations

1)Hindi To English       2)English To Hindi       3) Dutch To English

If you want to add more translations Follow the steps below

1) Go to http://translate.google.com/ by Mozilla Firefox

2) open firebug and type any thing them copy the URL of Ajax request made by the browser.

3) Place it in the translate function with proper condition and replace &text parameter in the url with $word variable.
Let me know if it is helpful or give your valuable suggestions.

And Please don’t tell this to Google Ha ha …….

 

AS per rob’s suggestion I am placing the  updated translate function to support all kind of languages supported by Google. I have not tested it but hope it works fine

function Translate($word,$conversion = 'hi_to_en')
{
$word = urlencode($word);
$arr_langs = explode(‘_to_’, $conversion);
 $url = “http://translate.google.com/translate_a/t?client=t&text=$word&hl=".$arr_langs[1]."&sl=".$arr_langs[0]."&tl=".$arr_langs[1]."&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1″;

$name_en = curl($url);

$name_en = explode('"',$name_en);
return  $name_en[1];
}

Thanks And Regards,
Rupesh Patel

170 comments on “Usage of Google Translator Api For Free !!!!!!!

  1. I have also Implement this Code but It Return a .txt file with so many string. Right Now I am Just Wirte a Text “Hello” and I want Hindi Version of Hello at Output only. Not any Other Meaning.

    $word = “Hello”;

    $res = Translate($word, $conversion = ‘en_to_hi’);

    echo $res;

  2. It shows only content-type at output and nothing else

  3. Is it Poosible through Query String Like as:-

    http://www.mtechthesis.com/translator2.php?strg=hello

    Here is my URL and I want the Hindi Meaning of This Query String.

    It Shows me the Unwanted Characters.

    Tell me what we have to do to Resolve this.

  4. Sini Tuwirin says:

    Is it still applicable these days?

  5. Adil says:

    How can I call this in Java? Can it be done using PHP-Java Bridge??

  6. chintan singh says:

    its not working for me giving output is: Hindi To English -//W3C//DTD HTML 4.01 Transitional//EN English To Hindi -//W3C//DTD HTML 4.01 Transitional//EN Dutch To English -//W3C//DTD HTML 4.01 Transitional//EN Just Kidding ……. 🙂

  7. gird says:

    its not working for me giving output is: ja To en -//W3C//DTD HTML 4.01 Transitional//EN ja To id -//W3C//DTD HTML 4.01 Transitional//EN ja To id -//W3C//DTD HTML 4.01 Transitional//EN..
    where i m wrong please let me know

  8. Hi, I am not familiar with API. What I need is to use google translator API in SDL Trados, but I have no idea what kind of file or address I should use to use the translator built in for free… Do you guys have any idea what I need to do? Thanks!!!

  9. […] rastrear por Internet dimos con una pista que nos puso en alerta sobre cómo conseguirlo. Gracias a Rupesh Patel se nos ocurrió este pequeño y sencillo código PHP que consiguió nuestro objetivo y que hoy […]

Leave a reply to rupeshpatel Cancel reply