-->

Only get single translation from Google Translate

2019-03-04 17:19发布

问题:

So I'm using the Google Translate API in PHP as explained in the documentation...

require 'vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=src/i18n-php/credential.json');
use Google\Cloud\Translate\TranslateClient;
$g_tr = new TranslateClient([ 'projectId' => 'my-project' ]);
$source_language = "en";
$target_language = "de";
$text_to_translate = "Last name";
$result = $g_tr->translate($text_to_translate, [
                              'source' => $source_language,
                              'target' => $target_language,
                          ]);
echo $result['text'];

This works just as expected until there are several translation possibilities, like in the above code example, where the German translation can either be "Familienname" or "Nachname" - The Google Translate API outputs both into the $result['text'] seperated by comma: Familienname, Nachname.

I've been searching my fingers sore looking for a way to only get a single (preferably the most popular) translation. What do I have to do so that I only get one single translation possibility back from Google?