Jump to content

Anyone familiar with Google Translate API?


soma56

Recommended Posts

I've signed up for Google's translating service and I'm able to get results, however, it's wrapped around some information. Here's an example:

 


//Content to translate - Spanish for 'Hello World'
$content = urlencode("Hola Mundo");

$translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en");

echo $translate;

 

The code returns

 

{ "data": { "translations": [ { "translatedText": "Hello World" } ] } }

 

All I want is the result Hello World

 

I tried using str_replace but it does not work through file_get_contents. The Google Translate does not provide any information on how to do this. Does anyone have any ideas?

Thanks, this is where I'm at right now:

 

$translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en");

$obj = json_decode($translate);
print "TEST 1".$obj->{'translatedText'};
echo "<br />";

$response = json_decode($translate);

echo "TEST 2".$response->data->translations->translatedText;
echo "<br />";

echo "TEST 3".$response->translatedText;
echo "<br />";

$response2 = json_decode($translate, true);

echo "TEST 4".$response2['translatedText'];
echo "<br />";

 

No results returned but 'json' is working based on an example from php.net. Got a feeling I'm in the right direction?

Thanks, getting somewhere:

 

array(1) { ["data"]=> array(1) { ["translations"]=> array(1) { [0]=> array(1) { ["translatedText"]=> string(11) "Hello World" } } } } 

 

I appreciate it and I'll see if I can learn how to figure out the rest.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.