Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

translate a PHP $string using google translator API

been google'ing for a while how is the best way to translate with google translator in PHP, found very different ways converting URLS, or using Js but i want to do it only with php (or with a very simple solution JS/JQUery)

example:

//hopefully with $from_lan and $to_lan being like 'en','de', .. or similar
function translate($from_lan, $to_lan, $text){

// do

return $translated_text;

}

can you give me a clue? or maybe you already have this function..

my intention it's to use it only for the languages i have not already defined (or keys i haven't defined), that's why i wan it so simple, will be only temporal..

EDIT

thanks for your replies we are now trying this soulutions:

function auto_translate($from_lan, $to_lan, $text){
// do


$json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
$translated_text = $json->responseData->translatedText;


return $translated_text;

}

(there was a extra 'g' on variables for lang... anyway)

it returns: works now :)

i don't really understand much the function, so any idea why is not acepting the object? (now i do)

OR:

    function auto_translate($from_lan, $to_lan, $text){
    // do

//    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
//    $translated_text = $json['responseData']['translatedText'];
    error_reporting(1);
    require_once('GTranslate.php');
    try{
       $gt = new Gtranslate();
       $translated_text = $gt->english_to_german($text);

     } catch (GTranslateException $ge)
     {
           $translated_text= $ge->getMessage();
     }


    return $translated_text;
}

And this one looks great but it doesn't even gives me an error, the page won't load (error_report(1) :S)

thanks in advance!

like image 714
Toni Michel Caubet Avatar asked Jan 09 '11 16:01

Toni Michel Caubet


People also ask

How do I use Google Translate API in PHP?

To use Google Translate API in PHP, you need to get Google API “Server Key”. All the translate API are called using GET requests. So we can use file_get_contents() or PHP CURL Library.To encode the parameter, rawurlencode() function is used. To decode JSON text as object, json_decode() function is used.

Can I use Google Translate API for free?

The Google Translate API is not free. Its pricing is based off monthly usage in terms of millions of characters. It costs $20 per 1 million characters for translation or language detection. Price is per character sent to the API for processing, including whitespace characters.


1 Answers

I have new solution for this.. Because last solution need new version and some fetched other issue.


    $text = 'Test new message only.';
    $apiKey = '<past your google api key here>';
    $url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=fr';
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handle);
    $responseDecoded = json_decode($response, true);

    curl_close($handle);

    print_r($responseDecoded['data']['translations'][0]['translatedText']);
    die;

    //expected output
     Testez le nouveau message uniquement.

I hope is very helpful in PHP

like image 146
hadiya vipul Avatar answered Oct 18 '22 01:10

hadiya vipul