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

About these ads

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

  1. Steve Teale says:

    I was in character encoding hell with this for a while until I spotted an alternate URL in the comments. My browser is set to default UTF-8. Works better with …&ie=UTF-8&oe=UTF-8&multires…

    You may see what I mean if you use one of the muli-sentence variants using source:

    Your dog ate my cat. How come? What the hell!

    Try translating from en to es.

  2. w2009 says:

    Hi mate,

    I have a “fatal error: Call to undefined function curl_init() on line 11″… Do you have any idea why it doesn’t work for me…?

    Thx…;-)

    • rupeshpatel says:

      curl is disable in your server … google “enable curl in php”

      • w2009 says:

        Sorry, I’m stupid… I should UTFG next time;-) Works fine now…The only problem seems to be with a character limit (already mentioned above by Guilherme Cunha etc.)… Have you dealt with it…?

  3. john slater says:

    heey i am trying this but i cant get the url using firebug of other languages

  4. chimele says:

    Big thanks for this! any thoughts on the below error? It has already been posted but not answered… cheers!

    I am getting this when i translate more than 1 word.
    “initial-scale=1, minimum-scale=1, width=device-width”

  5. aryan says:

    Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /home/content/99/10382899/html/test.php on line 73

  6. awesome!!!
    thanks a lot, you saved me :)
    i hope this awesome trick will not turn of by “G” :mrgreen:

  7. Sven says:

    Thank you very much for this posting :) Do you know whether Google has some mechanism detecting that you make use of their service? In earlier times I remember they had something like that got the search API.

  8. Krzysiek says:

    echo ” Just Kidding ……. “;
    Not a big bug if you know basic php coding… huh

    Thank you for the script!

  9. Krzysiek says:

    Multiple sentences give problem (there is no char limit…). Google answer is divided somehow into some kind of array
    like sentence_1_en, sentence_1_de, senstence_2_en, sentence_2_de.
    So mix of sentences – source language sentence next to its translated version and so on.
    Your code is to simple to cope with that. What can we do?
    Google output is json? I have trouble to automate that.

    Like cats. Hate dogs. Eat Cows.

    Translate that at http://translate.google.com and turn on firebug – network tab. Then search for output tab and see what it outputs (json can be seen to). This is a problem to “glue” all translated sentences into one pice. And still this is a workaround of problem, google can change website each minute and code will stop working. API needs to be paid, 20$ for one automated translation a week is to much for me.

  10. Krzysiek says:

    OK I did it myself with a little help of google search :)
    It turns out that client GET parameter changes response to be send as JSON!

    Here is my body of translate (with little changes).

    function translate($what, $from = ‘en’, $to = ‘pl’)
    {
    $url = “http://translate.google.com/translate_a/t?client=p&q=”.urlencode($what).”&hl=”.$to.”&sl=”.$from.”&tl=”.$to.”&ie=UTF-8&oe=UTF-8&multires=0″; // client = p !

    $out = json_decode(curl($url), 1);

    foreach($out[‘sentences’] as $sentence) {
    $ret .= $sentence[‘trans’].’ ‘;
    }

    return stripslashes(trim($ret));
    }

  11. Cron says:

    There’s a limit of 2048 characters in the translation so I’ve modified Krzsiek’s function to split translation into several requests:

    function translate($phrase, $from =’en’, $to = ‘de’){
    $ret = ”;
    if(strlen($phrase)>1600){ //split and translate each part individually to get around 2048 size limit:
    $phrases = str_split($phrase,1600);
    foreach($phrases as $p) $ret .= translate($p, $from, $to);
    return($ret);
    }else{
    $url = “http://translate.google.com/translate_a/t?client=p&q=”.urlencode($phrase).”&hl={$to}&sl={$from}&tl={$to}&ie=UTF-8&oe=UTF-8&multires=0″ ; // client = p !
    $out = json_decode(curl($url), 1);
    foreach($out[‘sentences’] as $sentence) {
    $ret .= $sentence[‘trans’].’ ‘;
    }
    return stripslashes(trim($ret));
    }
    }

  12. Javier says:

    Thanks Rupesh and the other guys for this post.

    If you want to achieve same functionality using python:

    $ sudo pip install translate

    from translate import Translator
    translator= Translator(from_lang=”en”,to_lang=”es”)
    translation = translator.translate(“Hello world”)

    Cheers.

    @jlegidomartinez

  13. Thobib says:

    Thank you very much Rupeshpatel, you save my time to find and lern about grabbing google translator result. thank you once again ;)

  14. Bruce says:

    Very useful, thanks Rupesh; I used it to translate from French to English, although I found that to get a full translation I had to use a (huge!) WordPress function I found on Stackoverflow, to remove all accents from the text.

  15. Shona says:

    hy ppl i need it for android application plz tell how can i use php in it :(

  16. neeraj says:

    hi I am getting the content-type when i am translating my site please suggest me some solution as soon as possible.

  17. naysa says:

    really???google “enable curl in php”???

  18. hablutzel1 says:

    Still working!, by the way “the updated translate function to support all kind of languages supported by Google” is working ok.

  19. Is it possible to do an ajax request on the url? If yes, how to do it?

    Doing this:
    $.ajax({
    url: ‘http://translate.google.com/translate_a/t?client=p&q=hello%20world&hl=fr&sl=en&tl=fr&ie=UTF-8&oe=UTF-8&multires=0′
    }).done(function(data) {
    alert(JSON.parse(data));
    }).error(function(xhr, textStatus, errorThrown) {
    alert(errorThrown);
    });

    gives me an error “Unexpected token”…

    • rupeshpatel says:

      No, you can not because translate.google.com does not allow cross domain requests.
      you can still add this code in php file on you server and can call that url by ajax

      $.ajax({
      url: ‘http://yourwebsite.com/translate.php?text=Hello world′
      }).done(function(data) {
      console.log(data);
      }).error(function(xhr, textStatus, errorThrown) {
      alert(errorThrown);
      });

    • rupeshpatel says:

      That will not b possible, because your browser will prevent cross domain request

  20. avishayhajbi says:

    hello .
    can i change that code into js/ajax ?

  21. khong says:

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

    i cannot find the Ajax request

    please help me

  22. Mr. M says:

    Seriously, tried your code, way to complex with cUrl.. I found out the data creates a txt file after the AJAX, what I did was include that txt using PHP get contents from file, it works with just awesome!!

    Important: the urlencode is needed to send commas etc.

    CODE:

  23. Mr. M says:

    Sorry, CODE here:

    $ToTranslate = “Hello, sir!”;
    $ToTranslate = urlencode($ToTranslate);
    $GetTranslation = file_get_contents(“http://translate.google.com/translate_a/t?client=t&text=” . $ToTranslate . “&hl=en&sl=en&tl=fr&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1″);
    $Translation = explode(‘”‘,$GetTranslation);
    echo $Translation[1];

  24. WoW Its work form me…

  25. willi says:

    that works great! many thanks !!

    can you give the line for german to russian translation?

  26. rashmi says:

    Hi Rupesh, I am trying to call translator from C# program using the url from ur program ‘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′;. I have characters more than 10,000 in my file that need to be translated and I found that after 300 characters, i get error “Your client has issued a malformed or illegal request.” I have never used curl before. So, does the curl handle things differently so many characters can be translated?? Thanks Rashmi

  27. frank says:

    what about when using it for Instant messaging

  28. ZiziPop says:

    There is a bug when translating text that contains double quotes (“) and then the result is exploded by “.
    $name_en = explode(‘”‘,$name_en);

    so better way is to parsing by square brackets [ ]

  29. dskanth says:

    It seems like it works… but is it good to use this code for making translations without paying a single rupee to google? It sounds illegal. I am afraid that my application will get banned later, if google takes any action or imposes fine on me.

    Can someone throw more light on this please ?

  30. It Works Correctly..Thanks A lot ….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s