Skip to content

Commit 5a20734

Browse files
authored
chore: update all translate samples to new format (GoogleCloudPlatform#1719)
1 parent 576286e commit 5a20734

21 files changed

+825
-708
lines changed

translate/src/detect_language.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,23 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s TEXT\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\Translate;
3125

3226
// [START translate_detect_language]
3327
use Google\Cloud\Translate\TranslateClient;
3428

35-
/** Uncomment and populate these variables in your code */
36-
// $text = 'The text whose language to detect. This will be detected as en.';
37-
38-
$translate = new TranslateClient();
39-
$result = $translate->detectLanguage($text);
40-
print("Language code: $result[languageCode]\n");
41-
print("Confidence: $result[confidence]\n");
29+
/**
30+
* @param string $text The text whose language to detect. This will be detected as en.
31+
*/
32+
function detect_language(string $text): void
33+
{
34+
$translate = new TranslateClient();
35+
$result = $translate->detectLanguage($text);
36+
print("Language code: $result[languageCode]\n");
37+
print("Confidence: $result[confidence]\n");
38+
}
4239
// [END translate_detect_language]
40+
41+
// The following 2 lines are only needed to run the samples
42+
require_once __DIR__ . '/../../testing/sample_helpers.php';
43+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

translate/src/list_codes.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 1) {
28-
return printf("Usage: php %s\n", __FILE__);
29-
}
24+
namespace Google\Cloud\Samples\Translate;
3025

3126
// [START translate_list_codes]
3227
use Google\Cloud\Translate\TranslateClient;
3328

34-
$translate = new TranslateClient();
35-
foreach ($translate->languages() as $code) {
36-
print("$code\n");
29+
function list_codes(): void
30+
{
31+
$translate = new TranslateClient();
32+
foreach ($translate->languages() as $code) {
33+
print("$code\n");
34+
}
3735
}
3836
// [END translate_list_codes]
37+
38+
// The following 2 lines are only needed to run the samples
39+
require_once __DIR__ . '/../../testing/sample_helpers.php';
40+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

translate/src/list_languages.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) > 2) {
28-
return printf("Usage: php %s [TARGET_LANGUAGE]\n", __FILE__);
29-
}
30-
$targetLanguage = isset($argv[1]) ? $argv[1] : 'en';
24+
namespace Google\Cloud\Samples\Translate;
3125

3226
// [START translate_list_language_names]
3327
use Google\Cloud\Translate\TranslateClient;
3428

35-
/** Uncomment and populate these variables in your code */
36-
// $targetLanguage = 'en'; // Language to print the language names in
37-
38-
$translate = new TranslateClient();
39-
$result = $translate->localizedLanguages([
40-
'target' => $targetLanguage,
41-
]);
42-
foreach ($result as $lang) {
43-
printf('%s: %s' . PHP_EOL, $lang['code'], $lang['name']);
29+
/**
30+
* @param string $targetLanguage Language to print the language names in
31+
*/
32+
function list_languages(string $targetLanguage = 'en'): void
33+
{
34+
$translate = new TranslateClient();
35+
$result = $translate->localizedLanguages([
36+
'target' => $targetLanguage,
37+
]);
38+
foreach ($result as $lang) {
39+
printf('%s: %s' . PHP_EOL, $lang['code'], $lang['name']);
40+
}
4441
}
4542
// [END translate_list_language_names]
43+
44+
// The following 2 lines are only needed to run the samples
45+
require_once __DIR__ . '/../../testing/sample_helpers.php';
46+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

translate/src/translate.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) < 2 || count($argv) > 3) {
28-
return printf("Usage: php %s TEXT [TARGET_LANGUAGE]\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
31-
$targetLanguage = isset($argv[2]) ? $argv[2] : 'en';
24+
namespace Google\Cloud\Samples\Translate;
3225

3326
// [START translate_translate_text]
3427
use Google\Cloud\Translate\TranslateClient;
3528

36-
/** Uncomment and populate these variables in your code */
37-
// $text = 'The text to translate.';
38-
// $targetLanguage = 'ja'; // Language to translate to
29+
/**
30+
* @param string $text The text to translate.
31+
* @param string $targetLanguage Language to translate to.
32+
*/
33+
function translate(string $text, string $targetLanguage): void
34+
{
35+
/** Uncomment and populate these variables in your code */
36+
// $text = 'The text to translate.';
37+
// $targetLanguage = 'ja'; // Language to translate to
3938

40-
$translate = new TranslateClient();
41-
$result = $translate->translate($text, [
42-
'target' => $targetLanguage,
43-
]);
44-
print("Source language: $result[source]\n");
45-
print("Translation: $result[text]\n");
39+
$translate = new TranslateClient();
40+
$result = $translate->translate($text, [
41+
'target' => $targetLanguage,
42+
]);
43+
print("Source language: $result[source]\n");
44+
print("Translation: $result[text]\n");
45+
}
4646
// [END translate_translate_text]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

translate/src/translate_with_model.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) < 2 || count($argv) > 3) {
28-
return printf("Usage: php %s TEXT [TARGET_LANGUAGE]\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
31-
$targetLanguage = isset($argv[2]) ? $argv[2] : 'en';
24+
namespace Google\Cloud\Samples\Translate;
3225

3326
// [START translate_text_with_model]
3427
use Google\Cloud\Translate\TranslateClient;
3528

36-
/** Uncomment and populate these variables in your code */
37-
// $text = 'The text to translate.'
38-
// $targetLanguage = 'ja'; // Language to translate to
39-
40-
$model = 'nmt'; // "base" for standard edition, "nmt" for premium
41-
$translate = new TranslateClient();
42-
$result = $translate->translate($text, [
43-
'target' => $targetLanguage,
44-
'model' => $model,
45-
]);
46-
print("Source language: $result[source]\n");
47-
print("Translation: $result[text]\n");
48-
print("Model: $result[model]\n");
29+
/**
30+
* @param string $text The text to translate.
31+
* @param string $targetLanguage Language to translate to.
32+
*/
33+
function translate_with_model(string $text, string $targetLanguage): void
34+
{
35+
$model = 'nmt'; // "base" for standard edition, "nmt" for premium
36+
$translate = new TranslateClient();
37+
$result = $translate->translate($text, [
38+
'target' => $targetLanguage,
39+
'model' => $model,
40+
]);
41+
print("Source language: $result[source]\n");
42+
print("Translation: $result[text]\n");
43+
print("Model: $result[model]\n");
44+
}
4945
// [END translate_text_with_model]
46+
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

translate/src/v3_batch_translate_text.php

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
require_once __DIR__ . '/../vendor/autoload.php';
19-
20-
if (count($argv) < 7 || count($argv) > 7) {
21-
return printf("Usage: php %s INPUT_URI OUTPUT_URI PROJECT_ID LOCATION SOURCE_LANGUAGE TARGET_LANGUAGE\n", __FILE__);
22-
}
23-
list($_, $inputUri, $outputUri, $projectId, $location, $sourceLang, $targetLang) = $argv;
18+
namespace Google\Cloud\Samples\Translate;
2419

2520
// [START translate_v3_batch_translate_text]
2621
use Google\Cloud\Translate\V3\GcsDestination;
@@ -29,49 +24,63 @@
2924
use Google\Cloud\Translate\V3\OutputConfig;
3025
use Google\Cloud\Translate\V3\TranslationServiceClient;
3126

32-
$translationServiceClient = new TranslationServiceClient();
27+
/**
28+
* @param string $inputUri Path to to source input (e.g. "gs://cloud-samples-data/text.txt").
29+
* @param string $outputUri Path to store results (e.g. "gs://YOUR_BUCKET_ID/results/").
30+
* @param string $projectId Your Google Cloud project ID.
31+
* @param string $location Project location (e.g. us-central1)
32+
* @param string $targetLanguage Language to translate to.
33+
* @param string $sourceLanguage Language of the source.
34+
*/
35+
function v3_batch_translate_text(
36+
string $inputUri,
37+
string $outputUri,
38+
string $projectId,
39+
string $location,
40+
string $targetLanguage,
41+
string $sourceLanguage
42+
): void {
43+
$translationServiceClient = new TranslationServiceClient();
3344

34-
/** Uncomment and populate these variables in your code */
35-
// $inputUri = 'gs://cloud-samples-data/text.txt';
36-
// $outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
37-
// $projectId = '[Google Cloud Project ID]';
38-
// $location = 'us-central1';
39-
// $sourceLang = 'en';
40-
// $targetLang = 'ja';
41-
$targetLanguageCodes = [$targetLang];
42-
$gcsSource = (new GcsSource())
43-
->setInputUri($inputUri);
45+
$targetLanguageCodes = [$targetLanguage];
46+
$gcsSource = (new GcsSource())
47+
->setInputUri($inputUri);
4448

45-
// Optional. Can be "text/plain" or "text/html".
46-
$mimeType = 'text/plain';
47-
$inputConfigsElement = (new InputConfig())
48-
->setGcsSource($gcsSource)
49-
->setMimeType($mimeType);
50-
$inputConfigs = [$inputConfigsElement];
51-
$gcsDestination = (new GcsDestination())
52-
->setOutputUriPrefix($outputUri);
53-
$outputConfig = (new OutputConfig())
54-
->setGcsDestination($gcsDestination);
55-
$formattedParent = $translationServiceClient->locationName($projectId, $location);
49+
// Optional. Can be "text/plain" or "text/html".
50+
$mimeType = 'text/plain';
51+
$inputConfigsElement = (new InputConfig())
52+
->setGcsSource($gcsSource)
53+
->setMimeType($mimeType);
54+
$inputConfigs = [$inputConfigsElement];
55+
$gcsDestination = (new GcsDestination())
56+
->setOutputUriPrefix($outputUri);
57+
$outputConfig = (new OutputConfig())
58+
->setGcsDestination($gcsDestination);
59+
$formattedParent = $translationServiceClient->locationName($projectId, $location);
5660

57-
try {
58-
$operationResponse = $translationServiceClient->batchTranslateText(
59-
$formattedParent,
60-
$sourceLang,
61-
$targetLanguageCodes,
62-
$inputConfigs,
63-
$outputConfig
64-
);
65-
$operationResponse->pollUntilComplete();
66-
if ($operationResponse->operationSucceeded()) {
67-
$response = $operationResponse->getResult();
68-
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
69-
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
70-
} else {
71-
$error = $operationResponse->getError();
72-
print($error->getMessage());
61+
try {
62+
$operationResponse = $translationServiceClient->batchTranslateText(
63+
$formattedParent,
64+
$sourceLanguage,
65+
$targetLanguageCodes,
66+
$inputConfigs,
67+
$outputConfig
68+
);
69+
$operationResponse->pollUntilComplete();
70+
if ($operationResponse->operationSucceeded()) {
71+
$response = $operationResponse->getResult();
72+
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
73+
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
74+
} else {
75+
$error = $operationResponse->getError();
76+
print($error->getMessage());
77+
}
78+
} finally {
79+
$translationServiceClient->close();
7380
}
74-
} finally {
75-
$translationServiceClient->close();
7681
}
7782
// [END translate_v3_batch_translate_text]
83+
84+
// The following 2 lines are only needed to run the samples
85+
require_once __DIR__ . '/../../testing/sample_helpers.php';
86+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)