我正在使用 Google Translate API 来翻译文档,我正在使用他们的 API,天哪,它坏了吗?
我正在使用此链接创建一个可以翻译给定输入文本的函数,
对于此代码块,我收到以下错误
# Imports the Google Cloud Translation library
from google.cloud import translate
# Initialize Translation client
def translate_text(
text: str = "YOUR_TEXT_TO_TRANSLATE", project_id: str = "YOUR_PROJECT_ID"
) -> translate.TranslationServiceClient:
"""Translating Text."""
client = translate.TranslationServiceClient()
location = "global"
parent = f"projects/{project_id}/locations/{location}"
# Translate text from English to French
# Detail on supported types can be found here:
# https://cloud.google.com/translate/docs/supported-formats
response = client.translate_text(
request={
"parent": parent,
"contents": [text],
"mime_type": "text/plain", # mime types: text/plain, text/html
"source_language_code": "en-US",
"target_language_code": "fr",
}
)
# Display the translation for each input text provided
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
return response
我收到此错误,但我不确定发生了什么
TypeError: TranslationServiceClient.translate_text() got an unexpected keyword
argument 'request'
糟糕的文档,你可以按照
translate_text()
的定义来看看它现在应该如何工作(假设 used 是一个参数 request
),但这仍然很糟糕。为什么谷歌不能提供工作文档?修复最基本的快速入门文档有多难? https://cloud.google.com/translate/docs/advanced/translate-text-advance
request = {
"parent": parent,
"contents": [text],
"mime_type": "text/plain", # mime types: text/plain, text/html
"source_language_code": "en-US",
"target_language_code": "fr",
}
response = client.translate_text(**request)