aym / google-ajax-apis

Automatically exported from code.google.com/p/google-ajax-apis
0 stars 0 forks source link

Google skipping translations #523

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
using the google translations api to translate lots of text on the fly
but having major issue when translating text that exceeds weirdly 1200 
characters. any suggestions hints or background on the translator will be great 
help

Cheers!

Original issue reported on code.google.com by phelan....@gmail.com on 30 Sep 2010 at 11:46

GoogleCodeExporter commented 8 years ago
It sounds to me like you're using the Javascript side of the API and running 
into the url length limit. You see, the JS side utilizes an approach called 
JSONP to request translations from Google. Essentially, this means that the 
script creates a script element and appends it to the document head in order to 
avoid the browsers' built-in same-origin policy. This forces the API to submit 
requests via the GET method, where arguments are passed in the URL. 
Unfortunately, there are two problems with this approach: MSIE imposes an 
approximately 2,000 character limit on URLs, and Google has done the same on 
their servers. So when using the Javascript API or GET method, you're really 
limited to strings of no more than 2,000 - [all the rest of the url] 
characters. This is further limited by the fact that special characters must be 
url-encoded in order for transport. So the bottom line is that your url-encoded 
string fit into that allowance. Generally, when using the JS API, I recommend 
url encoding the original string and then splitting it according clauses (e.g., 
commas (,) and periods (.)), not to exceed 1,200 characters. The easiest and 
best way that I've come up with to do this on the pure JS side is with a 
regexp. Something like:

var strings = encodeURIComponent(originalString).match(/(.{1,1200}[\,\.\n])/g);

Then simply loop through the strings array to run your translations

You'll need to be familiar with method closures, and you'll need to figure out 
how to deal with strings that are only pieces of the overall string you were 
trying to translate. But I've got to keep a little bit of the secret sauce for 
myself!

Of course, there is an alternative. If you scrap the Google-written JS API, you 
can write your own which would send the string back to your own server via 
XMLHttpRequest, which can use the POST method. Your would then need to have a 
server-side proxy which would forward the request to the RESTful side of the 
Translation API and relay the response back to the client to be parsed. Using 
the POST method, your url-encoded string can be up to 5,000 characters long, so 
you could reduce the number of overall requests from your application by a 
factor of 4 or 5. But you would still have to be prepared for strings that are 
longer than that.

Because this is not a bug with the API, I'm going to mark it invalid.

Original comment by jrgeer...@gmail.com on 30 Sep 2010 at 1:15

GoogleCodeExporter commented 8 years ago
Forgot to do the marking. Oops.

Original comment by jrgeer...@gmail.com on 30 Sep 2010 at 1:16