Discuss and announce AkelPad plugins
Offline
Posts: 2247 Joined: Tue Aug 07, 2007 2:03 pm
Location: Vinnitsa, Ukraine
Post
by FeyFre » Fri Apr 29, 2011 3:22 pm
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 или поснифферите запросы как оно в сети передается
Offline
Site Admin
Posts: 6311 Joined: Thu Jul 06, 2006 7:20 am
Post
by Instructor » Fri Apr 29, 2011 3:43 pm
Насчет 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");
Offline
Posts: 3217 Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:
Post
by VladSh » Fri Apr 29, 2011 4:03 pm
Instructor
Была идея на какой-то символ заменить, но не знал какой не съедается..
Здорово! Добавил в скрипт; спасибо! Теперь переводы не съедаются
&
FeyFre
по поводу POST - то не скоро ещё будет, т.к. в эту область вступил впервые
Offline
Posts: 9 Joined: Sat Apr 23, 2011 7:53 am
Post
by user-id-4576 » Fri Apr 29, 2011 6:39 pm
Так глядишь и в плагин чето перерастет)))
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Fri Apr 29, 2011 6:47 pm
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=");
Offline
Posts: 3217 Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:
Post
by VladSh » Fri Apr 29, 2011 7:06 pm
KDJ
Thank you! Your correction was introduced.
But I still v.2 not working, I do not know how you make it to work:)
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Fri Apr 29, 2011 7:23 pm
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";
Offline
Posts: 3217 Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:
Post
by VladSh » Fri Apr 29, 2011 7:42 pm
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".
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Fri Apr 29, 2011 7:56 pm
VladSh
Maybe, you do not type the googleAPIkey.
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Sat Apr 30, 2011 9:59 pm
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");
Offline
Posts: 3217 Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:
Post
by VladSh » Mon May 02, 2011 7:34 pm
KDJ
Super! Thank you!
Коллеги, ну что, заменяем в скрипте GET на POST? Скорость для небольшого текста такая же, - переводит почти мгновенно..
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Mon May 02, 2011 8:28 pm
In POST method is limit size of the input text to 5000 characters.
Offline
Site Admin
Posts: 6311 Joined: Thu Jul 06, 2006 7:20 am
Post
by Instructor » Tue May 03, 2011 4:48 am
VladSh wrote: Коллеги, ну что, заменяем в скрипте GET на POST?
У меня POST тоже работает.
Offline
Posts: 3217 Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:
Post
by VladSh » Wed May 04, 2011 11:39 am
Откорректировал скрипт.
Для истории и на всякий случай (всё с течением времени имеет место отваливаться) оставил метод 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;
?
Offline
Posts: 1949 Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland
Post
by KDJ » Wed May 04, 2011 1:00 pm
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);