I have successfully created a glossary to the cloud which I can successfully list, but when trying to use it to test the translation out, I get an error:
Traceback (most recent call last):
File "C:\py\gtranstest2.py", line 57, in <module>
translate_text_with_glossary(eng,pid,gid)
File "C:\py\gtranstest2.py", line 45, in translate_text_with_glossary
response = client.translate_text(
TypeError: translate_text() got an unexpected keyword argument 'glossary_config'
My code (which is based on their example code provided here: https://cloud.google.com/translate/docs/advanced/glossary#v3):
from google.cloud import translate_v3
eng = "H1 High beam, H1 Low beam (included)"
pid = "[HIDDEN]"
def translate_text_with_glossary(
text,
project_id,
):
"""Translates a given text using a glossary."""
client = translate_v3.TranslationServiceClient()
parent = 'projects/[HIDDEN]/locations/us-central1'
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary="projects/[HIDDEN]/locations/us-central1/glossaries/kittglossaryv2")
# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.translate_text(
contents=[text],
target_language_code="en",
source_language_code="hu",
parent=parent,
glossary_config=glossary_config,
)
print("Translated text: \n")
for translation in response.glossary_translations:
# print(u"\t {}".format(translation.translated_text))
return translation.translated_text
translate_text_with_glossary(eng,pid)
glossary_config should be the correct argument so I don't understand the error at all. I would appreciate any help
Let me copy user Paddy Alton's answer from another question that helped me partially solve my issue:
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)
Now after changing the code like this:
response = client.translate_text(
request={
"contents": [text],
"source_language_code": "en",
"target_language_code": "hu",
"parent": parent,
"glossary_config": glossary_config,
}
)
I get no glossary_config error and the code returns a translation. My only problem is now that the result translation I get doesn't seem to use my provided glossary even though it says it does. But this probably should be another thread.