Only get single translation from Google Translate API

ぐ巨炮叔叔 提交于 2019-12-04 05:03:13

问题


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?

来源:https://stackoverflow.com/questions/43398554/only-get-single-translation-from-google-translate-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!