Jump to content

Easy Language Handler


revochen Mayne
 Share

You are about to reply to a thread that has been inactive for 3911 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

This script will handle different languages, set up by notecards and provides a user menu based on. The notecard setup is described below the following script code.

 

/* Multi Language Script

by revochen Mayne */

// global system variables
integer listen_handler;
integer menu_channel = -1000;
list main_menu;
key user = NULL_KEY;
string language = "en";
list lang_params;
list lang_values;
key notecard_request;
integer notecard_line;

// handy method to get a language value by param
string GetLang(string GL_param)
{
    integer GL_index = llListFindList(lang_params,[GL_param]);
    if(GL_index!=-1)
    {
        return llList2String(lang_values,GL_index);
    }
    else return GL_param;
}
// handy method to set a language value by param
SetLang(string SL_param, string SL_value)
{
    integer SL_index = llListFindList(lang_params,[SL_param]);
    if(SL_index!=-1)
    {
        lang_values = llListReplaceList(lang_values,[SL_value],SL_index,SL_index);
    }
}

default
{
    state_entry()
    {
        // initializing english language setup
        notecard_request = llGetNotecardLine("lang_"+language,notecard_line);
    }
    touch_start(integer total_number)
    {
        key who = llDetectedKey(0);
        if(user==NULL_KEY || who==user) // if menu is not in used or got requested again by 'user'
        {
            user = who;
            listen_handler = llListen(menu_channel,"",user,"");
            llDialog(user,"\n"+GetLang("MENU_DESC"),main_menu,menu_channel);
            llSetTimerEvent(30);
        }
    }
    listen(integer channel, string name, key id, string message)
    {
        llSetTimerEvent(0);
        llListenRemove(listen_handler);
        if(channel==menu_channel)
        {
            if(message==GetLang("AN_OPTION")) // menu user picked AN_OPTION
            {
                llSay(0,GetLang("OPTION_REPLY"));
                user = NULL_KEY;
            }
            else if(message==GetLang("SET_LANG")) // menu user picked SET_LANG
            {
                // gathering available languages by reading containing notecards:
                integer count = 0;
                integer max = llGetInventoryNumber(INVENTORY_NOTECARD);
                list lang_menu;
                while(count<max && count<12)
                {
                    list split = llParseString2List(llGetInventoryName(INVENTORY_NOTECARD,count),["_"],[]);
                    lang_menu += llList2String(split,-1);
                    ++count;
                }
                listen_handler = llListen(menu_channel+1,"",id,"");
                llDialog(id,"\n"+GetLang("CHOOSE_LANG"),lang_menu,menu_channel+1);
                llSetTimerEvent(30);
            }
        }
        else if(channel==menu_channel+1) // user picked a new language
        {
            language = message;
            user = NULL_KEY;
            
            // initializing new language setup:
            lang_params = [];
            lang_values = [];
            notecard_line = 0;
            notecard_request = llGetNotecardLine("lang_"+language,notecard_line);
        } 
    }
    timer()
    {
        llSetTimerEvent(0);
        llListenRemove(listen_handler);
        user=NULL_KEY;
    }
    dataserver(key request, string data)
    {
        if(request==notecard_request)
        {
            if(data!=EOF) // didn't reached EndOfFile yet
            {
                // parsing and processing notecard data
                list split = llParseString2List(data,["="],[]);
                if(llGetListLength(split)==2)
                {
                    lang_params += llList2String(split,0);
                    lang_values += llList2String(split,1);
                }
                ++notecard_line;
                notecard_request = llGetNotecardLine("lang_"+language,notecard_line);
            }
            else // finished reading notecard
            {
                main_menu = [GetLang("AN_OPTION"), GetLang("SET_LANG")]; // reassemble menu
                llSay(0, GetLang("CHANGE_COMPLETE")+" "+(string)llGetUsedMemory()+"bytes "+GetLang("MEM_USED")+"/"+(string)llGetFreeMemory()+"bytes "+GetLang("MEM_FREE"));
            }
        }
    } 
}

/* http://hotfusion-sl.com*/

 

A language notecard name needs to start with "lang_" and ends with its short language name like "en" for english. Following parameters and values needs to get defined in:

MENU_DESC=Please choose an option:

AN_OPTION=Any Option
OPTION_REPLY=This is just an example!

SET_LANG=Set Language
CHOOSE_LANG=Please choose a language:
CHANGE_COMPLETE=Language change complete!

MEM_FREE=memory free
MEM_USED=memory used

 

 The script is very basic and only provides an option to change the language and another sample option. For any further use the script needs additional code especially for error handling if wanted (like no notecard including, menu buttons limited to 12, button label limited to 24 characters and so on).

Any feedback and suggestion much appreciated. =)

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3911 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...