环境:API:谷歌云翻译V3,文本大小:12000字
translateText()
方法可以翻译单词和短句,但当我运行全文时,我会遇到"文本太长"的错误。
"message": "Text is too long.",
"code": 3,
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": 0,
"data": "type.googleapis.com/google.rpc.BadRequest"
},
{
"@type": 0,
"data": [
{
"field": "contents",
"description": "The total codepoints in the request must be less than 30720, actual: 90005"
}
]
}
]
}
U可以将文本划分为多个部分(多个请求(,如API中所述,contents[]
内的最大码点为30k:https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/translateText
// Request 1
{
"sourceLanguageCode": "en",
"targetLanguageCode": "de",
"contents": ["Text part one..."]
}
// Request 2
{
"sourceLanguageCode": "en",
"targetLanguageCode": "de",
"contents": ["...text part two..."]
}
// Request n
{
"sourceLanguageCode": "en",
"targetLanguageCode": "de",
"contents": ["...text part n."]
}
或者使用在异步批处理模式下翻译大量文本的批处理翻译:https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/batchTranslateText.这个有点复杂,因为你必须将文件上传到谷歌云存储。