To your first question: yes, that is exactly what jQuery is doing under the hood. And to your second question: that is a valid approach, but here are a couple of observations:
1. To do this, you will actually need to keep your text at less than 2,000 characters. In fact, you may have to keep it substantially less than 2,000 characters. You see, at least older versions of MSIE had a URL length limit of 2,048 characters (or thereabouts. I didn't take the time to look up the exact number). Subsequently, Google has also established a length limit of about 2,000 characters on URLs to which their servers will respond (thus you get the error you were seeing). This means that the length of your *urlencoded* string can't be longer than 2,000 - the actual service URL - all other name/value pairs. By the time you get API key, callback, language parameters, etc., that can substantially lessen the length of the string you can pass, and even more so if you're using special characters. Each one of these will be translated into %xx format, potentially tripling the length of the string you actually send to the URL. 2. Doing this inevitably impacts performance. For a string of 5,000 characters, instead of making one request to Google, you end up making at least three requests. This will slow your application. 3. It also introduces a little more complexity into your application. If you're doing the requests asynchronously (best for performance), you're going to need be able to keep track of each translation as it comes back because they may not be returned in the same order you sent them. You'll need to use method closures, regardless of how you split the string. 4. You'll also want to be very careful about how you split the string. The machine translation algorithms work better with long strings, particularly when they form complete clauses (e.g., sentences). So you would not want to just cut it off at the word break closest to the length you would need (much less the character at the word length you need!). Rather, you will want to break on commas or periods or equivalent grammatical marks (although, given the degradation of grammar which seems so prevalent today, you may not be able to count on such things). 5. Regardless of how you proceed, you may be interested in the Javascript wrapper I built for the Translate API v2. You can find it here: https://code.google.com/p/gtranslate-api-v2-jsapi/ . It contains a couple of things which you may find useful. First, you can see the regex I use to split long strings on line 251 of jTranslate.js, as well as the way that I use it starting on line 216. And second, if you're wanting to avoid writing the server-side proxy because you aren't comfortable with server-side languages, the package includes just such an app written in PHP. The downside is that I just noticed that the server-side proxy is apparently having issues (don't know how I missed the emails about that issue), so I will have to address that in the next few days. Hope these observations help! jg On Sun, Apr 7, 2013 at 3:54 AM, Andrew Beaven <ajbea...@gmail.com> wrote: > Awesome, thanks for your help mate. As you may have already guessed, I > haven't used JSONP before and don't know much about it. In the google > translate docs, there's an example under REST using > JavaScript<https://developers.google.com/translate/v2/getting_started#jsonp>, > is this what jquery does under the hood when you specify dataType: 'jsonp' > in an ajax call? > > Anyway, back to the question at hand... I'm thinking that because I will > potentially need to translate more than 5K characters at a time, I think > I'll go about this a different way, use a GET and if the text is bigger > than 2K, I'll do multiple requests to the translate service and join the > results back up when they return. Thoughts on this approach? > > On Sunday, April 7, 2013 2:16:30 PM UTC+12, jgeerdes [AJAX APIs "Guru"] > wrote: > >> That's okay. We used to support the Translate API over here and, in fact, >> are still happy to do so as we can. At any rate, the problem is that JSONP >> does not do POST requests. Instead of using a XMLHttpRequest, where you can >> control headers and request method, it uses a script tag, where you can >> only send information via GET. To do what you're wanting to do, you're >> going to need to build a proxy on your own domain/server using PHP, Perl, >> or some other server-side language. That way you can use an XMLHttpRequest >> to send the data via POST to your server, which will relay the request to >> Google's server and then return Google's response to your application. >> >> Don't forget to tell them over in the Translate API forum that we >> provided the answer :D >> >> jg >> >> On Sat, Apr 6, 2013 at 6:55 PM, Andrew Beaven <ajbe...@gmail.com> wrote: >> >>> Sorry, wrong forum. Reposted in https://groups.google.com/** >>> forum/#!topic/google-**translate-api/vFfr7lhWz1M<https://groups.google.com/forum/#!topic/google-translate-api/vFfr7lhWz1M> >>> >>> >>> On Sunday, April 7, 2013 2:34:04 AM UTC+13, Andrew Beaven wrote: >>>> >>>> Hi there, >>>> >>>> I'm trying to call the Google Translate API using ajax like so: >>>> >>>> var Utils = { >>>>> translate: function(message, callback) { >>>>> $.ajax({ >>>>> type: 'POST', >>>>> url: >>>>> 'https://www.googleapis.com/**la**nguage/translate/v2<https://www.googleapis.com/language/translate/v2> >>>>> ', >>>>> headers: { "X-HTTP-Method-Override": "GET" }, >>>>> dataType: 'jsonp', >>>>> data: { >>>>> key: '<my key here>', >>>>> source: 'en', >>>>> target: 'ru', >>>>> q: message >>>>> }, >>>>> success: callback >>>>> }); >>>>> } >>>>> } >>>> >>>> >>>> I'm finding that when the message is too large, I get a 414 error, >>>> indicating that the request URL is too large - it looks like the message >>>> is concatenated onto the request URL. I'm confused by this because I had >>>> thought that data sent in POST ajax requests were not sent via parameters >>>> in the URL. The translate docs mention if you want to sent lots of data, to >>>> do so via a POST, so I thought I'd be fine >>>> >>>> Is there anything I'm doing wrong here? >>>> >>> -- >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Google AJAX APIs" group. >>> To post to this group, send email to >>> google-ajax...@**googlegroups.com >>> >>> To unsubscribe from this group, send email to >>> google-ajax-search-api+**unsubscr...@googlegroups.com >>> To view this message on the web, visit >>> https://groups.google.com/d/**msg/google-ajax-search-api/-/** >>> XfBD35OP1m4J<https://groups.google.com/d/msg/google-ajax-search-api/-/XfBD35OP1m4J> >>> >>> For more options, visit this group at >>> http://groups.google.com/**group/google-ajax-search-api?**hl=en?hl=en<http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en> >>> >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Google AJAX APIs" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to google-ajax-search-api+**unsubscr...@googlegroups.com. >>> For more options, visit >>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>> . >>> >>> >>> >> >> >> >> -- >> Jeremy R. Geerdes >> Generally Cool Guy >> Des Moines, IA >> >> If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan >> Church! >> http://www.**debraheightswesleyan.org<http://www.debraheightswesleyan.org> >> > -- > -- > You received this message because you are subscribed to the Google > Groups "Google AJAX APIs" group. > To post to this group, send email to > google-ajax-search-api@googlegroups.com > To unsubscribe from this group, send email to > google-ajax-search-api+unsubscr...@googlegroups.com > To view this message on the web, visit > https://groups.google.com/d/msg/google-ajax-search-api/-/h8tnFmhrqNYJ > > For more options, visit this group at > http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "Google AJAX APIs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to google-ajax-search-api+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Jeremy R. Geerdes Generally Cool Guy Des Moines, IA If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church! http://www.debraheightswesleyan.org -- -- You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group. To post to this group, send email to google-ajax-search-api@googlegroups.com To unsubscribe from this group, send email to google-ajax-search-api+unsubscr...@googlegroups.com To view this message on the web, visit http://groups.google.com/group/google-ajax-search-api?hl=en_US For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-search-api+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.