/** * Translate multiple strings from one language to another. * * Example: * ``` * $translations = $translate->translateBatch([ * 'Hello world!', * 'My name is David.' * ]); * * foreach ($translations as $translation) { * echo $translation['text']; * } * ``` * * @see https://cloud.google.com/translate/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"`. * } * @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 = []) { $response = $this->connection->listTranslations($options + ['q' => $strings, 'key' => $this->key, 'target' => $this->targetLanguage]); $translations = []; foreach ($response['data']['translations'] as $key => $translation) { $source = isset($translation['detectedSourceLanguage']) ? $translation['detectedSourceLanguage'] : $options['source']; $translations[] = ['source' => $source, 'input' => $strings[$key], 'text' => $translation['translatedText']]; } return $translations; }