|
| 1 | +# 3.0.0 Migration Guide |
| 2 | + |
| 3 | +The 3.0 release of the `google-cloud-translate` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage. |
| 4 | + |
| 5 | +If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-translate/issues). |
| 6 | + |
| 7 | +## Supported Python Versions |
| 8 | + |
| 9 | +> **WARNING**: Breaking change |
| 10 | +
|
| 11 | +The 3.0.0 release requires Python 3.6+. |
| 12 | + |
| 13 | + |
| 14 | +## Method Calls |
| 15 | + |
| 16 | +> **WARNING**: Breaking change |
| 17 | +
|
| 18 | +Methods expect request objects. We provide a script that will convert most common use cases. |
| 19 | + |
| 20 | +* Install the library |
| 21 | + |
| 22 | +```py |
| 23 | +python3 -m pip install google-cloud-translate |
| 24 | +``` |
| 25 | + |
| 26 | +* The script `fixup_translation_{version}_keywords.py` is shipped with the library. It expects |
| 27 | +an input directory (with the code to convert) and an empty destination directory. |
| 28 | + |
| 29 | +```sh |
| 30 | +$ fixup_translation_v3_keywords.py --input-directory .samples/ --output-directory samples/ |
| 31 | +``` |
| 32 | + |
| 33 | +**Before:** |
| 34 | +```py |
| 35 | +from google.cloud import translate |
| 36 | + |
| 37 | +client = translate.TranslationServiceClient() |
| 38 | +parent = client.location_path("<PROJECT_ID>", "<LOCATION>") |
| 39 | +text = "Good morning!" |
| 40 | + |
| 41 | +response = client.translate_text( |
| 42 | + parent=parent, |
| 43 | + contents=[text], |
| 44 | + mime_type="text/plain", |
| 45 | + source_language_code="en-US", |
| 46 | + target_language_code="fr", |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +**After:** |
| 52 | +```py |
| 53 | +from google.cloud import translate |
| 54 | + |
| 55 | +client = translate.TranslationServiceClient() |
| 56 | +parent = "projects/<PROJECT_ID>/locations/<LOCATION>" |
| 57 | +text = "Good morning!" |
| 58 | + |
| 59 | +response = client.translate_text( |
| 60 | + request={ |
| 61 | + "parent": parent, |
| 62 | + "contents": [text], |
| 63 | + "mime_type": "text/plain", |
| 64 | + "source_language_code": "en-US", |
| 65 | + "target_language_code": "fr" |
| 66 | + } |
| 67 | +) |
| 68 | +``` |
| 69 | + |
| 70 | +### More Details |
| 71 | + |
| 72 | +In `google-cloud-translate<3.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters. |
| 73 | + |
| 74 | +**Before:** |
| 75 | +```py |
| 76 | + def translate_text( |
| 77 | + self, |
| 78 | + contents, |
| 79 | + target_language_code, |
| 80 | + parent, |
| 81 | + mime_type=None, |
| 82 | + source_language_code=None, |
| 83 | + model=None, |
| 84 | + glossary_config=None, |
| 85 | + labels=None, |
| 86 | + retry=google.api_core.gapic_v1.method.DEFAULT, |
| 87 | + timeout=google.api_core.gapic_v1.method.DEFAULT, |
| 88 | + metadata=None, |
| 89 | + ): |
| 90 | +``` |
| 91 | + |
| 92 | +In the 3.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional. |
| 93 | + |
| 94 | +Some methods have additional keyword only parameters. The available parameters depend on the [`google.api.method_signature` annotation](https://github.com/googleapis/googleapis/blob/master/google/cloud/translate/v3/translation_service.proto#L55) specified by the API producer. |
| 95 | + |
| 96 | + |
| 97 | +**After:** |
| 98 | +```py |
| 99 | + def translate_text( |
| 100 | + self, |
| 101 | + request: translation_service.TranslateTextRequest = None, |
| 102 | + *, |
| 103 | + parent: str = None, |
| 104 | + target_language_code: str = None, |
| 105 | + contents: Sequence[str] = None, |
| 106 | + model: str = None, |
| 107 | + mime_type: str = None, |
| 108 | + source_language_code: str = None, |
| 109 | + retry: retries.Retry = gapic_v1.method.DEFAULT, |
| 110 | + timeout: float = None, |
| 111 | + metadata: Sequence[Tuple[str, str]] = (), |
| 112 | + ) -> translation_service.TranslateTextResponse: |
| 113 | +``` |
| 114 | + |
| 115 | +> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive. |
| 116 | +> Passing both will result in an error. |
| 117 | +
|
| 118 | + |
| 119 | +Both of these calls are valid: |
| 120 | + |
| 121 | +```py |
| 122 | +response = client.translate_text( |
| 123 | + request={ |
| 124 | + "parent": parent, |
| 125 | + "target_language_code": target_language_code, |
| 126 | + "contents": contents |
| 127 | + } |
| 128 | +) |
| 129 | +``` |
| 130 | + |
| 131 | +```py |
| 132 | +response = client.translate_text( |
| 133 | + parent=parent, |
| 134 | + target_language_code=target_language_code, |
| 135 | + contents=contents |
| 136 | +) |
| 137 | +``` |
| 138 | + |
| 139 | +This call is invalid because it mixes `request` with a keyword argument `target_language_code`. Executing this code |
| 140 | +will result in an error. |
| 141 | + |
| 142 | +```py |
| 143 | +response = client.translate_text( |
| 144 | + request={ |
| 145 | + "parent": parent, |
| 146 | + "contents": contents, |
| 147 | + }, |
| 148 | + target_language_code=target_language_code |
| 149 | +) |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +## Enums and types |
| 155 | + |
| 156 | + |
| 157 | +> **WARNING**: Breaking change |
| 158 | +
|
| 159 | +The submodule `enums` (containing enum classes for long running operation State) has been removed. |
| 160 | + |
| 161 | +The submodule `types` is still present. When using the primary version module alias (`translate`) |
| 162 | +it is possible to access the types classes directly. |
| 163 | + |
| 164 | +```py |
| 165 | +from google.cloud import translate # the primary version is imported by default |
| 166 | + |
| 167 | +client = translate.TranslationServiceClient() |
| 168 | + |
| 169 | +glossary_config = client.TranslateTextGlossaryConfig( |
| 170 | + glossary=glossary_path |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +When a specific version is imported, the full module name must be specified to access types classes. |
| 175 | + |
| 176 | +```py |
| 177 | +from google.cloud import translate_v3beta1 as translate |
| 178 | + |
| 179 | +client = translate.TranslationServiceClient() |
| 180 | + |
| 181 | +glossary_config = client.types.TranslateTextGlossaryConfig( |
| 182 | + glossary=glossary_path |
| 183 | +) |
| 184 | +``` |
0 commit comments