Page 2 of 3

Posted: Fri Apr 29, 2011 3:22 pm
by FeyFre
VladSh

Code: Select all

req.open("POST", url тоже может быть в виде /path/to/doc?query, false);
req.send(строка - тело POST-запроса например "source=eng&dst=rus&text=properly_escaped_text_data")
только не забудьте правильно заголовки выставить при POST запросе(больше уточните в RFC или поснифферите запросы как оно в сети передается

Posted: Fri Apr 29, 2011 3:43 pm
by Instructor
Насчет POST: http://stackoverflow.com/questions/1915 ... using-post
И ещё неприятно - съедаются переводы строк.. можно ли что-то с этим сделать?
Можно попробовать так:

Code: Select all

   var selection;
   var url;

   if (selection=AkelPad.GetSelText(2 /*\n*/))
     selection=selection.replace(/\n/g, "<n>");

   if (iAPIVersion == 2)      //Google API v.2
      url = "https://www.googleapis.com/language/translate/v2?key=" + googleAPIkey + "&source=" + langSource + "&target=" + langTarget + "&callback=resultObject&q=" + encodeURIComponent(selection);
   else      //Google API v.1
      url = "https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + encodeURIComponent(selection) + "&langpair=" + langSource + "%7C" + langTarget + "&callback=resultObject";
и

Code: Select all

                  ...
                  showErrorMessage(resultObject.responseDetails);
            }
            resultText=resultText.replace(/ {0,1}<n> {0,1}/g, "\n");

Posted: Fri Apr 29, 2011 4:03 pm
by VladSh
Instructor
Была идея на какой-то символ заменить, но не знал какой не съедается..
Здорово! Добавил в скрипт; спасибо! Теперь переводы не съедаются 8)

& FeyFre
по поводу POST - то не скоро ещё будет, т.к. в эту область вступил впервые :D

Posted: Fri Apr 29, 2011 6:39 pm
by user-id-4576
Так глядишь и в плагин чето перерастет)))

Posted: Fri Apr 29, 2011 6:47 pm
by KDJ
VladSh
Do not work in API v2.
Generates this error: "Error: [objectError]".
Because, at end of responseText is a semicolon ";", not a parenthesis ")".
Instead:

Code: Select all

var tmpText = req.responseText;
var tmpText = tmpText.replace("(", " = ");
tmpText = tmpText.replace(new RegExp("[" + ")" + "]+$", ""), "");
tmpText = "var " + tmpText ;
You can do this:

Code: Select all

var tmpText = req.responseText.replace("resultObject", "var resultObject=");

Posted: Fri Apr 29, 2011 7:06 pm
by VladSh
KDJ
Thank you! Your correction was introduced.
But I still v.2 not working, I do not know how you make it to work:)

Posted: Fri Apr 29, 2011 7:23 pm
by KDJ
VladSh
For me, v2 works.
There is still a problem with autodetect of the source language.
You should change this code:

Code: Select all

var url;
if (iAPIVersion == 2) //Google API v.2
{
  if (langSource)
    url = "https://www.googleapis.com/language/translate/v2?key=" + googleAPIkey + "&source=" + langSource + "&target=" + langTarget + "&callback=resultObject&q=" + encodeURIComponent(selection);
  else
    url = "https://www.googleapis.com/language/translate/v2?key=" + googleAPIkey + "&target=" + langTarget + "&callback=resultObject&q=" + encodeURIComponent(selection);
}
else //Google API v.1
  url = "https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + encodeURIComponent(selection) + "&langpair=" + langSource + "%7C" + langTarget + "&callback=resultObject";

Posted: Fri Apr 29, 2011 7:42 pm
by VladSh
KDJ
Changed.
But the problem is apparently not the case, since it have not worked and now does not work even with an explicit langSource="ru" or "en".

Posted: Fri Apr 29, 2011 7:56 pm
by KDJ
VladSh
Maybe, you do not type the googleAPIkey.

Posted: Sat Apr 30, 2011 9:59 pm
by KDJ
VladSh
POST method works.
For the google API v1, try this:

Code: Select all

url = "https://ajax.googleapis.com/ajax/services/language/translate";
req.open("POST", url, false);
req.onreadystatechange = processReqChange;
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send("v=1.0&q=" + selection + "&langpair=" + langSource + "%7C" + langTarget + "&callback=resultObject");

Posted: Mon May 02, 2011 7:34 pm
by VladSh
KDJ
Super! Thank you!

Коллеги, ну что, заменяем в скрипте GET на POST? Скорость для небольшого текста такая же, - переводит почти мгновенно..

Posted: Mon May 02, 2011 8:28 pm
by KDJ
In POST method is limit size of the input text to 5000 characters.

Posted: Tue May 03, 2011 4:48 am
by Instructor
VladSh wrote:Коллеги, ну что, заменяем в скрипте GET на POST?
У меня POST тоже работает.

Posted: Wed May 04, 2011 11:39 am
by VladSh
Откорректировал скрипт.
Для истории и на всякий случай (всё с течением времени имеет место отваливаться) оставил метод GET (<=500 символов) и добавил POST.

KDJ
I can not test work on the v.2, please do.
I do not know, whether the current settings for the POST, or have to do something like this:

Code: Select all

url = "https://ajax.googleapis.com/ajax/services/language/translate";
params = "v=2.0&key=" + googleAPIkey + (langSource ? "&source=" + langSource : "") + "&target=" + langTarget + "&callback=" + vn_resultObject + "&q=" + selection;
?

Posted: Wed May 04, 2011 1:00 pm
by KDJ
VladSh
To use POST method in API v2, you must add the header: X-HTTP-Method-Override -> GET.

Code: Select all

req.open("POST", url, false);
req.onreadystatechange = processReqChange;
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestHeader("X-HTTP-Method-Override", "GET");
req.send(params);