Search code examples
phpgoogle-translate

Google Translate & PHP - Getting alternative translations


I have a very simple translation script using Google translate like so:

use Google\Cloud\Translate\TranslateClient;
$translate = new TranslateClient([
    'key' => 'xxmyxsuperxsecretxapixkeyxx'
]);

$result = $translate->translate($string, [
    'target' => $lang
]);

$translation = $result['text'];

This gives me back a nice array of data like this:

array(4) { 
    ["source"]=> string(2) "en" 
    ["input"]=> string(10) "dummy text" 
    ["text"]=> string(11) "dummer Text" 
    ["model"]=> NULL 
}

So the first question is what is $result['model'] in this response?

The second question is, what can I do if I want to get other suggestions like on the actual Google Translate page like this:

enter image description here


Solution

  • what is $result['model'] in this response?

    From php client library docs:

    The model to use for the translation request.

    Regarding your second question: the translate client sends requests over the REST API and the latter's docs do not show any support for getting the additional suggestions you see in the translate web UI. One alternative suggestion would be to attempt to scrape the results but, honestly, I have no idea how you could do that using php. Here you can see an answer to an older question regarding doing so with python and BeautifulSoup and here one using node.js, maybe these could prove helpful.