f5b5ba86-2cbf-3e9e-9fbc-c9c60d68c9e9_script.lsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Standard translation script for Sloodle.
  4. // Contains the common, re-usable words and phrases.
  5. //
  6. // The "locstrings" list is pairs of strings.
  7. // The first of each pair is the name, and second is the translation.
  8. //
  9. // This script is part of the Sloodle project.
  10. // Copyright (c) 2008 Sloodle (various contributors)
  11. // Released under the GNU GPL v3
  12. //
  13. // Contributors:
  14. // Peter R. Bloomfield
  15. //
  16. // 6 Apr 09 TW Opensim init mods
  17. // Note: where a translation string contains {{x}} (where x is a number),
  18. // it means that a parameter can be inserted. Please make sure to include these
  19. // parameters in the appropriate location in your translation.
  20. // It may be sensible to add comments after your string to indicate what its parameters mean.
  21. // NOTE: parameter numbering starts at 0 (unlike previous versions, which started at 1).
  22. // Translations can be requested by sending a link message on the SLOODLE_CHANNEL_TRANSLATION_REQUEST channel.
  23. // It is advisable simply to use the "sloodle_translation_request" function provided in this script.
  24. ///// TRANSLATION /////
  25. // Localization batch - indicates the purpose of this file
  26. string mybatch = "toolbar";
  27. // List of string names and translation pairs.
  28. // The name is the first of each pair, and should not be translated.
  29. // The second of each pair is the translation.
  30. // Additional comments are sometimes given afterward to aid translations.
  31. list locstrings = [
  32. // Gestures (parameter for each is an avatar name)
  33. "gesture:handup", "{{0}} raises his/her hand.",
  34. "gesture:wave", "{{0}} waves.",
  35. "gesture:clap", "{{0}} claps.",
  36. "gesture:nodoff", "{{0}} falls asleep.",
  37. "gesture:huh", "{{0}} scratches his/her head.",
  38. "gesture:gotit", "{{0}} has got it!",
  39. "gesture:yes", "{{0}} nods his/her head.",
  40. "gesture:no", "{{0}} shakes his/her head.",
  41. // General
  42. "helpnotecardnotfound", "Cannot give help; notecard \"{{0}}\" not found in my inventory.", // Parameter: notecard name
  43. "channelmenu", "Currently using channel: {{0}}.\nPlease select which channel you would like to use.", // Parameter: current channel number
  44. "usingchannel", "Now using chat channel {{0}}.", // Parameter: channel number
  45. "visibilitymenu", "Please select which post visibility level you would like to use.\n\n{{0}} = public\n{{1}} = site\n{{2}} = private", // Parameters: button labels
  46. "visibility:public", "Now using public visibility. Anybody will be able to see your post in Moodle.",
  47. "visibility:site", "Now using site visibility. Only registered users will be able to see your post in Moodle.",
  48. "visibility:private", "Now using private visibility. Only you will be able to see your post in Moodle.",
  49. "chatsubject", "Please chat the subject line of your blog entry.",
  50. "cancelled", "Blog entry cancelled.",
  51. "chatbody", "Please chat the body text of your blog entry. You can use multiple chat messages, up to a total of {{0}} characters.", // Parameter: maximum number of characters
  52. "full", "Your blog entry is now full.",
  53. "charsleft", "{{0}} character(s) remaining.", // Parameter: number of characters
  54. "sending", "Thank you {{0}}. Please wait while your entry is sent...", // Parameter: avatar name
  55. "sent", "Updated blog entry successfully.",
  56. // Error messages
  57. "attachashud", "Error: please attach me as a HUD object.",
  58. "errorstate", "Initialisation failed. Please detach then re-attach the Toolbar to try again.",
  59. "timeout:subject", "Timeout waiting for blog subject. Cancelling blog entry.",
  60. "cannotsave:needsubjectbody", "Cannot save blog entry entry. Please enter the subject and body text first.",
  61. "needsubject", "The blog subject cannot be empty. Please enter it again.",
  62. "timeout:body", "Timeout waiting for blog body text. Cancelling blog entry.",
  63. "needbody", "Please enter some body text before saving your blog entry.",
  64. "atmaximum", "Sorry {{0}}. Your blog is already at the maximum length.", // Parameter: avatar name
  65. "toolong", "Sorry {{0}}. That message would make your blog entry too long. You only have {{1}} characters left", // Parameters: avatar name, number of characters
  66. "httperror", "ERROR: Failed to update blog due to HTTP request failure. You may try again or cancel.",
  67. "autherror", "Failed to update blog due to user authentication error {{0}}."
  68. ];
  69. ///// ----------- /////
  70. // Link message channels
  71. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  72. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  73. // Translation output methods
  74. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  75. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  76. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  77. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  78. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  79. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  80. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  81. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  82. string SLOODLE_TRANSLATE_LOAD_URL_PARALLEL = "loadurlpar"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  83. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  84. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  85. // Used for sending parallel URL loading messages
  86. integer SLOODLE_CHANNEL_OBJECT_LOAD_URL = -1639270041;
  87. ///// FUNCTIONS /////
  88. // Send a translation request link message
  89. // (Here for reference only)
  90. // Parameter: output_method = should identify an output method, as given by the "SLOODLE_TRANSLATE_..." constants above
  91. // Parameter: output_params = a list of parameters which controls the output, such as chat channel or buttons for a dialog
  92. // Parameter: string_name = the name of the localization string to output
  93. // Parameter: string_params = a list of parameters which will be included in the translated string (or an empty list if none)
  94. // Parameter: keyval = a key to send in the link message
  95. // Parameter: batch = the name of the localization batch which should handle this request
  96. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  97. {
  98. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  99. }
  100. // Send a translation response link message
  101. sloodle_translation_response(integer target, string name, string translation)
  102. {
  103. llMessageLinked(target, SLOODLE_CHANNEL_TRANSLATION_RESPONSE, name + "|" + translation + "|" + mybatch, NULL_KEY);
  104. }
  105. // Get the translation of a particular string
  106. string sloodle_get_string(string name)
  107. {
  108. // Attempt to find the string name
  109. integer numstrings = llGetListLength(locstrings);
  110. integer pos = llListFindList(locstrings, [name]);
  111. // IMPORTANT: we must be careful not to match a translations instead of a name.
  112. // If this was an even number, then we have a string name.
  113. if ((pos % 2) == 0) {
  114. // Make sure there is a subsequent translation in the list
  115. if ((pos + 1) < numstrings) return llList2String(locstrings, pos + 1);
  116. // The translation is not there
  117. pos = -1;
  118. }
  119. // If we could not find the string, then return a placeholder
  120. if (pos < 0) return "[[" + name + "]]";
  121. // If we got here, then we matched a translation instead of a string name.
  122. // As such, we need to resort to searching through the list manually (which can be very slow).
  123. // To saved time, we can start from the position just beyond where we got to.
  124. // We advance by 2 each time to skip the translations completely.
  125. //pos += 1;
  126. for (pos +=1 ; pos < numstrings; pos += 2) {
  127. // Do we have a match?
  128. if (llList2String(locstrings, pos) == name) {
  129. // Yes - make sure there is a translation following it
  130. if ((pos + 1) < numstrings) return llList2String(locstrings, pos + 1);
  131. // The translation is not there - skip the rest of the search
  132. pos = numstrings;
  133. }
  134. }
  135. // If we reached this point, then the string is not available. Return a placeholder.
  136. return "[[" + name + "]]";
  137. }
  138. // Send a debug link message
  139. sloodle_debug(string msg)
  140. {
  141. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  142. }
  143. // Get a formatted translation of a string
  144. string sloodle_get_string_f(string name, list params)
  145. {
  146. // Get the string itself
  147. string str = sloodle_get_string(name);
  148. // How many parameters do we have?
  149. integer numparams = llGetListLength(params);
  150. // Go through each parameter we have been provided
  151. integer curparamnum;
  152. string curparamtok = "{{x}}";
  153. integer curparamtoklength = 0;
  154. string curparamstr = "";
  155. integer tokpos = -1;
  156. // TW 06 Apr 09 Init updated
  157. for (curparamnum = 0; curparamnum < numparams; curparamnum++) {
  158. // Construct this parameter token
  159. curparamtok = "{{" + (string)(curparamnum) + "}}";
  160. curparamtoklength = llStringLength(curparamtok);
  161. // Fetch the parameter text
  162. curparamstr = llList2String(params, curparamnum);
  163. // Ensure the parameter text does NOT contain double braces (this avoids an infinite loop!)
  164. if (llSubStringIndex(curparamstr, "{{") < 0 && llSubStringIndex(curparamstr, "}}") < 0) {
  165. // Go through every instance of this parameter's token
  166. while ((tokpos = llSubStringIndex(str, curparamtok)) >= 0) {
  167. // Replace the token with the parameter string
  168. str = llDeleteSubString(str, tokpos, tokpos + curparamtoklength - 1);
  169. str = llInsertString(str, tokpos, curparamstr);
  170. }
  171. }
  172. }
  173. return str;
  174. }
  175. ///// STATES /////
  176. default
  177. {
  178. state_entry()
  179. {
  180. }
  181. link_message(integer sender_num, integer num, string str, key id)
  182. {
  183. // Check the channel
  184. if (num == SLOODLE_CHANNEL_TRANSLATION_REQUEST) {
  185. // // PROCESS REQUEST // //
  186. // Split the incoming message into fields
  187. list fields = llParseStringKeepNulls(str, ["|"], []);
  188. integer numfields = llGetListLength(fields);
  189. // Extract and check the localization batch
  190. string batch = "";
  191. if (numfields > 4) batch = llList2String(fields, 4);
  192. if (batch != mybatch) return;
  193. // We expect at least 3 fields
  194. // ... or 4 if there are insertion parameters...
  195. // ... anybody up for a 6th parameter? :)
  196. if (numfields < 3) {
  197. sloodle_debug("ERROR: Insufficient fields for translation of string.");
  198. return;
  199. }
  200. // Extract the key parts of the request
  201. string output_method = llList2String(fields, 0);
  202. list output_params = llCSV2List(llList2String(fields, 1));
  203. integer num_output_params = llGetListLength(output_params);
  204. string string_name = llList2String(fields, 2);
  205. list string_params = [];
  206. if (numfields > 3) {
  207. // Extract the string parameters (extra text added to the output)
  208. string string_param_text = llList2String(fields, 3);
  209. if (string_param_text != "") string_params = llCSV2List(string_param_text);
  210. }
  211. // // TRANSLATE STRING // //
  212. // This string will store the translation
  213. string trans = "";
  214. // Do nothing if the string name is empty
  215. if (string_name != "") {
  216. // If there are no string parameters, then it is only a basic translation
  217. if (llGetListLength(string_params) == 0) {
  218. // Get the basic translation
  219. trans = sloodle_get_string(string_name);
  220. } else {
  221. // Construct the formatted string
  222. trans = sloodle_get_string_f(string_name, string_params);
  223. }
  224. }
  225. // // OUTPUT STRING // //
  226. // Check what output method has been requested
  227. if (output_method == SLOODLE_TRANSLATE_LINK) {
  228. // Return the string via link message
  229. sloodle_translation_response(sender_num, string_name, trans);
  230. } else if (output_method == SLOODLE_TRANSLATE_SAY) {
  231. // Say the string
  232. if (num_output_params > 0) llSay(llList2Integer(output_params, 0), trans);
  233. else sloodle_debug("ERROR: Insufficient output parameters to say string \"" + string_name + "\".");
  234. } else if (output_method == SLOODLE_TRANSLATE_WHISPER) {
  235. // Whisper the string
  236. if (num_output_params > 0) llWhisper(llList2Integer(output_params, 0), trans);
  237. else sloodle_debug("ERROR: Insufficient output parameters to whisper string \"" + string_name + "\".");
  238. } else if (output_method == SLOODLE_TRANSLATE_SHOUT) {
  239. // Shout the string
  240. if (num_output_params > 0) llShout(llList2Integer(output_params, 0), trans);
  241. else sloodle_debug("ERROR: Insufficient output parameters to shout string \"" + string_name + "\".");
  242. } else if (output_method == SLOODLE_TRANSLATE_REGION_SAY) {
  243. // RegionSay the string
  244. if (num_output_params > 0) llRegionSay(llList2Integer(output_params, 0), trans);
  245. else sloodle_debug("ERROR: Insufficient output parameters to region-say string \"" + string_name + "\".");
  246. } else if (output_method == SLOODLE_TRANSLATE_OWNER_SAY) {
  247. // Ownersay the string
  248. llOwnerSay(trans);
  249. } else if (output_method == SLOODLE_TRANSLATE_DIALOG) {
  250. // Display a dialog - we need a valid key
  251. if (id == NULL_KEY) {
  252. sloodle_debug("ERROR: Non-null key value required to show dialog with string \"" + string_name + "\".");
  253. return;
  254. }
  255. // We need at least 2 additional output parameters (channel, and at least 1 button)
  256. if (num_output_params >= 2) {
  257. // Extract the channel number
  258. integer channel = llList2Integer(output_params, 0);
  259. // Extract up to 12 button values
  260. list buttons = llList2List(output_params, 1, 12);
  261. // Display the dialog
  262. llDialog(id, trans, buttons, channel);
  263. } else sloodle_debug("ERROR: Insufficient output parameters to show dialog with string \"" + string_name + "\".");
  264. } else if (output_method == SLOODLE_TRANSLATE_LOAD_URL) {
  265. // Display a URL - we need a valid key
  266. if (id == NULL_KEY) {
  267. sloodle_debug("ERROR: Non-null key value required to load URL with string \"" + string_name + "\".");
  268. return;
  269. }
  270. // We need 1 additional parameter, containing the URL to load
  271. if (num_output_params >= 1) llLoadURL(id, trans, llList2String(output_params, 0));
  272. else sloodle_debug("ERROR: Insufficient output parameters to load URL with string \"" + string_name + "\".");
  273. } else if (output_method == SLOODLE_TRANSLATE_LOAD_URL_PARALLEL) {
  274. // Display a URL - we need a valid key
  275. if (id == NULL_KEY) {
  276. sloodle_debug("ERROR: Non-null key value required to load URL with string \"" + string_name + "\".");
  277. return;
  278. }
  279. // NOTE: currently the parallel URL loaders will drop any text.
  280. // TODO: make parallel URL loaders use text as well.
  281. // We need 1 additional parameter, containing the URL to load
  282. if (num_output_params >= 1) llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_LOAD_URL, llList2String(output_params, 0), id);
  283. else sloodle_debug("ERROR: Insufficient output parameters to load URL with string \"" + string_name + "\".");
  284. } else if (output_method == SLOODLE_TRANSLATE_HOVER_TEXT) {
  285. // We need 1 additional parameter, containing the URL to load
  286. if (num_output_params >= 2) llSetText(trans, (vector)llList2String(output_params, 0), (float)llList2String(output_params, 1));
  287. else sloodle_debug("ERROR: Insufficient output parameters to show hover text with string \"" + string_name + "\".");
  288. } else if (output_method == SLOODLE_TRANSLATE_IM) {
  289. // Send an IM - we need a valid key
  290. if (id == NULL_KEY) {
  291. sloodle_debug("ERROR: Non-null key value required to send IM with string \"" + string_name + "\".");
  292. return;
  293. }
  294. // Send the IM
  295. llInstantMessage(id, trans);
  296. } else {
  297. // Don't know the output method
  298. sloodle_debug("ERROR: unrecognised output method \"" + output_method + "\".");
  299. }
  300. }
  301. }
  302. }
  303. // Please leave the following line intact to show where the script lives in Git:
  304. // SLOODLE LSL Script Git Location: mod/toolbar_giver-1.0/objects/default/assets/lang/en/sloodle_translation_toolbar_en.lslp