【发布时间】:2020-11-28 01:11:30
【问题描述】:
我正在使用 PHP 中的 Cloud Translation API v3。我完成了设置过程并尝试了显示here 的简单翻译,它成功了。然后我想测试词汇表,所以我尝试按照here 的描述添加一个。当我尝试调用该函数时:
protected function createGlossary()
{
$translationServiceClient = new TranslationServiceClient();
$projectId = 'my-project-id';
$glossaryId = 'my-new-glossary';
$inputUri = 'gs://bucket/file.csv';
$formattedParent = $translationServiceClient->locationName(
$projectId,
'us-central1'
);
$formattedName = $translationServiceClient->glossaryName(
$projectId,
'us-central1',
$glossaryId
);
$languageCodesElement = 'pl';
$languageCodesElement2 = 'en';
$languageCodes = [$languageCodesElement, $languageCodesElement2];
$languageCodesSet = new LanguageCodesSet();
$languageCodesSet->setLanguageCodes($languageCodes);
$gcsSource = (new GcsSource())
->setInputUri($inputUri);
$inputConfig = (new GlossaryInputConfig())
->setGcsSource($gcsSource);
$glossary = (new Glossary())
->setName($formattedName)
->setLanguageCodesSet($languageCodesSet)
->setInputConfig($inputConfig);
try {
$operationResponse = $translationServiceClient->createGlossary(
$formattedParent,
$glossary
);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
printf('Created Glossary.' . PHP_EOL);
printf('Glossary name: %s' . PHP_EOL, $response->getName());
printf('Entry count: %s' . PHP_EOL, $response->getEntryCount());
printf(
'Input URI: %s' . PHP_EOL,
$response->getInputConfig()
->getGcsSource()
->getInputUri()
);
} else {
$error = $operationResponse->getError();
// handleError($error)
}
} finally {
$translationServiceClient->close();
}
}
它返回一个错误:
Failed to build request, as the provided path (google.longrunning.Operations/GetOperation) was not found in the configuration.
它在$operationResponse->pollUntilComplete(); 处引发错误。存储桶中的文件只包含一行 - test,test。
然后,当我尝试调用列出所有词汇表的函数时,它可以工作,但没有返回任何内容。
什么会导致这个问题?如何添加词汇表?
【问题讨论】:
标签: php google-cloud-platform google-translate google-translation-api