Skip to content

Commit 03beb83

Browse files
authored
docs(Translate): add MIGRATING.md and update README.md (googleapis#9235)
1 parent be57a83 commit 03beb83

2 files changed

Lines changed: 127 additions & 11 deletions

File tree

Translate/MIGRATING.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Migration Guide
2+
3+
This migration guide shows how to migrate `google/cloud-translate` to v2.
4+
5+
**NOTE**: The package version (e.g. `google/cloud-translate:^2.0`) is distinct
6+
from the Translate API version `V3`, which this package calls.
7+
8+
## Client Initialization
9+
10+
The `TranslateClient` has been replaced by `TranslationServiceClient`.
11+
12+
**Before**
13+
```php
14+
use Google\Cloud\Translate\TranslateClient;
15+
16+
$translate = new TranslateClient();
17+
```
18+
19+
**After**
20+
```php
21+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
22+
23+
$translate = new TranslationServiceClient();
24+
```
25+
26+
## Translate Text
27+
28+
The `translate()` method has been replaced by `translateText()`. The arguments have also changed to accept a request object.
29+
30+
### Single Translation
31+
32+
**Before**
33+
```php
34+
use Google\Cloud\Translate\TranslateClient;
35+
36+
$translate = new TranslateClient([
37+
'projectId' => 'my-project'
38+
]);
39+
40+
$result = $translate->translate('Hello, world!', [
41+
'target' => 'fr'
42+
]);
43+
44+
echo $result['text'];
45+
```
46+
47+
**After**
48+
```php
49+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
50+
use Google\Cloud\Translate\V3\TranslateTextRequest;
51+
52+
$translate = new TranslationServiceClient();
53+
54+
$projectId = 'my-project';
55+
$location = 'global';
56+
$parent = $translate->locationName($projectId, $location);
57+
58+
$request = (new TranslateTextRequest())
59+
->setContents(['Hello, world!'])
60+
->setTargetLanguageCode('fr')
61+
->setParent($parent);
62+
63+
$response = $translate->translateText($request);
64+
65+
echo $response->getTranslations()[0]->getTranslatedText();
66+
```
67+
68+
### Batch Translation
69+
70+
**Before**
71+
```php
72+
use Google\Cloud\Translate\TranslateClient;
73+
74+
$translate = new TranslateClient([
75+
'projectId' => 'my-project'
76+
]);
77+
78+
$results = $translate->translateBatch([
79+
'Hello, world!',
80+
'My name is Jeff.'
81+
], [
82+
'target' => 'fr'
83+
]);
84+
85+
foreach ($results as $result) {
86+
echo $result['text'] . "
87+
";
88+
}
89+
```
90+
91+
**After**
92+
```php
93+
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
94+
use Google\Cloud\Translate\V3\TranslateTextRequest;
95+
96+
$translate = new TranslationServiceClient();
97+
98+
$projectId = 'my-project';
99+
$location = 'global';
100+
$parent = $translate->locationName($projectId, $location);
101+
102+
$request = (new TranslateTextRequest())
103+
->setContents([
104+
'Hello, world!',
105+
'My name is Jeff.'
106+
])
107+
->setTargetLanguageCode('fr')
108+
->setParent($parent);
109+
110+
$response = $translate->translateText($request);
111+
112+
foreach ($response->getTranslations() as $translation) {
113+
echo $translation->getTranslatedText() . "
114+
";
115+
}
116+
```
117+
118+
## Formatting Project Name
119+
120+
In `v2`, you need to provide a formatted project name as the parent. You can use
121+
the `locationName` method on the client to format this.
122+
123+
```php
124+
$projectId = 'my-project';
125+
$location = 'global';
126+
$parent = $translate->locationName($projectId, $location);
127+
```

Translate/README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,6 @@ try {
5555
}
5656
```
5757

58-
### Choosing the Right Client for You
59-
60-
This component offers both a handwritten and generated client, used to access the V2 and V3 translation APIs, respectively.
61-
Both clients will receive on-going support and feature additions, however, it is worth noting the streamlined nature of
62-
the generated client means it will receive updates more frequently. Additionally, the generated client is capable of
63-
utilizing gRPC for its transport (by installing the gRPC extension) while the handwritten client interacts over
64-
REST & HTTP/1.1 only.
65-
66-
The handwritten client can be found under `Google\Cloud\Translate\TranslateClient`, whereas the generated client is
67-
found under `Google\Cloud\Translate\V3\TranslationServiceClient`.
68-
6958
### Debugging
7059

7160
Please see our [Debugging guide](https://github.com/googleapis/google-cloud-php/blob/main/DEBUG.md)

0 commit comments

Comments
 (0)