Skip to content

Commit 32a3915

Browse files
authored
feat(translate): upgrade google/cloud-translate to ^2.0 (#2345)
* feat(translate): upgrade google/cloud-translate to ^2.0 * style(translate): use printf with PHP_EOL in quickstart
1 parent b26947a commit 32a3915

10 files changed

Lines changed: 27 additions & 307 deletions

‎translate/README.md‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ from PHP.
3232
(if composer is installed globally).
3333
5. **Run** with the command `php src/SNIPPET_NAME.php`. For example:
3434
```sh
35-
$ php src/list_languages.php
36-
af: Afrikaans
37-
sq: Albanian
38-
am: Amharic
35+
$ php src/v3_get_supported_languages.php YOUR_PROJECT_ID
36+
Language Code: af
37+
Language Code: sq
38+
Language Code: am
3939
...
4040

41-
$ php src/translate.php "This is my text to translate" fr
42-
Source language: en
43-
Translation: Ceci est mon texte à traduire
41+
$ php src/v3_translate_text.php "This is my text to translate" fr YOUR_PROJECT_ID
42+
Translated text: Ceci est mon texte à traduire
4443
```
4544

4645
## Contributing changes

‎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.17"
5+
"google/cloud-translate": "^2.0"
66
},
77
"require-dev": {
8-
"google/cloud-storage": "^1.36"
8+
"google/cloud-storage": "^2.0"
99
}
1010
}

‎translate/quickstart.php‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,30 @@
2020
require __DIR__ . '/vendor/autoload.php';
2121

2222
# Imports the Google Cloud client library
23-
use Google\Cloud\Translate\TranslateClient;
23+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
24+
use Google\Cloud\Translate\V3\TranslateTextRequest;
2425

2526
# Your Google Cloud Platform project ID
2627
$projectId = 'YOUR_PROJECT_ID';
2728

2829
# Instantiates a client
29-
$translate = new TranslateClient([
30-
'projectId' => $projectId
31-
]);
30+
$translate = new TranslationServiceClient();
3231

3332
# The text to translate
3433
$text = 'Hello, world!';
3534
# The target language
36-
$target = 'ru';
35+
$targetLanguage = 'ru';
3736

3837
# Translates some text into Russian
39-
$translation = $translate->translate($text, [
40-
'target' => $target
41-
]);
38+
$request = (new TranslateTextRequest())
39+
->setContents([$text])
40+
->setTargetLanguageCode($targetLanguage)
41+
->setParent(TranslationServiceClient::locationName($projectId, 'global'));
42+
$response = $translate->translateText($request);
4243

43-
echo 'Text: ' . $text . '
44-
Translation: ' . $translation['text'];
44+
foreach ($response->getTranslations() as $translation) {
45+
printf('Text: %s' . PHP_EOL, $text);
46+
printf('Translation: %s' . PHP_EOL, $translation->getTranslatedText());
47+
}
4548
# [END translate_quickstart]
46-
return $translation;
49+
return $response;

‎translate/src/detect_language.php‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎translate/src/list_codes.php‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎translate/src/list_languages.php‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎translate/src/translate.php‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎translate/src/translate_with_model.php‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎translate/test/quickstartTest.php‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
use Google\Cloud\TestUtils\TestTrait;
19+
use Google\Cloud\Translate\V3\TranslateTextResponse;
1920
use PHPUnit\Framework\TestCase;
2021

2122
class quickstartTest extends TestCase
@@ -35,16 +36,12 @@ public function testQuickstart()
3536

3637
// Invoke quickstart.php
3738
ob_start();
38-
$translation = include $file;
39+
$response = include $file;
3940
$output = ob_get_clean();
4041

4142
// Make sure it looks correct
42-
$this->assertTrue(is_array($translation));
43-
$this->assertArrayHasKey('source', $translation);
44-
$this->assertArrayHasKey('input', $translation);
45-
$this->assertArrayHasKey('text', $translation);
46-
$this->assertEquals('en', $translation['source']);
47-
$this->assertEquals('Hello, world!', $translation['input']);
48-
$this->assertStringContainsString('мир', $translation['text']);
43+
$this->assertInstanceOf(TranslateTextResponse::class, $response);
44+
$this->assertCount(1, $response->getTranslations());
45+
$this->assertStringContainsString('мир', $response->getTranslations()[0]->getTranslatedText());
4946
}
5047
}

‎translate/test/translateTest.php‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -47,57 +47,6 @@ public static function tearDownAfterClass(): void
4747
self::$bucket->delete();
4848
}
4949

50-
public function testTranslate()
51-
{
52-
$output = $this->runFunctionSnippet(
53-
'translate',
54-
['Hello.', 'ja']
55-
);
56-
$this->assertStringContainsString('Source language: en', $output);
57-
$this->assertStringContainsString('Translation:', $output);
58-
}
59-
60-
public function testTranslateBadLanguage()
61-
{
62-
$this->expectException('Google\Cloud\Core\Exception\BadRequestException');
63-
64-
$this->runFunctionSnippet('translate', ['Hello.', 'jp']);
65-
}
66-
67-
public function testTranslateWithModel()
68-
{
69-
$output = $this->runFunctionSnippet('translate_with_model', ['Hello.', 'ja']);
70-
$this->assertStringContainsString('Source language: en', $output);
71-
$this->assertStringContainsString('Translation:', $output);
72-
$this->assertStringContainsString('Model: nmt', $output);
73-
}
74-
75-
public function testDetectLanguage()
76-
{
77-
$output = $this->runFunctionSnippet('detect_language', ['Hello.']);
78-
$this->assertStringContainsString('Language code: en', $output);
79-
$this->assertStringContainsString('Confidence:', $output);
80-
}
81-
82-
public function testListCodes()
83-
{
84-
$output = $this->runFunctionSnippet('list_codes');
85-
$this->assertStringContainsString("\nen\n", $output);
86-
$this->assertStringContainsString("\nja\n", $output);
87-
}
88-
89-
public function testListLanguagesInEnglish()
90-
{
91-
$output = $this->runFunctionSnippet('list_languages', ['en']);
92-
$this->assertStringContainsString('ja: Japanese', $output);
93-
}
94-
95-
public function testListLanguagesInJapanese()
96-
{
97-
$output = $this->runFunctionSnippet('list_languages', ['ja']);
98-
$this->assertStringContainsString('en: 英語', $output);
99-
}
100-
10150
public function testV3TranslateText()
10251
{
10352
$output = $this->runFunctionSnippet(

0 commit comments

Comments
 (0)