Skip to content

Commit 0ee07a7

Browse files
sirtorrybshaffer
authored andcommitted
Translation v3 samples (GoogleCloudPlatform#982)
1 parent 2b9ed12 commit 0ee07a7

17 files changed

+1352
-1
lines changed

translate/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "google/translate-sample",
33
"type": "project",
44
"require": {
5-
"google/cloud-translate": "^1.1"
5+
"google/cloud-translate": "^1.5"
6+
},
7+
"require-dev": {
8+
"google/cloud-storage": "^1.14"
69
}
710
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
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;
24+
25+
// [START translate_v3_batch_translate_text]
26+
use Google\Cloud\Translate\V3\GcsDestination;
27+
use Google\Cloud\Translate\V3\GcsSource;
28+
use Google\Cloud\Translate\V3\InputConfig;
29+
use Google\Cloud\Translate\V3\OutputConfig;
30+
use Google\Cloud\Translate\V3\TranslationServiceClient;
31+
32+
$translationServiceClient = new TranslationServiceClient();
33+
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);
44+
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);
56+
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());
73+
}
74+
} finally {
75+
$translationServiceClient->close();
76+
}
77+
// [END translate_v3_batch_translate_text]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once __DIR__ . '/../vendor/autoload.php';
19+
20+
if (count($argv) < 8 || count($argv) > 8) {
21+
return printf("Usage: php %s INPUT_URI OUTPUT_URI PROJECT_ID LOCATION GLOSSARY_ID TARGET_LANGUAGE SOURCE_LANGUAGE\n", __FILE__);
22+
}
23+
list($_, $inputUri, $outputUri, $projectId, $location, $glossaryId, $targetLanguage, $sourceLanguage) = $argv;
24+
25+
// [START translate_v3_batch_translate_text_with_glossary]
26+
use Google\Cloud\Translate\V3\GcsDestination;
27+
use Google\Cloud\Translate\V3\GcsSource;
28+
use Google\Cloud\Translate\V3\InputConfig;
29+
use Google\Cloud\Translate\V3\OutputConfig;
30+
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
31+
use Google\Cloud\Translate\V3\TranslationServiceClient;
32+
33+
$translationServiceClient = new TranslationServiceClient();
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $inputUri = 'gs://cloud-samples-data/text.txt';
37+
// $outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
38+
// $projectId = '[Google Cloud Project ID]';
39+
// $location = 'us-central1';
40+
// $glossaryId = '[YOUR_GLOSSARY_ID]';
41+
// $targetLanguage = 'en';
42+
// $sourceLanguage = 'de';
43+
$glossaryPath = $translationServiceClient->glossaryName(
44+
$projectId,
45+
$location,
46+
$glossaryId
47+
);
48+
$targetLanguageCodes = [$targetLanguage];
49+
$gcsSource = (new GcsSource())
50+
->setInputUri($inputUri);
51+
52+
// Optional. Can be "text/plain" or "text/html".
53+
$mimeType = 'text/plain';
54+
$inputConfigsElement = (new InputConfig())
55+
->setGcsSource($gcsSource)
56+
->setMimeType($mimeType);
57+
$inputConfigs = [$inputConfigsElement];
58+
$gcsDestination = (new GcsDestination())
59+
->setOutputUriPrefix($outputUri);
60+
$outputConfig = (new OutputConfig())
61+
->setGcsDestination($gcsDestination);
62+
$formattedParent = $translationServiceClient->locationName(
63+
$projectId,
64+
$location
65+
);
66+
$glossariesItem = (new TranslateTextGlossaryConfig())
67+
->setGlossary($glossaryPath);
68+
$glossaries = ['ja' => $glossariesItem];
69+
70+
try {
71+
$operationResponse = $translationServiceClient->batchTranslateText(
72+
$formattedParent,
73+
$sourceLanguage,
74+
$targetLanguageCodes,
75+
$inputConfigs,
76+
$outputConfig,
77+
['glossaries' => $glossaries]
78+
);
79+
$operationResponse->pollUntilComplete();
80+
if ($operationResponse->operationSucceeded()) {
81+
$response = $operationResponse->getResult();
82+
// Display the translation for each input text provided
83+
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
84+
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
85+
} else {
86+
$error = $operationResponse->getError();
87+
print($error->getMessage());
88+
}
89+
} finally {
90+
$translationServiceClient->close();
91+
}
92+
// [END translate_v3_batch_translate_text_with_glossary]
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once __DIR__ . '/../vendor/autoload.php';
19+
20+
if (count($argv) < 9 || count($argv) > 9) {
21+
return printf("Usage: php %s INPUT_URI OUTPUT_URI PROJECT_ID LOCATION TARGET_LANGUAGE SOURCE_LANGUAGE MODEL_ID GLOSSARY_ID\n", __FILE__);
22+
}
23+
list($_, $inputUri, $outputUri, $projectId, $location, $targetLanguage, $sourceLanguage, $modelId, $glossaryId) = $argv;
24+
25+
// [START translate_v3_batch_translate_text_with_glossary_and_model]
26+
use Google\Cloud\Translate\V3\GcsDestination;
27+
use Google\Cloud\Translate\V3\GcsSource;
28+
use Google\Cloud\Translate\V3\InputConfig;
29+
use Google\Cloud\Translate\V3\OutputConfig;
30+
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
31+
use Google\Cloud\Translate\V3\TranslationServiceClient;
32+
33+
$translationServiceClient = new TranslationServiceClient();
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $inputUri = 'gs://cloud-samples-data/text.txt';
37+
// $outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
38+
// $projectId = '[Google Cloud Project ID]';
39+
// $location = 'us-central1';
40+
// $targetLanguage = 'en';
41+
// $sourceLanguage = 'de';
42+
// $modelId = '{your-model-id}';
43+
// $glossaryId = '[YOUR_GLOSSARY_ID]';
44+
$glossaryPath = $translationServiceClient->glossaryName(
45+
$projectId,
46+
$location,
47+
$glossaryId
48+
);
49+
$modelPath = sprintf(
50+
'projects/%s/locations/%s/models/%s',
51+
$projectId,
52+
$location,
53+
$modelId
54+
);
55+
$targetLanguageCodes = [$targetLanguage];
56+
$gcsSource = (new GcsSource())
57+
->setInputUri($inputUri);
58+
59+
// Optional. Can be "text/plain" or "text/html".
60+
$mimeType = 'text/plain';
61+
$inputConfigsElement = (new InputConfig())
62+
->setGcsSource($gcsSource)
63+
->setMimeType($mimeType);
64+
$inputConfigs = [$inputConfigsElement];
65+
$gcsDestination = (new GcsDestination())
66+
->setOutputUriPrefix($outputUri);
67+
$outputConfig = (new OutputConfig())
68+
->setGcsDestination($gcsDestination);
69+
$formattedParent = $translationServiceClient->locationName($projectId, $location);
70+
$models = ['ja' => $modelPath];
71+
$glossariesItem = (new TranslateTextGlossaryConfig())
72+
->setGlossary($glossaryPath);
73+
$glossaries = ['ja' => $glossariesItem];
74+
75+
try {
76+
$operationResponse = $translationServiceClient->batchTranslateText(
77+
$formattedParent,
78+
$sourceLanguage,
79+
$targetLanguageCodes,
80+
$inputConfigs,
81+
$outputConfig,
82+
[
83+
'models' => $models,
84+
'glossaries' => $glossaries
85+
]
86+
);
87+
$operationResponse->pollUntilComplete();
88+
if ($operationResponse->operationSucceeded()) {
89+
$response = $operationResponse->getResult();
90+
// Display the translation for each input text provided
91+
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
92+
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
93+
} else {
94+
$error = $operationResponse->getError();
95+
print($error->getMessage());
96+
}
97+
} finally {
98+
$translationServiceClient->close();
99+
}
100+
// [END translate_v3_batch_translate_text_with_glossary_and_model]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once __DIR__ . '/../vendor/autoload.php';
19+
20+
if (count($argv) < 8 || count($argv) > 8) {
21+
return printf("Usage: php %s INPUT_URI OUTPUT_URI PROJECT_ID LOCATION TARGET_LANGUAGE SOURCE_LANGUAGE MODEL_ID \n", __FILE__);
22+
}
23+
list($_, $inputUri, $outputUri, $projectId, $location, $targetLanguage, $sourceLanguage, $modelId) = $argv;
24+
25+
// [START translate_v3_batch_translate_text_with_model]
26+
use Google\Cloud\Translate\V3\GcsDestination;
27+
use Google\Cloud\Translate\V3\GcsSource;
28+
use Google\Cloud\Translate\V3\InputConfig;
29+
use Google\Cloud\Translate\V3\OutputConfig;
30+
use Google\Cloud\Translate\V3\TranslationServiceClient;
31+
32+
$translationServiceClient = new TranslationServiceClient();
33+
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+
// $targetLanguage = 'en';
40+
// $sourceLanguage = 'de';
41+
// $modelId = '{your-model-id}';
42+
$modelPath = sprintf(
43+
'projects/%s/locations/%s/models/%s',
44+
$projectId,
45+
$location,
46+
$modelId
47+
);
48+
$targetLanguageCodes = [$targetLanguage];
49+
$gcsSource = (new GcsSource())
50+
->setInputUri($inputUri);
51+
52+
// Optional. Can be "text/plain" or "text/html".
53+
$mimeType = 'text/plain';
54+
$inputConfigsElement = (new InputConfig())
55+
->setGcsSource($gcsSource)
56+
->setMimeType($mimeType);
57+
$inputConfigs = [$inputConfigsElement];
58+
$gcsDestination = (new GcsDestination())
59+
->setOutputUriPrefix($outputUri);
60+
$outputConfig = (new OutputConfig())
61+
->setGcsDestination($gcsDestination);
62+
$formattedParent = $translationServiceClient->locationName($projectId, $location);
63+
$models = ['ja' => $modelPath];
64+
65+
try {
66+
$operationResponse = $translationServiceClient->batchTranslateText(
67+
$formattedParent,
68+
$sourceLanguage,
69+
$targetLanguageCodes,
70+
$inputConfigs,
71+
$outputConfig,
72+
['models' => $models]
73+
);
74+
$operationResponse->pollUntilComplete();
75+
if ($operationResponse->operationSucceeded()) {
76+
$response = $operationResponse->getResult();
77+
// Display the translation for each input text provided
78+
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
79+
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
80+
} else {
81+
$error = $operationResponse->getError();
82+
print($error->getMessage());
83+
}
84+
} finally {
85+
$translationServiceClient->close();
86+
}
87+
// [END translate_v3_batch_translate_text_with_model]

0 commit comments

Comments
 (0)