balazs-endresz/jquery-translate

support for Google v2 API

GoogleCodeExporter opened this issue · 25 comments

Already implemented, I'll commit it soon.

Original issue reported on code.google.com by balazs.endresz on 6 Nov 2011 at 4:21

When will this be committed?

Original comment by sofia.chiang on 9 Nov 2011 at 10:59

Then I hope you will make it this weekend.

Original comment by sofia.chiang on 11 Nov 2011 at 12:08

In the weekend probably, unless I'll be moving to the new house then. Or if I 
don't have to spend a day persuading Windows to log in instead of showing a 
blank screen with a mouse pointer like last weekend.

Original comment by balazs.endresz on 9 Nov 2011 at 12:41

You can give it a try now, it hasn't been tested much at all so any feedback is 
welcome.

Original comment by balazs.endresz on 13 Nov 2011 at 5:56

Im not really sure how to invoke version 2:

I am trying this:

 $.translate.load('09389083jhih3839hihi3h8-A', "2");

And I get:

Uncaught Error: Module: 'language' with version: '2' not found!
(anonymous function)

09389083jhih3839hihi3h8-A is of course not my API-key

I

Original comment by axelsson...@gmail.com on 14 Nov 2011 at 3:07

Hello!

Thanks a lot for your effort with v2 API.

We will be very grateful if  you can make the commit this weekend. ;)

Best regards,
Frankai

Original comment by frank...@gmail.com on 12 Nov 2011 at 6:12

One thing that needs to be fixed up is the API request currently expects a 
successful response, but a "success" may contain an error response.

In this section, the action response does not necessarily have data, but may 
have an error. In fact, it always has for me to this point :-)

In var tr = response.data.translations[0].translatedText

there is no "data", just "errors"


    $.ajax({
                url:  "https://www.googleapis.com/language/translate/v2",
                dataType: "jsonp",
                jsonp: "callback",
                crossDomain: true,
                context: this,
                data: {"key": key, source: this.from, target: this.to, q: src},
                success: function(response){
                    var tr = response.data.translations[0].translatedText
                    this.queue[i] = tr || this.rawSourceSub;
                    //this.detectedSourceLanguage = result.detectedSourceLanguage;
                    this._check();
                }

Original comment by ra...@randyfay.com on 15 Nov 2011 at 3:39

I'll fix the error handling tonight.

Original comment by balazs.endresz on 15 Nov 2011 at 7:54

I'd say check the request in the network tab in the dev tools first, or if you 
don't mind you can send your API key by email as well to 
balazs.endresz@gmail.com.

Original comment by balazs.endresz on 14 Nov 2011 at 3:31

Hi,

You can discard my previous post. I was using release 1.4.7 not the trunk.

So to my new issue. I ve downloaded trunk ( 
http://jquery-translate.googlecode.com/svn/trunk/jquery.translate-debug-all.js 
revision: 247 ). At line 661 to 690 the code is out commented and the version 
argument is never used in "load". 

This is not right is it?

BR
paera

Original comment by axelsson...@gmail.com on 17 Nov 2011 at 2:56

That's because recently I've removed support for the Google v1 API, and now the 
version number is unnecessary. I'll clean these things up when there aren't any 
issues with the new APIs.

Original comment by balazs.endresz on 17 Nov 2011 at 5:02

@ra...@randyfay.com: That should be fixed now.

Original comment by balazs.endresz on 15 Nov 2011 at 9:10

Thanks again for all your work on this.

Currently, if you call _translate without a this.from (if fromOriginal:true), 
it formats the REST request with &source=&, which is a failure for the Google 
API. To use the detected source language you have to leave out the &source 
argument.

So right now, if you leave the from language empty, this request is generated:

https://www.googleapis.com/language/translate/v2?callback=jsonp1322241205714&_=1
322241210290&key=XXXXXXBwT3hoRXWU1UkYIBK5Mm_tdpdRbXXXXXX&source=&target=es&q=Thi
s%20is%20a%20test

and it's invalid. It gets a response of:

// API callback
jsonp1322241205714({
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}
);

However, if you just change this to 

https://www.googleapis.com/language/translate/v2?callback=jsonp1322241205714&_=1
322241210290&key=XXXXXXBwT3hoRXWU1UkYIBK5Mm_tdpdRbXXXXXX&target=es&q=This%20is%2
0a%20test

it works perfectly (removing the source=)

Original comment by ra...@randyfay.com on 25 Nov 2011 at 5:20

Nice catch! Fixed at r248.

Original comment by balazs.endresz on 26 Nov 2011 at 11:04

The interaction with the google api is now working, THANKS!.

The next problem is that in the code below we get

this.queue is undefined
[Break On This Error] this.queue[i] = tr || this.rawSourceSub; 

success: function(response){
if(response.error){
return this.options.error.call(this, response.error, this.rawSourceSub, 
this.from, this.to, this.options);
}
var tr = response.data.translations[0].translatedText
this.queue[i] = tr || this.rawSourceSub; 

Original comment by ra...@randyfay.com on 26 Nov 2011 at 4:24

That just can't be happening. Could you please send me the page via email or 
somehow?

Original comment by balazs.endresz on 26 Nov 2011 at 4:36

Sent a firebug screenshot showing the state at the time of the #fail. Thanks.

Original comment by ra...@randyfay.com on 26 Nov 2011 at 5:03

It seems like the `context` option in $.ajax has no effect in you're case. What 
version of jQuery are you using?

Original comment by balazs.endresz on 26 Nov 2011 at 5:22

Ah great question. This test was with 1.2.6, which is what this site has always 
used with jquery.translate. If I have to, I can push it up to 1.3.2 using an 
adapter module (jquery_update), but no farther for Drupal 6.

Original comment by ra...@randyfay.com on 26 Nov 2011 at 5:36

It should work with r249.

Original comment by balazs.endresz on 26 Nov 2011 at 5:43

Great! :)

Original comment by balazs.endresz on 26 Nov 2011 at 6:16

Drupal 6 and Drupal 7 versions of http://drupal.org/project/translatableregions 
are working and checked in! Yay. And the $20/MB that Google charges may not 
bankrupt the site that uses this.

Original comment by ra...@randyfay.com on 26 Nov 2011 at 6:33

And it does! Yay! balazs.endresz++ Thanks so much.

Original comment by ra...@randyfay.com on 26 Nov 2011 at 5:56

Oh, and I almost forgot that the MS API seems to accept arbitrary long text, 
which makes it actually usable because otherwise it can't always handle HTML 
split into multiple parts: 
http://code.google.com/p/jquery-translate/issues/detail?id=75
So that seems like a good alternative as well. I'll try to work on it tomorrow.

Original comment by balazs.endresz on 26 Nov 2011 at 8:14

Great - I'll try to test after you do. Unfortunately I *think* Microsoft's 
offerings are less advantageous economically. You pay them by the month as 
opposed to paying Google by the MB. And the entry point after 2000 transactions 
(for MS) is $40/month.

Since you've made all this great progress it would be good to update the main 
page as well.

Original comment by ra...@randyfay.com on 26 Nov 2011 at 11:28