Skip to content

Commit c3054b4

Browse files
authored
chore: upgrade translate samples to new surface (#1883)
1 parent 5a8554b commit c3054b4

17 files changed

+122
-109
lines changed

translate/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "google/translate-sample",
33
"type": "project",
44
"require": {
5-
"google/cloud-translate": "^1.7.2"
5+
"google/cloud-translate": "^1.17"
66
},
77
"require-dev": {
8-
"google/cloud-storage": "^1.14"
8+
"google/cloud-storage": "^1.36"
99
}
1010
}

translate/src/v3_batch_translate_text.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_batch_translate_text]
21+
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
22+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
2123
use Google\Cloud\Translate\V3\GcsDestination;
2224
use Google\Cloud\Translate\V3\GcsSource;
2325
use Google\Cloud\Translate\V3\InputConfig;
2426
use Google\Cloud\Translate\V3\OutputConfig;
25-
use Google\Cloud\Translate\V3\TranslationServiceClient;
2627

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

6162
try {
62-
$operationResponse = $translationServiceClient->batchTranslateText(
63-
$formattedParent,
64-
$sourceLanguage,
65-
$targetLanguageCodes,
66-
$inputConfigs,
67-
$outputConfig
68-
);
63+
$request = (new BatchTranslateTextRequest())
64+
->setParent($formattedParent)
65+
->setSourceLanguageCode($sourceLanguage)
66+
->setTargetLanguageCodes($targetLanguageCodes)
67+
->setInputConfigs($inputConfigs)
68+
->setOutputConfig($outputConfig);
69+
$operationResponse = $translationServiceClient->batchTranslateText($request);
6970
$operationResponse->pollUntilComplete();
7071
if ($operationResponse->operationSucceeded()) {
7172
$response = $operationResponse->getResult();

translate/src/v3_batch_translate_text_with_glossary.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_batch_translate_text_with_glossary]
21+
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
22+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
2123
use Google\Cloud\Translate\V3\GcsDestination;
2224
use Google\Cloud\Translate\V3\GcsSource;
2325
use Google\Cloud\Translate\V3\InputConfig;
2426
use Google\Cloud\Translate\V3\OutputConfig;
2527
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
26-
use Google\Cloud\Translate\V3\TranslationServiceClient;
2728

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

7576
try {
76-
$operationResponse = $translationServiceClient->batchTranslateText(
77-
$formattedParent,
78-
$sourceLanguage,
79-
$targetLanguageCodes,
80-
$inputConfigs,
81-
$outputConfig,
82-
['glossaries' => $glossaries]
83-
);
77+
$request = (new BatchTranslateTextRequest())
78+
->setParent($formattedParent)
79+
->setSourceLanguageCode($sourceLanguage)
80+
->setTargetLanguageCodes($targetLanguageCodes)
81+
->setInputConfigs($inputConfigs)
82+
->setOutputConfig($outputConfig)
83+
->setGlossaries($glossaries);
84+
$operationResponse = $translationServiceClient->batchTranslateText($request);
8485
$operationResponse->pollUntilComplete();
8586
if ($operationResponse->operationSucceeded()) {
8687
$response = $operationResponse->getResult();

translate/src/v3_batch_translate_text_with_glossary_and_model.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_batch_translate_text_with_glossary_and_model]
21+
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
22+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
2123
use Google\Cloud\Translate\V3\GcsDestination;
2224
use Google\Cloud\Translate\V3\GcsSource;
2325
use Google\Cloud\Translate\V3\InputConfig;
2426
use Google\Cloud\Translate\V3\OutputConfig;
2527
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
26-
use Google\Cloud\Translate\V3\TranslationServiceClient;
2728

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

8182
try {
82-
$operationResponse = $translationServiceClient->batchTranslateText(
83-
$formattedParent,
84-
$sourceLanguage,
85-
$targetLanguageCodes,
86-
$inputConfigs,
87-
$outputConfig,
88-
[
89-
'models' => $models,
90-
'glossaries' => $glossaries
91-
]
92-
);
83+
$request = (new BatchTranslateTextRequest())
84+
->setParent($formattedParent)
85+
->setSourceLanguageCode($sourceLanguage)
86+
->setTargetLanguageCodes($targetLanguageCodes)
87+
->setInputConfigs($inputConfigs)
88+
->setOutputConfig($outputConfig)
89+
->setModels($models)
90+
->setGlossaries($glossaries);
91+
$operationResponse = $translationServiceClient->batchTranslateText($request);
9392
$operationResponse->pollUntilComplete();
9493
if ($operationResponse->operationSucceeded()) {
9594
$response = $operationResponse->getResult();

translate/src/v3_batch_translate_text_with_model.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_batch_translate_text_with_model]
21+
use Google\Cloud\Translate\V3\BatchTranslateTextRequest;
22+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
2123
use Google\Cloud\Translate\V3\GcsDestination;
2224
use Google\Cloud\Translate\V3\GcsSource;
2325
use Google\Cloud\Translate\V3\InputConfig;
2426
use Google\Cloud\Translate\V3\OutputConfig;
25-
use Google\Cloud\Translate\V3\TranslationServiceClient;
2627

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

7071
try {
71-
$operationResponse = $translationServiceClient->batchTranslateText(
72-
$formattedParent,
73-
$sourceLanguage,
74-
$targetLanguageCodes,
75-
$inputConfigs,
76-
$outputConfig,
77-
['models' => $models]
78-
);
72+
$request = (new BatchTranslateTextRequest())
73+
->setParent($formattedParent)
74+
->setSourceLanguageCode($sourceLanguage)
75+
->setTargetLanguageCodes($targetLanguageCodes)
76+
->setInputConfigs($inputConfigs)
77+
->setOutputConfig($outputConfig)
78+
->setModels($models);
79+
$operationResponse = $translationServiceClient->batchTranslateText($request);
7980
$operationResponse->pollUntilComplete();
8081
if ($operationResponse->operationSucceeded()) {
8182
$response = $operationResponse->getResult();

translate/src/v3_create_glossary.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_create_glossary]
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\CreateGlossaryRequest;
2123
use Google\Cloud\Translate\V3\GcsSource;
2224
use Google\Cloud\Translate\V3\Glossary;
23-
use Google\Cloud\Translate\V3\GlossaryInputConfig;
2425
use Google\Cloud\Translate\V3\Glossary\LanguageCodesSet;
25-
use Google\Cloud\Translate\V3\TranslationServiceClient;
26+
use Google\Cloud\Translate\V3\GlossaryInputConfig;
2627

2728
/**
2829
* @param string $projectId Your Google Cloud project ID.
@@ -60,10 +61,10 @@ function v3_create_glossary(
6061
->setInputConfig($inputConfig);
6162

6263
try {
63-
$operationResponse = $translationServiceClient->createGlossary(
64-
$formattedParent,
65-
$glossary
66-
);
64+
$request = (new CreateGlossaryRequest())
65+
->setParent($formattedParent)
66+
->setGlossary($glossary);
67+
$operationResponse = $translationServiceClient->createGlossary($request);
6768
$operationResponse->pollUntilComplete();
6869
if ($operationResponse->operationSucceeded()) {
6970
$response = $operationResponse->getResult();

translate/src/v3_delete_glossary.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_delete_glossary]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\DeleteGlossaryRequest;
2223

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

3738
try {
38-
$operationResponse = $translationServiceClient->deleteGlossary($formattedName);
39+
$request = (new DeleteGlossaryRequest())
40+
->setName($formattedName);
41+
$operationResponse = $translationServiceClient->deleteGlossary($request);
3942
$operationResponse->pollUntilComplete();
4043
if ($operationResponse->operationSucceeded()) {
4144
$response = $operationResponse->getResult();

translate/src/v3_detect_language.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_detect_language]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\DetectLanguageRequest;
2223

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

3940
try {
40-
$response = $translationServiceClient->detectLanguage(
41-
$formattedParent,
42-
[
43-
'content' => $text,
44-
'mimeType' => $mimeType
45-
]
46-
);
41+
$request = (new DetectLanguageRequest())
42+
->setParent($formattedParent)
43+
->setContent($text)
44+
->setMimeType($mimeType);
45+
$response = $translationServiceClient->detectLanguage($request);
4746
// Display list of detected languages sorted by detection confidence.
4847
// The most probable language is first.
4948
foreach ($response->getLanguages() as $language) {

translate/src/v3_get_glossary.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_get_glossary]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\GetGlossaryRequest;
2223

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

3738
try {
38-
$response = $translationServiceClient->getGlossary($formattedName);
39+
$request = (new GetGlossaryRequest())
40+
->setName($formattedName);
41+
$response = $translationServiceClient->getGlossary($request);
3942
printf('Glossary name: %s' . PHP_EOL, $response->getName());
4043
printf('Entry count: %s' . PHP_EOL, $response->getEntryCount());
4144
printf(

translate/src/v3_get_supported_languages.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_get_supported_languages]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\GetSupportedLanguagesRequest;
2223

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

3233
try {
33-
$response = $translationServiceClient->getSupportedLanguages($formattedParent);
34+
$request = (new GetSupportedLanguagesRequest())
35+
->setParent($formattedParent);
36+
$response = $translationServiceClient->getSupportedLanguages($request);
3437
// List language codes of supported languages
3538
foreach ($response->getLanguages() as $language) {
3639
printf('Language Code: %s' . PHP_EOL, $language->getLanguageCode());

translate/src/v3_get_supported_languages_for_target.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_get_supported_languages_for_target]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\GetSupportedLanguagesRequest;
2223

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

3334
try {
34-
$response = $translationServiceClient->getSupportedLanguages(
35-
$formattedParent,
36-
['displayLanguageCode' => $languageCode]
37-
);
35+
$request = (new GetSupportedLanguagesRequest())
36+
->setParent($formattedParent)
37+
->setDisplayLanguageCode($languageCode);
38+
$response = $translationServiceClient->getSupportedLanguages($request);
3839
// List language codes of supported languages
3940
foreach ($response->getLanguages() as $language) {
4041
printf('Language Code: %s' . PHP_EOL, $language->getLanguageCode());

translate/src/v3_list_glossary.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_list_glossary]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\ListGlossariesRequest;
2223

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

3536
try {
3637
// Iterate through all elements
37-
$pagedResponse = $translationServiceClient->listGlossaries($formattedParent);
38+
$request = (new ListGlossariesRequest())
39+
->setParent($formattedParent);
40+
$pagedResponse = $translationServiceClient->listGlossaries($request);
3841
foreach ($pagedResponse->iterateAllElements() as $responseItem) {
3942
printf('Glossary name: %s' . PHP_EOL, $responseItem->getName());
4043
printf('Entry count: %s' . PHP_EOL, $responseItem->getEntryCount());

translate/src/v3_translate_text.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_translate_text]
21-
use Google\Cloud\Translate\V3\TranslationServiceClient;
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
use Google\Cloud\Translate\V3\TranslateTextRequest;
2223

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

3839
try {
39-
$response = $translationServiceClient->translateText(
40-
$contents,
41-
$targetLanguage,
42-
$formattedParent
43-
);
40+
$request = (new TranslateTextRequest())
41+
->setContents($contents)
42+
->setTargetLanguageCode($targetLanguage)
43+
->setParent($formattedParent);
44+
$response = $translationServiceClient->translateText($request);
4445
// Display the translation for each input text provided
4546
foreach ($response->getTranslations() as $translation) {
4647
printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());

translate/src/v3_translate_text_with_glossary.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
namespace Google\Cloud\Samples\Translate;
1919

2020
// [START translate_v3_translate_text_with_glossary]
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
2122
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
22-
use Google\Cloud\Translate\V3\TranslationServiceClient;
23+
use Google\Cloud\Translate\V3\TranslateTextRequest;
2324

2425
/**
2526
* @param string $text The text to translate.
@@ -54,16 +55,14 @@ function v3_translate_text_with_glossary(
5455
$mimeType = 'text/plain';
5556

5657
try {
57-
$response = $translationServiceClient->translateText(
58-
$contents,
59-
$targetLanguage,
60-
$formattedParent,
61-
[
62-
'sourceLanguageCode' => $sourceLanguage,
63-
'glossaryConfig' => $glossaryConfig,
64-
'mimeType' => $mimeType
65-
]
66-
);
58+
$request = (new TranslateTextRequest())
59+
->setContents($contents)
60+
->setTargetLanguageCode($targetLanguage)
61+
->setParent($formattedParent)
62+
->setSourceLanguageCode($sourceLanguage)
63+
->setGlossaryConfig($glossaryConfig)
64+
->setMimeType($mimeType);
65+
$response = $translationServiceClient->translateText($request);
6766
// Display the translation for each input text provided
6867
foreach ($response->getGlossaryTranslations() as $translation) {
6968
printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());

0 commit comments

Comments
 (0)