Cannot create glossary in Cloud Translation API v3

Viewed 493

I'm playing around with Cloud Translation API v3 in PHP. I went through setup process and tried simple translation shown here and it worked. Then I wanted to test glossaries so I tried to add one as described here. When I'm trying to call the function:

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();
        }
    }

it returns an error:

Failed to build request, as the provided path (google.longrunning.Operations/GetOperation) was not found in the configuration.

It throws error at $operationResponse->pollUntilComplete();. The file in bucket contains just one line - test,test.

Then when I try to call function that lists all glossaries, it works but it doesn't return any.

What can cause this problem and how do I add glossary?

1 Answers

You need to install gRPC extension for PHP.

Info: https://cloud.google.com/php/grpc

Install gRPC for PHP

gRPC is a modern, open-source, high-performance remote procedure call framework. If you want to use PHP client libraries for gRPC-enabled APIs, you must install gRPC for PHP. This tutorial explains how to install and enable gRPC.

Install the gRPC extension for PHP.

sudo pecl install grpc

Enable the gRPC extension for PHP.

Add this line anywhere in your php.ini file, for example, /etc/php7/cli/php.ini. You can find this file by running php --ini.

extension=grpc.so
Related