AkelPad Forum Index AkelPad
Support forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Request: плагин - переводчик
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins
View previous topic :: View next topic  

Будет ли для вас такой плагин полезным?
Да!
50%
 50%  [ 3 ]
Нет!
50%
 50%  [ 3 ]
Total Votes : 6

Author Message
FeyFre



Joined: 07 Aug 2007
Posts: 2045
Location: Vinnitsa, Ukraine

PostPosted: Fri Apr 29, 2011 3:22 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Instructor
Site Admin


Joined: 06 Jul 2006
Posts: 5381

PostPosted: Fri Apr 29, 2011 3:43 pm    Post subject: Reply with quote

Насчет 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
View user's profile Send private message Send e-mail
VladSh



Joined: 29 Nov 2006
Posts: 2610
Location: Киев, Русь

PostPosted: Fri Apr 29, 2011 4:03 pm    Post subject: Reply with quote

Instructor
Была идея на какой-то символ заменить, но не знал какой не съедается..
Здорово! Добавил в скрипт; спасибо! Теперь переводы не съедаются Cool

& FeyFre
по поводу POST - то не скоро ещё будет, т.к. в эту область вступил впервые Very Happy
Back to top
View user's profile Send private message Visit poster's website
user-id-4576



Joined: 23 Apr 2011
Posts: 9

PostPosted: Fri Apr 29, 2011 6:39 pm    Post subject: Reply with quote

Так глядишь и в плагин чето перерастет)))
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Fri Apr 29, 2011 6:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
VladSh



Joined: 29 Nov 2006
Posts: 2610
Location: Киев, Русь

PostPosted: Fri Apr 29, 2011 7:06 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Fri Apr 29, 2011 7:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
VladSh



Joined: 29 Nov 2006
Posts: 2610
Location: Киев, Русь

PostPosted: Fri Apr 29, 2011 7:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Fri Apr 29, 2011 7:56 pm    Post subject: Reply with quote

VladSh
Maybe, you do not type the googleAPIkey.
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Sat Apr 30, 2011 9:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
VladSh



Joined: 29 Nov 2006
Posts: 2610
Location: Киев, Русь

PostPosted: Mon May 02, 2011 7:34 pm    Post subject: Reply with quote

KDJ
Super! Thank you!

Коллеги, ну что, заменяем в скрипте GET на POST? Скорость для небольшого текста такая же, - переводит почти мгновенно..
Back to top
View user's profile Send private message Visit poster's website
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Mon May 02, 2011 8:28 pm    Post subject: Reply with quote

In POST method is limit size of the input text to 5000 characters.
Back to top
View user's profile Send private message
Instructor
Site Admin


Joined: 06 Jul 2006
Posts: 5381

PostPosted: Tue May 03, 2011 4:48 am    Post subject: Reply with quote

VladSh wrote:
Коллеги, ну что, заменяем в скрипте GET на POST?
У меня POST тоже работает.
Back to top
View user's profile Send private message Send e-mail
VladSh



Joined: 29 Nov 2006
Posts: 2610
Location: Киев, Русь

PostPosted: Wed May 04, 2011 11:39 am    Post subject: Reply with quote

Откорректировал скрипт.
Для истории и на всякий случай (всё с течением времени имеет место отваливаться) оставил метод 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
View user's profile Send private message Visit poster's website
KDJ



Joined: 06 Mar 2010
Posts: 1372
Location: Poland

PostPosted: Wed May 04, 2011 1:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


SourceForge.net Logo Powered by phpBB © 2001, 2005 phpBB Group