Google Translate PHP Client Broken

3/3/2016 · 1 min read

Trying to use the PHP Google API Client for Google Translate but can't get any translations out, always getting an empty array when you call getTranslations()?

Try this:


$client = new Google_Client();

$client->setDeveloperKey('xxxx-your-dev-key-xxxx');


$translate = new Google_Service_Translate($client);
$translations = $translate->translations->listTranslations('Hello world!', 'fr');


var_dump($translations->data);
var_dump($translations->data['translations'][0]["translatedText"]);
// Will give you
//
// array(1) {
//   ["translations"]=>
//   array(1) {
//     [0]=>
//     array(2) {
//       ["translatedText"]=>
//       string(17) "Bonjour le monde!"
//       ["detectedSourceLanguage"]=>
//       string(2) "en"
//     }
//   }
// }
// string(17) "Bonjour le monde!"

Unfortunately this client is generated, so it's not as simple as submitting a PR to fix it, but this still works to get hold of those awesome translations.