Translate/src/TranslateClient.php
public function translateBatch(array $strings, array $options = [])
    {
        $options += [
            'model' => null,
        ];

        $options = array_filter($options + [
            'q' => $strings,
            'key' => $this->key,
            'target' => $this->targetLanguage,
            'model' => $options['model']
        ], function ($opt) {
            return !is_null($opt);
        });

        $response = $this->connection->listTranslations($options);

        $translations = [];
        $strings = array_values($strings);

        if (isset($response['data']['translations'])) {
            foreach ($response['data']['translations'] as $key => $translation) {
                $source = isset($translation['detectedSourceLanguage'])
                    ? $translation['detectedSourceLanguage']
                    : $options['source'];

                $model = (isset($translation['model']))
                    ? $translation['model']
                    : null;

                $translations[] = [
                    'source' => $source,
                    'input' => $strings[$key],
                    'text' => $translation['translatedText'],
                    'model' => $model
                ];
            }
        }

        return $translations;
    }
public function translateBatch(array $strings, array $options = [])
    {
        $options += [
            'model' => null,
        ];

        $options = array_filter($options + [
            'q' => $strings,
            'key' => $this->key,
            'target' => $this->targetLanguage,
            'model' => $options['model']
        ], function ($opt) {
            return !is_null($opt);
        });

        $response = $this->connection->listTranslations($options);

        $translations = [];
        $strings = array_values($strings);

        if (isset($response['data']['translations'])) {
            foreach ($response['data']['translations'] as $key => $translation) {
                $source = isset($translation['detectedSourceLanguage'])
                    ? $translation['detectedSourceLanguage']
                    : $options['source'];

                $model = (isset($translation['model']))
                    ? $translation['model']
                    : null;

                $translations[] = [
                    'source' => $source,
                    'input' => $strings[$key],
                    'text' => $translation['translatedText'],
                    'model' => $model
                ];
            }
        }

        return $translations;
    }
public function translateBatch ( array $ strings , array $ options = [ ] ) { $ options += [ 'model' => null , ] ; $ options = array_filter ( $ options + [ 'q' => $ strings , 'key' => $ this -> key , 'target' => $ this -> targetLanguage , 'model' => $ options [ 'model' ] ] , function ( $ opt ) { return ! is_null ( $ opt ) ; } ) ; $ response = $ this -> connection -> listTranslations ( $ options ) ; $ translations = [ ] ; $ strings = array_values ( $ strings ) ; if ( isset ( $ response [ 'data' ] [ 'translations' ] ) ) { foreach ( $ response [ 'data' ] [ 'translations' ] as $ key => $ translation ) { $ source = isset ( $ translation [ 'detectedSourceLanguage' ] ) ? $ translation [ 'detectedSourceLanguage' ] : $ options [ 'source' ] ; $ model = ( isset ( $ translation [ 'model' ] ) ) ? $ translation [ 'model' ] : null ; $ translations [ ] = [ 'source' => $ source , 'input' => $ strings [ $ key ] , 'text' => $ translation [ 'translatedText' ] , 'model' => $ model ] ; } } return $ translations ; }
Translate multiple strings from one language to another.

Example:
```
$results = $translate->translateBatch([
'Hello world!',
'My name is David.'
]);

foreach ($results as $result) {
echo $result['text'];
}
```

@see https://cloud.google.com/translation/v2/translating-text-with-rest Translating Text

@param array $strings An array of strings to translate.
@param array $options [optional] {
Configuration Options.

@type string $source The source language to translate from. Must be a
valid ISO 639-1 language code. If not provided the value will
be automatically detected by the server.
@type string $target The target language to translate to. Must be a
valid ISO 639-1 language code. **Defaults to** the value assigned
to the client (`"en"` by default).
@type string $format Indicates whether the string to be translated is
either plain-text or HTML. Acceptable values are `html` or
`text`. **Defaults to** `"html"`.
@type string $model The model to use for the translation request. May
be `nmt` (for the NMT model) or `base` (for the PBMT model).
**Defaults to** using the NMT model. If the NMT model is not
supported for the requested language translation pair, the PBMT
model will be defaulted instead. For a list of supported
languages for the model types, please see the
[Language Support](https://cloud.google.com/translate/docs/languages)
documentation.
}
@return array A set of translation results. Each result includes a
`source` key containing the detected or provided language of the
provided input, an `input` key containing the original string,
and a `text` key containing the translated result.
Translate multiple strings from one language to another .