Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llHTTPRequest to BabelFish?

Avery Drebin
Registered User
Join date: 21 Mar 2006
Posts: 1
09-08-2007 01:39
I'm trying to make an object that will perform a simple, one-line translation from english to portuguese. I have no experience with HTTPRequest. Can anybody offer any advice on how to send a request to babelfish?
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
09-08-2007 02:07
The problem with this is, that llHTTPRequest only fetches the first 2048 Bytes of a page. This isn't enough to get to the line where Babelfish returns the results. And there's no API that only returns the translated part...

You'd have to set up a php-script on a webserver that does the translation, extracts the results and then sends them back to SL.

There's a nice tool called «Babbler» which you can obtain for free (inworld) that does multiple translations, though... :)

I made another translation tool myself, using Systrans API - but since it returns an XML-File, all the special characters - like ñ or ö, ä, ü - are not returned correctly. So I decided to abandon this project and go with babbler :)

If you wanna try anyway - here's the code:

CODE


key requestid;
string msg_body;

default
{
state_entry()
{
llListen(0,"",llGetOwner(),"");
}

listen( integer chan, string name, key id, string msg )
{
msg_body = "id=sherlock&charset=utf-8&api=1&text=" + llEscapeURL(msg) + "&lp=de_en";

requestid = llHTTPRequest("http://w4.systranlinks.com/trans",[HTTP_METHOD,"POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"],msg_body);
}

http_response(key request_id, integer status, list metadata, string body){

if (request_id == requestid)
{
integer i = llSubStringIndex(body,"CDATA[");

string dummy = llGetSubString(body, i + 7, -1);

i = llSubStringIndex(dummy, "]");

llSay(0,llGetSubString(dummy, 0, i - 1));
}
else
{
llSay(0,(string)status+" error");
}
}
}




on the first line of the listener event, you'll find the translation encoding (msg_body = [...] lp=de_en). I guess to translate from english to portuguese, you'd have to use lp=en_pt (not quite sure though :)