Google Translator API POST Call Request character Limit

653 views
Skip to first unread message

jayshr...@mrrsoft.com

unread,
Dec 14, 2015, 7:00:44 AM12/14/15
to Google Translate API Developer Forum
I have query regarding character limit for Post Call to Google Translator API, it is 5k characters.
Is their any way where i set it unlimited, So that i can translate whole web page at a time.

My website page containing 123504 characters on one page including html. and it can be increase as data entered.
what should i do for this case, when using Google Translator API.

I have searched for this. In one blog i have found following , in which given the solution that..Your text needs to be split into several chunks and translated separately.. Is it a right way??
Can you please suggest me some way for doing this with Google Translator API.
And if no other way instead of this (Your text needs to be split into several chunks and translated separately) then can you please suggest me how i split HTML into chunks and get translated bodies at the same position where it was.




<script type="text/javascript">
    $(document).ready(function () {
        $(document).on("change", "#ddlTranslateLanguage", function () {
            var htmlString = $("html").html();
            $.ajax({
                type: "POST",
                url: "https://www.googleapis.com/language/translate/v2",
                headers: {
                    "X-HTTP-Method-Override": "GET"
                },
                data: { q: htmlString, target: "hi", source: "en", fields: "translations", key: "AIzaSyCnqpOsgFssI3wki_KJRmwlAXTWb_UtefA" },
                success: function (response) {
                    $("html").html(response.data.translations[0]["translatedText"])
                  alert("success");
                },
                error: function (error) {
                     alert("error");
                }
            });

        });
    });
</script>

Patrice (Google Cloud Support)

unread,
Dec 14, 2015, 11:24:36 AM12/14/15
to Google Translate API Developer Forum
Hi,

With HTML, this gets really tricky, unfortunately. With the limit being what it is, you need to be able to cut your request into multiple ones. Ideally, you'd need to send one sentence at a time to keep meaning (instead of translating word per word, or cutting a sentence mid-way).

The way I would go about with HTML is that I'd probably sent each section on it's own, and translate that. Instead of trying to translate the whole page, I would go in and parse the HTML to grab what's in different sub-sections of the page, translate only that, and put it back where I "took" it. Not ideal, and may require some parsing logic to be devised on your end, but with the current limit, I see this as the only way to go.

Cheers!

Jayshree

unread,
Dec 16, 2015, 1:23:10 AM12/16/15
to Google Translate API Developer Forum
Hi Patrice,

Thank you for your response.

I have doubt that when free Google Translator can translate whole page at time with no limit of character in request.
then why do not Paid Google Translator API.

Is it not strange?

Can you please do some inquiry at you end and let me know about this. there must be any way to rid of this.

Nick

unread,
Dec 16, 2015, 7:55:01 PM12/16/15
to Google Translate API Developer Forum
Hey Jayshree,

The Translate API and the Website Translator gadget are two separate products which don't necessarily have the same billing features. There are things which the Translate API is capable of which the Website Translator does not work with as well, so being quite different, it's natural that they would have different billing features. Are you experiencing a specific issue with the Translate API? It's hard to give general advice on how to split up a web page for translation, although one piece of advice would be to translate the actual content, not the raw HTML, and to prevent splitting in the middle of a sentence, as this could affect the translation.

Jayshree

unread,
Dec 30, 2015, 5:55:06 AM12/30/15
to Google Translate API Developer Forum
Hi

I have translated web page by splitting it on nodes. now its working. but sometimes while hitting to Translate API for translation getting following error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: q",
    "locationType": "parameter",
    "location": "q"
   },
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: target",
    "locationType": "parameter",
    "location": "target"
   }
  ],
  "code": 400,
  "message": "Required parameter: q"
 }
}

can u please suggest some solution get rid of this.
The error says "Required parameter: q" and "Required parameter: target", although it has passed and works for the time also.
but sometimes gives this issue.

Jayshree

unread,
Dec 30, 2015, 6:36:37 AM12/30/15
to Google Translate API Developer Forum
forgot to mention call:


function TranslateMasterData() {
    var selectedLanguageCode = $("#languageChange").attr("data-languagecode");
    var table = $("[data-frontpagename]");
    if (table != undefined) {
        $("[data-frontpagename]").each(function (e) {
            var outputBody = "";
            var frontPageName = $(this).data("frontpagename")
            if (frontPageName != undefined && frontPageName.indexOf("data-masterdatacontainer") >= 0) {
                var id = $(this).attr("id");
                var rowCount = $("#" + id + " tbody").find("tr").length;
                var loopCount = 0;
                $("#" + id + " tbody").find("tr").each(function (e) {
                    var htmlString = $(this).html();
                    if ($(this).html().length < 5000) {
                        $.ajax({
                            type: "POST",
                            url: "https://www.googleapis.com/language/translate/v2",
                            headers: {
                                "X-HTTP-Method-Override": "GET"
                            },
                            data: { q: htmlString, target: selectedLanguageCode, source: "en", fields: "translations", key: "AIzaSyCnqpOsgFssI3wki_KJRmwlAXTWb_UtefA" },
                            success: function (response) {
                                loopCount++
                                outputBody += "<tr>" + response.data.translations[0]["translatedText"] + "</tr>";
                                if (loopCount == rowCount) {
                                    $("#" + id + " tbody").html(outputBody);
                                }
                            },
                            error: function (error) {
                                log("!!!OOPS Error Occurred  while changing language");
                            }
                        });
                    } else {

                    }
                });
            }
        });
    }
}


this is the call with i am hitting to api.

Nick

unread,
Dec 30, 2015, 5:14:19 PM12/30/15
to Google Translate API Developer Forum
In regards to the error, it's quite clear that the API is not receiving proper parameters. Have you managed to view the actual HTTP request, using wireshark or a similar program, as it leaves your computer? Or are you just trusting your code to generate the request properly? I can see that the request will contain variables only if the HTML sourcing of the data works properly, which is certainly an open question.
Reply all
Reply to author
Forward
0 new messages