|
| 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 | +``` |
0 commit comments