Difference between revisions of "Google Translator"

From Second Life Wiki
Jump to navigation Jump to search
m (Replaced old <LSL> block with <source lang="lsl2">)
 
(2 intermediate revisions by one other user not shown)
Line 11: Line 11:




<lsl>
<source lang="lsl2">
key http_request_id;
key http_request_id;
string transtring = "";
string transtring = "";
Line 46: Line 46:
         somestring = message;
         somestring = message;
         if (name == llKey2Name(llGetOwner())){
         if (name == llKey2Name(llGetOwner())){
         transtring =  strReplace(message, " ", "%20");
         transtring =  llEscapeURL(message);
         http_request_id = llHTTPRequest("http://translate.google.com/translate_a/t?client=t&text=" + transtring + "&hl=en&sl=es&tl=en&pc=0", [], "");
         http_request_id = llHTTPRequest("http://translate.google.com/translate_a/t?client=t&text=" + transtring + "&hl=en&sl=es&tl=en&pc=0", [], "");
     llSetObjectName(name);
     llSetObjectName(name);
Line 54: Line 54:


}
}
</lsl>
</source>

Latest revision as of 19:00, 24 January 2015

Google Translator by Ugleh Ulrik

Google Translator keeps their translated data in a Array. You can see this by looking at the HTTPRequests.

The URL is this

http://translate.google.com/translate_a/t?client=t&text=words here&hl=en&sl=es

This will make it go from Spanish to English. Then all you need to do is do a string replacement.


key http_request_id;
string transtring = "";
string nothing = "";
string name;
string somestring;
string strReplace(string str, string search, string replace) {
    return llDumpList2String(llParseStringKeepNulls((str = "") + str, [search], []), replace);
}
default
{
    state_entry()
    {
        llListen(0, "", NULL_KEY, "");
    }
 
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == http_request_id)
        {
            list trans1 = llParseString2List(body, ["\"orig\""], []);
            transtring = strReplace(llList2String(trans1,0), "{\"sentences\":[{\"trans\":\"", "");
            nothing = strReplace(transtring, "\",", "");
            nothing = strReplace(nothing, ",", "");
            nothing =  strReplace(nothing, "\\" + "\"", "\"");
            if (somestring != nothing){
            llSay(0, nothing);
        }
            llSetObjectName("es2en Translator");
        }
    }
     listen(integer channel, string name, key id, string message)
    {
        somestring = message;
         if (name == llKey2Name(llGetOwner())){
        transtring =  llEscapeURL(message);
         http_request_id = llHTTPRequest("http://translate.google.com/translate_a/t?client=t&text=" + transtring + "&hl=en&sl=es&tl=en&pc=0", [], "");
    llSetObjectName(name);
    }
    }
    

}