.comment-link {margin-left:.6em;}
Moriash Moreau: My Second Life
Wednesday, August 23, 2006
A few folks have made quite impressive attempts at building in-world "universal translator" objects. This has been going on for quite a while, actually, but the latest version has just made a splash in recent news. Coincidentally, I happened to be working on a very, very stripped down version for another project (which I'm certain I will be babbling about in great detail in the coming month, assuming it doesn't fall through), and put one together myself a week or so back.

The new SL HTTP commands make this kind of thing a breeze. Most of the free online translation services can be run directly from a single HTTP call, if you dig into them enough, and look closely at the source code and URLs they generate. I chose to use Google Language Tools, mostly because I like Google. That, and Google seems to be somewhat faster than Alta Vista's Babelfish.

Once you've sorted out the specifics of the translation service's PHP calls, you have to come up with a page for it to translate. While there are probably dozens of different ways to approach this, I settled on a simple, one-line PHP command. See the comments in the script linked below. Blogger doesn't seem to be smart enough to validate the HTML Code markup.

In any case, the .php file uses the Echo command to simply parrot anything sent to it by a GET call. Sadly, it took me a couple of hours to figure that out; this was my "Hello World" to PHP coding. I was gratified to see how many similarities there were to LSL (the only programming language I know, aside from some 10 year old HTML). If I ever get a hankering to do real world programming, it may go a little easier. I keep telling myself that, anyway, so I'm not completely wasting untold hours tinkering with LSL scripts.

Anyway, take that one line and dump it in a .php file. (Keep it on one line, with no carriage returns. The returns will show up later, and complicate your formatting and processing.) Put that on a PHP enabled webserver. (For the love of Pete, don't ask me how to do that! I'm a complete n00b at that kind of thing.) Then you can call it with "text" equal to your message, or have Google translate call it, and a simple one-line webpage will result. Have your in-world LSL script call Google translate to translate your echoed message, and you're in business.

If you're interested, here is the translator script I've been tinkering with. It's heavily commented, but the actual script is really quite simple. There's room for improvement in the interface (you have to modify the script to select language pairs, for example), but it does what I need it to do. You'll need to find your own server to host the required PHP file.

Keep in mind that llHTTPRequest is throttled to 20 calls (20 translations) per 100 seconds. I wouldn't expect this to be a problem in normal circumstances, but it could come up in crowded rooms if you use an open listen on the main chat channel. If you're serious about making a universal translator, you'd probably be better off issuing each participant in the conversation his own translator, which listens only to its owner. Or create some other scheme to limit the translation requests.

Addendum, 9/8/06:
Two things. First, the llHTTPRequest throttling is about to be changed (or may have already been, depending on when you read this) to "one request per second, per owner, per sim." This is both better and worse. (More requests available on a per minute basis, but less ability to send short, fast bursts of calls.) I'm not certain how this will affect the translator, although I'd expect the script to fall behind and possibly mis-attribute translations if this script is used on an open channel in a large, chatty crowd. This happens every once in a while now (with the 20 per second throttle), in unusually active chats, when there's a large backlog of requests sent to Google at once. The script tends to attribute backlogs to the last speaker when the requests finally clear. Some form of key checking (to make sure the right name is matched to a given translation request, when said request clears) for the HTTP requests would be good for a polished version of the script.

It's probably better to create individual translator attachments, with llListen restricted to the owner only, for each member of the conversation in those cases. That's probably better, anyway, if you're dealing with a real conversation. One attachment would go, say, from English to Spanish (worn by the English speaking user), while another would translate from Spanish to English (worn by the Spanish speaking user). A better interface would, of course, help with that.

Second, there is a better PHP script available. It helps deal with Magic Quotes settings on some PHP servers (and doesn't hurt anything on PHP servers without Magic Quotes enabled). I'm a little foggy on what Magic Quotes is for, but the upshot is it added a backslash "\" in front of all special characters when an escaped string was received (via GET) that contained special characters. This included punctuation like apostrophes (made a mess out of contractions like "it's" and "I'm") and quotation marks, and prevented Google from translating properly. (It doesn't know what "it\'s" or "I\'m" means.) Tremendous thanks to Sophia Weary for her help and her PHP wizardry in figuring this one out.

Oh, I'm afraid the PHP script above will not work if linked directly. The server it's hosted on doesn't have a workable version of PHP installed, and it's marked as a .TXT file anyway. You'll still need your own PHP server. I can't afford the bandwidth to host a publicly available PHP echo script. Sorry.
This post is very helpful and well explained. But I have a question, is it possible to translate text from SL w/o having a dedicated site like yours the one with the echo code?
I've not found a way, no. Google Translate and Alta Vista seem to require a page to translate. I wasn't able to find a way to send it the text directly as GET or POST and have it spit out a response directly. Not to say that it's not possible, but I couldn't find a way to do it. This was my first experiment with PHP and such, so someone more knowledgeable than I in such things may be able to bludgeon Google translate tools into working better for them.

Note also that the maximum chat text length is now 1024 characters. It was 256 characters when this was written. You'll need to modify the translate script accordingly. See the comments under the Listen event for the impact of this. Some sources say Get is limited to 1024 characters. So if your chat is nearly 1024 characters long, and it has odd punctuation that is escaped, it may get truncated as the overall string will end up greater than 1024 characters (remember, a space is escaped as "%20," so each space effectively adds two characters to the length of the string). Your mileage may vary. Experiment and test as needed.
Here is a finished product...

Post a Comment

Links to this post:

Create a Link

Return to Main Page