Google\Cloud\Translate\TranslateClient::translateBatch PHP Method

translateBatch() public method

Example: $translations = $translate->translateBatch([ 'Hello world!', 'My name is David.' ]); foreach ($translations as $translation) { echo $translation['text']; }
See also: https://cloud.google.com/translate/v2/translating-text-with-rest Translating Text
public translateBatch ( array $strings, array $options = [] ) : array
$strings array An array of strings to translate.
$options array [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` or `base`. **Defaults to** an empty string. }
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.
    public function translateBatch(array $strings, array $options = [])
    {
        $options += ['model' => ''];
        $response = $this->connection->listTranslations($options + ['q' => $strings, 'key' => $this->key, 'target' => $this->targetLanguage, 'model' => $options['model']]);
        $translations = [];
        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;
    }