Google Translate API TypeError:TranslationServiceClient.translate_text() 收到意外的关键字参数“request”

问题描述 投票:0回答:1

我正在使用 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'
python-3.x google-cloud-platform google-translate google-translation-api
1个回答
0
投票

糟糕的文档,你可以按照

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)
© www.soinside.com 2019 - 2024. All rights reserved.