michael-mammut
michael-mammut

Reputation: 2783

How to translate a long text with google cloud translate v3

Enviroment: API: Google Cloud Translate V3, Text Size: 12 000 words

It's possible to translate words and short sentences by the translateText() method, but when I run the whole text, I run into a 'Text is too long' error.

"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"
                }
            ]
        }
    ]
}

Upvotes: 2

Views: 2138

Answers (1)

Mirous
Mirous

Reputation: 463

U can either divide the text into multiple parts (multiple requests) with max 30k codepoints within contents[] as stated in the API: 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."]
}

Or use batch translate which translates a large volume of text in asynchronous batch mode: https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/batchTranslateText. This one is a little more complicated, because you have to upload your files into Google Cloud Storage.

Upvotes: 2

Related Questions