Skip to content

Commit

Permalink
chore: upgrade translate samples to new surface (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 5, 2024
1 parent 5a8554b commit c3054b4
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 109 deletions.
4 changes: 2 additions & 2 deletions translate/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "google/translate-sample",
"type": "project",
"require": {
"google/cloud-translate": "^1.7.2"
"google/cloud-translate": "^1.17"
},
"require-dev": {
"google/cloud-storage": "^1.14"
"google/cloud-storage": "^1.36"
}
}
17 changes: 9 additions & 8 deletions translate/src/v3_batch_translate_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_batch_translate_text]
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GcsDestination;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\InputConfig;
use Google\Cloud\Translate\V3\OutputConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;

/**
* @param string $inputUri Path to to source input (e.g. "gs://cloud-samples-data/text.txt").
Expand Down Expand Up @@ -59,13 +60,13 @@ function v3_batch_translate_text(
$formattedParent = $translationServiceClient->locationName($projectId, $location);

try {
$operationResponse = $translationServiceClient->batchTranslateText(
$formattedParent,
$sourceLanguage,
$targetLanguageCodes,
$inputConfigs,
$outputConfig
);
$request = (new BatchTranslateTextRequest())
->setParent($formattedParent)
->setSourceLanguageCode($sourceLanguage)
->setTargetLanguageCodes($targetLanguageCodes)
->setInputConfigs($inputConfigs)
->setOutputConfig($outputConfig);
$operationResponse = $translationServiceClient->batchTranslateText($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
Expand Down
19 changes: 10 additions & 9 deletions translate/src/v3_batch_translate_text_with_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_batch_translate_text_with_glossary]
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GcsDestination;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\InputConfig;
use Google\Cloud\Translate\V3\OutputConfig;
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;

/**
* @param string $inputUri Path to to source input (e.g. "gs://cloud-samples-data/text.txt").
Expand Down Expand Up @@ -73,14 +74,14 @@ function v3_batch_translate_text_with_glossary(
$glossaries = ['ja' => $glossariesItem];

try {
$operationResponse = $translationServiceClient->batchTranslateText(
$formattedParent,
$sourceLanguage,
$targetLanguageCodes,
$inputConfigs,
$outputConfig,
['glossaries' => $glossaries]
);
$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();
Expand Down
23 changes: 11 additions & 12 deletions translate/src/v3_batch_translate_text_with_glossary_and_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_batch_translate_text_with_glossary_and_model]
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GcsDestination;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\InputConfig;
use Google\Cloud\Translate\V3\OutputConfig;
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;

/**
* @param string $inputUri Path to to source input (e.g. "gs://cloud-samples-data/text.txt").
Expand Down Expand Up @@ -79,17 +80,15 @@ function v3_batch_translate_text_with_glossary_and_model(
$glossaries = ['ja' => $glossariesItem];

try {
$operationResponse = $translationServiceClient->batchTranslateText(
$formattedParent,
$sourceLanguage,
$targetLanguageCodes,
$inputConfigs,
$outputConfig,
[
'models' => $models,
'glossaries' => $glossaries
]
);
$request = (new BatchTranslateTextRequest())
->setParent($formattedParent)
->setSourceLanguageCode($sourceLanguage)
->setTargetLanguageCodes($targetLanguageCodes)
->setInputConfigs($inputConfigs)
->setOutputConfig($outputConfig)
->setModels($models)
->setGlossaries($glossaries);
$operationResponse = $translationServiceClient->batchTranslateText($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
Expand Down
19 changes: 10 additions & 9 deletions translate/src/v3_batch_translate_text_with_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_batch_translate_text_with_model]
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GcsDestination;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\InputConfig;
use Google\Cloud\Translate\V3\OutputConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;

/**
* @param string $inputUri Path to to source input (e.g. "gs://cloud-samples-data/text.txt").
Expand Down Expand Up @@ -68,14 +69,14 @@ function v3_batch_translate_text_with_model(
$models = ['ja' => $modelPath];

try {
$operationResponse = $translationServiceClient->batchTranslateText(
$formattedParent,
$sourceLanguage,
$targetLanguageCodes,
$inputConfigs,
$outputConfig,
['models' => $models]
);
$request = (new BatchTranslateTextRequest())
->setParent($formattedParent)
->setSourceLanguageCode($sourceLanguage)
->setTargetLanguageCodes($targetLanguageCodes)
->setInputConfigs($inputConfigs)
->setOutputConfig($outputConfig)
->setModels($models);
$operationResponse = $translationServiceClient->batchTranslateText($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
Expand Down
13 changes: 7 additions & 6 deletions translate/src/v3_create_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_create_glossary]
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\CreateGlossaryRequest;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\Glossary;
use Google\Cloud\Translate\V3\GlossaryInputConfig;
use Google\Cloud\Translate\V3\Glossary\LanguageCodesSet;
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\GlossaryInputConfig;

/**
* @param string $projectId Your Google Cloud project ID.
Expand Down Expand Up @@ -60,10 +61,10 @@ function v3_create_glossary(
->setInputConfig($inputConfig);

try {
$operationResponse = $translationServiceClient->createGlossary(
$formattedParent,
$glossary
);
$request = (new CreateGlossaryRequest())
->setParent($formattedParent)
->setGlossary($glossary);
$operationResponse = $translationServiceClient->createGlossary($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
Expand Down
7 changes: 5 additions & 2 deletions translate/src/v3_delete_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_delete_glossary]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\DeleteGlossaryRequest;

/**
* @param string $projectId Your Google Cloud project ID.
Expand All @@ -35,7 +36,9 @@ function v3_delete_glossary(string $projectId, string $glossaryId): void
);

try {
$operationResponse = $translationServiceClient->deleteGlossary($formattedName);
$request = (new DeleteGlossaryRequest())
->setName($formattedName);
$operationResponse = $translationServiceClient->deleteGlossary($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
Expand Down
15 changes: 7 additions & 8 deletions translate/src/v3_detect_language.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_detect_language]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\DetectLanguageRequest;

/**
* @param string $text The text whose language to detect. This will be detected as en.
Expand All @@ -37,13 +38,11 @@ function v3_detect_language(string $text, string $projectId): void
$mimeType = 'text/plain';

try {
$response = $translationServiceClient->detectLanguage(
$formattedParent,
[
'content' => $text,
'mimeType' => $mimeType
]
);
$request = (new DetectLanguageRequest())
->setParent($formattedParent)
->setContent($text)
->setMimeType($mimeType);
$response = $translationServiceClient->detectLanguage($request);
// Display list of detected languages sorted by detection confidence.
// The most probable language is first.
foreach ($response->getLanguages() as $language) {
Expand Down
7 changes: 5 additions & 2 deletions translate/src/v3_get_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_get_glossary]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GetGlossaryRequest;

/**
* @param string $projectId Your Google Cloud project ID.
Expand All @@ -35,7 +36,9 @@ function v3_get_glossary(string $projectId, string $glossaryId): void
);

try {
$response = $translationServiceClient->getGlossary($formattedName);
$request = (new GetGlossaryRequest())
->setName($formattedName);
$response = $translationServiceClient->getGlossary($request);
printf('Glossary name: %s' . PHP_EOL, $response->getName());
printf('Entry count: %s' . PHP_EOL, $response->getEntryCount());
printf(
Expand Down
7 changes: 5 additions & 2 deletions translate/src/v3_get_supported_languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_get_supported_languages]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GetSupportedLanguagesRequest;

/**
* @param string $projectId Your Google Cloud project ID.
Expand All @@ -30,7 +31,9 @@ function v3_get_supported_languages(string $projectId): void
$formattedParent = $translationServiceClient->locationName($projectId, 'global');

try {
$response = $translationServiceClient->getSupportedLanguages($formattedParent);
$request = (new GetSupportedLanguagesRequest())
->setParent($formattedParent);
$response = $translationServiceClient->getSupportedLanguages($request);
// List language codes of supported languages
foreach ($response->getLanguages() as $language) {
printf('Language Code: %s' . PHP_EOL, $language->getLanguageCode());
Expand Down
11 changes: 6 additions & 5 deletions translate/src/v3_get_supported_languages_for_target.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_get_supported_languages_for_target]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\GetSupportedLanguagesRequest;

/**
* @param string $projectId Your Google Cloud project ID.
Expand All @@ -31,10 +32,10 @@ function v3_get_supported_languages_for_target(string $languageCode, string $pro
$formattedParent = $translationServiceClient->locationName($projectId, 'global');

try {
$response = $translationServiceClient->getSupportedLanguages(
$formattedParent,
['displayLanguageCode' => $languageCode]
);
$request = (new GetSupportedLanguagesRequest())
->setParent($formattedParent)
->setDisplayLanguageCode($languageCode);
$response = $translationServiceClient->getSupportedLanguages($request);
// List language codes of supported languages
foreach ($response->getLanguages() as $language) {
printf('Language Code: %s' . PHP_EOL, $language->getLanguageCode());
Expand Down
7 changes: 5 additions & 2 deletions translate/src/v3_list_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_list_glossary]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\ListGlossariesRequest;

/**
* @param string $projectId Your Google Cloud project ID.
Expand All @@ -34,7 +35,9 @@ function v3_list_glossary(string $projectId): void

try {
// Iterate through all elements
$pagedResponse = $translationServiceClient->listGlossaries($formattedParent);
$request = (new ListGlossariesRequest())
->setParent($formattedParent);
$pagedResponse = $translationServiceClient->listGlossaries($request);
foreach ($pagedResponse->iterateAllElements() as $responseItem) {
printf('Glossary name: %s' . PHP_EOL, $responseItem->getName());
printf('Entry count: %s' . PHP_EOL, $responseItem->getEntryCount());
Expand Down
13 changes: 7 additions & 6 deletions translate/src/v3_translate_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_translate_text]
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\TranslateTextRequest;

/**
* @param string $text The text to translate.
Expand All @@ -36,11 +37,11 @@ function v3_translate_text(
$formattedParent = $translationServiceClient->locationName($projectId, 'global');

try {
$response = $translationServiceClient->translateText(
$contents,
$targetLanguage,
$formattedParent
);
$request = (new TranslateTextRequest())
->setContents($contents)
->setTargetLanguageCode($targetLanguage)
->setParent($formattedParent);
$response = $translationServiceClient->translateText($request);
// Display the translation for each input text provided
foreach ($response->getTranslations() as $translation) {
printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());
Expand Down
21 changes: 10 additions & 11 deletions translate/src/v3_translate_text_with_glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
namespace Google\Cloud\Samples\Translate;

// [START translate_v3_translate_text_with_glossary]
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;
use Google\Cloud\Translate\V3\TranslateTextRequest;

/**
* @param string $text The text to translate.
Expand Down Expand Up @@ -54,16 +55,14 @@ function v3_translate_text_with_glossary(
$mimeType = 'text/plain';

try {
$response = $translationServiceClient->translateText(
$contents,
$targetLanguage,
$formattedParent,
[
'sourceLanguageCode' => $sourceLanguage,
'glossaryConfig' => $glossaryConfig,
'mimeType' => $mimeType
]
);
$request = (new TranslateTextRequest())
->setContents($contents)
->setTargetLanguageCode($targetLanguage)
->setParent($formattedParent)
->setSourceLanguageCode($sourceLanguage)
->setGlossaryConfig($glossaryConfig)
->setMimeType($mimeType);
$response = $translationServiceClient->translateText($request);
// Display the translation for each input text provided
foreach ($response->getGlossaryTranslations() as $translation) {
printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());
Expand Down
Loading

0 comments on commit c3054b4

Please sign in to comment.