in translate/src/v3_batch_translate_text_with_glossary.php [38:98]
function v3_batch_translate_text_with_glossary(
string $inputUri,
string $outputUri,
string $projectId,
string $location,
string $glossaryId,
string $targetLanguage,
string $sourceLanguage
): void {
$translationServiceClient = new TranslationServiceClient();
$glossaryPath = $translationServiceClient->glossaryName(
$projectId,
$location,
$glossaryId
);
$targetLanguageCodes = [$targetLanguage];
$gcsSource = (new GcsSource())
->setInputUri($inputUri);
// Optional. Can be "text/plain" or "text/html".
$mimeType = 'text/plain';
$inputConfigsElement = (new InputConfig())
->setGcsSource($gcsSource)
->setMimeType($mimeType);
$inputConfigs = [$inputConfigsElement];
$gcsDestination = (new GcsDestination())
->setOutputUriPrefix($outputUri);
$outputConfig = (new OutputConfig())
->setGcsDestination($gcsDestination);
$formattedParent = $translationServiceClient->locationName(
$projectId,
$location
);
$glossariesItem = (new TranslateTextGlossaryConfig())
->setGlossary($glossaryPath);
$glossaries = ['ja' => $glossariesItem];
try {
$request = (new BatchTranslateTextRequest())
->setParent($formattedParent)
->setSourceLanguageCode($sourceLanguage)
->setTargetLanguageCodes($targetLanguageCodes)
->setInputConfigs($inputConfigs)
->setOutputConfig($outputConfig)
->setGlossaries($glossaries);
$operationResponse = $translationServiceClient->batchTranslateText($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
// Display the translation for each input text provided
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
} else {
$error = $operationResponse->getError();
print($error->getMessage());
}
} finally {
$translationServiceClient->close();
}
}