I'm using Python 3.6 and google-cloud-translate package.
from google.cloud import translate_v3 as translate
client = translate.TranslationServiceClient(credentials = credentials)
parent = client.location_path("my-location", "global")
Since yesterday, having updated the libraries I get this error:
AttributeError: 'TranslationServiceClient' object has no attribute 'location_path'
Is it that these libraries have changed? What is the right way to channel this inquiry?
Also encountered this. Not all the documentation has been updated yet, but they have published a migration guide:
https://googleapis.dev/python/translation/latest/UPGRADING.html
You could replace parent
with "projects/<PROJECT_ID>/locations/<LOCATION>"
or define
def location_path(project_id, location):
# might as well use an f-string, the new library supports python >=3.6
return f"projects/{project_id}/locations/{location}"
and change client.location_path
to location_path
if this is something you use in many locations.
There are more sweeping changes, too. They now prefer you to pass a dictionary called request
to the API methods, although the old way is still accepted. Thus, your code might look like this:
from google.cloud import translate_v3 as translate
client = translate.TranslationServiceClient(credentials=credentials)
response = client.translate_text(
request={
"parent": "projects/my-location/locations/global",
"target_language_code": target_language_code,
"contents": [text],
}
)
Now, you might well ask 'how will I know what to put in that request dictionary?'. It looks as though the library comes with type annotations for the dictionaries appropriate for each method: https://googleapis.dev/python/translation/latest/translate_v3/types.html
For example, I read in your comment on another answer that you have had trouble with the detect_language
method. The method signature indicates that if you use keyword arguments, content
should be a valid one, so I don't know why that fails - maybe it's a bug.
However, if instead you use a request
dictionary, that should look like this. You'll see that the keys don't appear to correspond exactly to the method signature keywords (although content
is one of them).
This code would work:
response = client.detect_language({
"parent": "projects/my-location/locations/global",
"content": "Tá Gaeilge Agam, ach tá mé i mo chonai i Sasana",
})
lang = response.languages[0].language_code
(the return type is somewhat convoluted, as you can see)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With