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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

worth a try.

$translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en");
$arr = json_decode($translate, true);
var_dump($arr);
echo $arr['data']['translations']['translatedText'];

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.