| View previous topic :: View next topic |
| Будет ли для вас такой плагин полезным? |
| Да! |
|
50% |
[ 3 ] |
| Нет! |
|
50% |
[ 3 ] |
|
| Total Votes : 6 |
|
| Author |
Message |
FeyFre
Joined: 07 Aug 2007 Posts: 1889 Location: Vinnitsa, Ukraine
|
Posted: Fri Apr 29, 2011 3:22 pm Post subject: |
|
|
VladSh
| Code: | req.open("POST", url тоже может быть в виде /path/to/doc?query, false);
req.send(строка - тело POST-запроса например "source=eng&dst=rus&text=properly_escaped_text_data") |
только не забудьте правильно заголовки выставить при POST запросе(больше уточните в RFC или поснифферите запросы как оно в сети передается |
|
| Back to top |
|
 |
Instructor Site Admin
Joined: 06 Jul 2006 Posts: 4640
|
Posted: Fri Apr 29, 2011 3:43 pm Post subject: |
|
|
Насчет POST: http://stackoverflow.com/questions/1915203/google-translate-call-from-js-using-post
| Quote: | | И ещё неприятно - съедаются переводы строк.. можно ли что-то с этим сделать? | Можно попробовать так:
| Code: | 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: | ...
showErrorMessage(resultObject.responseDetails);
}
resultText=resultText.replace(/ {0,1}<n> {0,1}/g, "\n"); |
|
|
| Back to top |
|
 |
VladSh
Joined: 29 Nov 2006 Posts: 2509 Location: Киев, Русь
|
Posted: Fri Apr 29, 2011 4:03 pm Post subject: |
|
|
Instructor
Была идея на какой-то символ заменить, но не знал какой не съедается..
Здорово! Добавил в скрипт; спасибо! Теперь переводы не съедаются
& FeyFre
по поводу POST - то не скоро ещё будет, т.к. в эту область вступил впервые  |
|
| Back to top |
|
 |
user-id-4576
Joined: 23 Apr 2011 Posts: 9
|
Posted: Fri Apr 29, 2011 6:39 pm Post subject: |
|
|
| Так глядишь и в плагин чето перерастет))) |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Fri Apr 29, 2011 6:47 pm Post subject: |
|
|
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: | var tmpText = req.responseText;
var tmpText = tmpText.replace("(", " = ");
tmpText = tmpText.replace(new RegExp("[" + ")" + "]+$", ""), "");
tmpText = "var " + tmpText ; |
You can do this:
| Code: | | var tmpText = req.responseText.replace("resultObject", "var resultObject="); |
|
|
| Back to top |
|
 |
VladSh
Joined: 29 Nov 2006 Posts: 2509 Location: Киев, Русь
|
Posted: Fri Apr 29, 2011 7:06 pm Post subject: |
|
|
KDJ
Thank you! Your correction was introduced.
But I still v.2 not working, I do not know how you make it to work:) |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Fri Apr 29, 2011 7:23 pm Post subject: |
|
|
VladSh
For me, v2 works.
There is still a problem with autodetect of the source language.
You should change this code:
| Code: | 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"; |
|
|
| Back to top |
|
 |
VladSh
Joined: 29 Nov 2006 Posts: 2509 Location: Киев, Русь
|
Posted: Fri Apr 29, 2011 7:42 pm Post subject: |
|
|
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". |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Fri Apr 29, 2011 7:56 pm Post subject: |
|
|
VladSh
Maybe, you do not type the googleAPIkey. |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Sat Apr 30, 2011 9:59 pm Post subject: |
|
|
VladSh
POST method works.
For the google API v1, try this:
| Code: | 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"); |
|
|
| Back to top |
|
 |
VladSh
Joined: 29 Nov 2006 Posts: 2509 Location: Киев, Русь
|
Posted: Mon May 02, 2011 7:34 pm Post subject: |
|
|
KDJ
Super! Thank you!
Коллеги, ну что, заменяем в скрипте GET на POST? Скорость для небольшого текста такая же, - переводит почти мгновенно.. |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Mon May 02, 2011 8:28 pm Post subject: |
|
|
| In POST method is limit size of the input text to 5000 characters. |
|
| Back to top |
|
 |
Instructor Site Admin
Joined: 06 Jul 2006 Posts: 4640
|
Posted: Tue May 03, 2011 4:48 am Post subject: |
|
|
| VladSh wrote: | | Коллеги, ну что, заменяем в скрипте GET на POST? | У меня POST тоже работает. |
|
| Back to top |
|
 |
VladSh
Joined: 29 Nov 2006 Posts: 2509 Location: Киев, Русь
|
Posted: Wed May 04, 2011 11:39 am Post subject: |
|
|
Откорректировал скрипт.
Для истории и на всякий случай (всё с течением времени имеет место отваливаться) оставил метод 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: | 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; | ? |
|
| Back to top |
|
 |
KDJ
Joined: 06 Mar 2010 Posts: 1067 Location: Poland
|
Posted: Wed May 04, 2011 1:00 pm Post subject: |
|
|
VladSh
To use POST method in API v2, you must add the header: X-HTTP-Method-Override -> GET.
| Code: | 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); |
|
|
| Back to top |
|
 |
|