OpenSim Web 2.0 contribution

As described over on Eightbar I have made my first contribution to the OpenSim opensource project. The contribution is in the form of a new scripting function called osParseJSON. This function allows a c# script in OpenSim to consume the JSON notation provided by many of the major Web 2.0 APIs provided by services such as Flickr and Google translation.

The following example is a script that uses the Google Translate API to let an OpenSim avatar translate ther conversations between 23 different languages.

(disclaimer – please read the terms of conditions of the Google translate API and abide by them)


//c#
// This script is written as an example use of the osParseJSON method
// it uses the Google translate API
// ensure you have read the terms and conditions of the Google translate API
// http://code.google.com/apis/ajaxlanguage/documentation/

LSL_Types.key requestID;
string sourceLang = "en";
string targetLang = "fr";

public void default_event_state_entry()
{
llSay(0,"translator running say '/1 sentence' to translate something");
llSay(0,"translator running say '/2 source langage' to change target language e.g. '/2 fr'");
llSay(0,"translator running say '/3 target langage' to change source language e.g. '/3 en'");
llSay(0,"translator running say '/4 help', to list languages");
llListen(1, "", NULL_KEY, "");
llListen(2, "", NULL_KEY, "");
llListen(3, "", NULL_KEY, "");
llListen(4, "", NULL_KEY, "");
}

public void default_event_touch_start(LSL_Types.LSLInteger total_number)
{
llSay(0,"translator running say '/1 sentence' to translate something");
llSay(0,"translator running say '/2 source langage' to change target language e.g. '/2 fr'");
llSay(0,"translator running say '/3 target langage' to change source language e.g. '/3 en'");
llSay(0,"translator running say '/4 help', to list languages");
}

public void default_event_http_response(LSL_Types.LSLString request_id, LSL_Types.LSLInteger status, LSL_Types.list metadata, LSL_Types.LSLString body)
{
if (requestID == request_id)
{
// the Google JSON string returned wil be of the format
// {"responseData": {"translatedText":"Bonjour"}, "responseDetails": null, "responseStatus": 200}
// call the osParseJSON method so we can read the contents
System.Collections.Hashtable response = (System.Collections.Hashtable) osParseJSON(body);
System.Collections.Hashtable responsedata = (System.Collections.Hashtable) response["responseData"];

llSay(0,(string)responsedata["translatedText"]);
}
}

public void default_event_listen(LSL_Types.LSLInteger channelIn, LSL_Types.LSLString name, LSL_Types.LSLString id, LSL_Types.LSLString message)
{
if(channelIn==1)
{
string toTranslate = (string) message;
requestID = llHTTPRequest( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="+toTranslate+"&langpair="+sourceLang+"%7C"+targetLang, new LSL_Types.list(), "" );

}
else if(channelIn==2)
{
sourceLang = (string) message;
}
else if(channelIn==3)
{
targetLang = (string)message;
}
else if(channelIn==4)
{
llOwnerSay("LANGUAGE (CODE)");
llOwnerSay("* Arabic (ar)");
llOwnerSay("* Bulgarian (bg)");
llOwnerSay("* Chinese (zh)");
llOwnerSay("* Croatian (hr)");
llOwnerSay("* Czech (cs)");
llOwnerSay("* Danish (da)");
llOwnerSay("* Dutch (nl)");
llOwnerSay("* English (en)");
llOwnerSay("* Finnish (fi)");
llOwnerSay("* French (fr)");
llOwnerSay("* German (de)");
llOwnerSay("* Greek (el)");
llOwnerSay("* Hindi (hi)");
llOwnerSay("* Italian (it)");
llOwnerSay("* Japanese (ja)");
llOwnerSay("* Korean (ko)");
llOwnerSay("* Norwegian(no) ");
llOwnerSay("* Polish (pl)");
llOwnerSay("* Portuguese (pt-PT)");
llOwnerSay("* Romanian (ro)");
llOwnerSay("* Russian (ru)");
llOwnerSay("* Spanish (es)");
llOwnerSay("* Swedish (sv)");

}
}

6 Comments

  1. Pingback: eightbar » Blog Archive » Web 2.0 to OpenSim made easy (JSON support added)

  2. Pingback: OpenSim Scripting Languages: LSL and OSSL « justincc’s opensim blog

  3. Pingback: C# scripting in OpenSim « justincc’s opensim blog

  4. pastowej

    C# is prominent nowadays. I’m learning it for a sideline income in the future.

  5. Sergio Lubo

    I use Dev OpenSim 7.0.2, running the code shows me a message “supplied then the key was not found in the dictionary”

  6. E Lawler

    I doubt anyone will look at this considering the article was written so long ago,
    BUT I was wondering if it would be possible to see the code with proper formatting?

    The way it appears in my browser and when copied is as a block of text that ends up being one giant comment. I could make out the lines by putting in a basic structure but this became confusing near the end.

    I’d appreciate it.

Comments are closed.