9810b277-c08d-5d66-39bd-ef25b6a1f056_script.lsl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // Note: where a translation string contains {{x}} (where x is a number),
  17. // it means that a parameter can be inserted. Please make sure to include these
  18. // parameters in the appropriate location in your translation.
  19. // It may be sensible to add comments after your string to indicate what its parameters mean.
  20. // NOTE: parameter numbering starts at 0 (unlike previous versions, which started at 1).
  21. // Translations can be requested by sending a link message on the SLOODLE_CHANNEL_TRANSLATION_REQUEST channel.
  22. // It is advisable simply to use the "sloodle_translation_request" function provided in this script.
  23. ///// TRANSLATION /////
  24. // Localization batch - indicates the purpose of this file
  25. string mybatch = "webintercom";
  26. // List of string names and translation pairs.
  27. // The name is the first of each pair, and should not be translated.
  28. // The second of each pair is the translation.
  29. // Additional comments are sometimes given afterward to aid translations.
  30. list locstrings = [
  31. // WebIntercom
  32. "webintercom:startedrecording", "Started recording {{0}}", // Parameter: name of an avatar
  33. "webintercom:stoppedrecording", "Stopped recording {{0}}",
  34. "webintercom:alreadyrecording", "Already recording {{0}}",
  35. "webintercom:notrecording", "Not recording {{0}}",
  36. "webintercom:recording", "Recording:\n{{0}}",
  37. "webintercom:chatloggingon", "Chat logging is on!",
  38. "webintercom:joinchat", "Join this Moodle chat at {{0}}", // Parameter should be link to Moodle chatroom
  39. "webintercom:anouncechatroom", "An archive of this session can be found at {{0}}", // Parameter should be link to Moodle chatroom
  40. "webintercom:enterchatroom", "You can access the Web Chat room at {{0}}", // Parameter should be link to Moodle chatroom
  41. "webintercom:touchtorecord", "Touch logger to record your chat",
  42. "webintercom:ctrlmenu", "Would you like to activate the WebIntercom?\n\n{{0}} = No\n{{1}} = Yes", // Parameters should give the button labels for NO and YES options
  43. "webintercom:usemenu", "What would you like to do?\n\n{{0}} = Stop recording me\n{{1}} = Record me\n{{2}} = Go to Web Chatroom", // Parameters: stop and start button labels
  44. "webintercom:usectrlmenu", "What would you like to do?\n\n{{0}} = Stop recording me\n{{1}} = Record me\n{{2}} = Deactivate WebIntercom\n{{3}} = Go to Web Chatroom", // Parameters: stop, start, and deactivate button labels
  45. "webintercom:autodeactivate", "Deactivating due to lack of nearby users"
  46. ];
  47. ///// ----------- /////
  48. // Link message channels
  49. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  50. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  51. // Translation output methods
  52. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  53. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  54. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  55. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  56. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  57. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  58. 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.
  59. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  60. string SLOODLE_TRANSLATE_LOAD_URL_PARALLEL = "loadurlpar"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  61. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  62. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  63. // Used for sending parallel URL loading messages
  64. integer SLOODLE_CHANNEL_OBJECT_LOAD_URL = -1639270041;
  65. ///// FUNCTIONS /////
  66. // Send a translation request link message
  67. // (Here for reference only)
  68. // Parameter: output_method = should identify an output method, as given by the "SLOODLE_TRANSLATE_..." constants above
  69. // Parameter: output_params = a list of parameters which controls the output, such as chat channel or buttons for a dialog
  70. // Parameter: string_name = the name of the localization string to output
  71. // Parameter: string_params = a list of parameters which will be included in the translated string (or an empty list if none)
  72. // Parameter: keyval = a key to send in the link message
  73. // Parameter: batch = the name of the localization batch which should handle this request
  74. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  75. {
  76. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  77. }
  78. // Send a translation response link message
  79. sloodle_translation_response(integer target, string name, string translation)
  80. {
  81. llMessageLinked(target, SLOODLE_CHANNEL_TRANSLATION_RESPONSE, name + "|" + translation + "|" + mybatch, NULL_KEY);
  82. }
  83. // Get the translation of a particular string
  84. string sloodle_get_string(string name)
  85. {
  86. // Attempt to find the string name
  87. integer numstrings = llGetListLength(locstrings);
  88. integer pos = llListFindList(locstrings, [name]);
  89. // IMPORTANT: we must be careful not to match a translations instead of a name.
  90. // If this was an even number, then we have a string name.
  91. if ((pos % 2) == 0) {
  92. // Make sure there is a subsequent translation in the list
  93. if ((pos + 1) < numstrings) return llList2String(locstrings, pos + 1);
  94. // The translation is not there
  95. pos = -1;
  96. }
  97. // If we could not find the string, then return a placeholder
  98. if (pos < 0) return "[[" + name + "]]";
  99. // If we got here, then we matched a translation instead of a string name.
  100. // As such, we need to resort to searching through the list manually (which can be very slow).
  101. // To saved time, we can start from the position just beyond where we got to.
  102. // We advance by 2 each time to skip the translations completely.
  103. pos += 1;
  104. for (; pos < numstrings; pos += 2) {
  105. // Do we have a match?
  106. if (llList2String(locstrings, pos) == name) {
  107. // Yes - make sure there is a translation following it
  108. if ((pos + 1) < numstrings) return llList2String(locstrings, pos + 1);
  109. // The translation is not there - skip the rest of the search
  110. pos = numstrings;
  111. }
  112. }
  113. // If we reached this point, then the string is not available. Return a placeholder.
  114. return "[[" + name + "]]";
  115. }
  116. // Send a debug link message
  117. sloodle_debug(string msg)
  118. {
  119. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  120. }
  121. // Get a formatted translation of a string
  122. string sloodle_get_string_f(string name, list params)
  123. {
  124. // Get the string itself
  125. string str = sloodle_get_string(name);
  126. // How many parameters do we have?
  127. integer numparams = llGetListLength(params);
  128. // Go through each parameter we have been provided
  129. integer curparamnum = 0;
  130. string curparamtok = "{{x}}";
  131. integer curparamtoklength = 0;
  132. string curparamstr = "";
  133. integer tokpos = -1;
  134. for (; curparamnum < numparams; curparamnum++) {
  135. // Construct this parameter token
  136. curparamtok = "{{" + (string)(curparamnum) + "}}";
  137. curparamtoklength = llStringLength(curparamtok);
  138. // Fetch the parameter text
  139. curparamstr = llList2String(params, curparamnum);
  140. // Ensure the parameter text does NOT contain double braces (this avoids an infinite loop!)
  141. if (llSubStringIndex(curparamstr, "{{") < 0 && llSubStringIndex(curparamstr, "}}") < 0) {
  142. // Go through every instance of this parameter's token
  143. while ((tokpos = llSubStringIndex(str, curparamtok)) >= 0) {
  144. // Replace the token with the parameter string
  145. str = llDeleteSubString(str, tokpos, tokpos + curparamtoklength - 1);
  146. str = llInsertString(str, tokpos, curparamstr);
  147. }
  148. }
  149. }
  150. return str;
  151. }
  152. ///// STATES /////
  153. default
  154. {
  155. state_entry()
  156. {
  157. }
  158. link_message(integer sender_num, integer num, string str, key id)
  159. {
  160. // Check the channel
  161. if (num == SLOODLE_CHANNEL_TRANSLATION_REQUEST) {
  162. // // PROCESS REQUEST // //
  163. // Split the incoming message into fields
  164. list fields = llParseStringKeepNulls(str, ["|"], []);
  165. integer numfields = llGetListLength(fields);
  166. // Extract and check the localization batch
  167. string batch = "";
  168. if (numfields > 4) batch = llList2String(fields, 4);
  169. if (batch != mybatch) return;
  170. // We expect at least 3 fields
  171. // ... or 4 if there are insertion parameters...
  172. // ... or 5 if there is a language code
  173. // ... anybody up for a 6th parameter? :)
  174. if (numfields < 3) {
  175. sloodle_debug("ERROR: Insufficient fields for translation of string.");
  176. return;
  177. }
  178. // Extract the key parts of the request
  179. string output_method = llList2String(fields, 0);
  180. list output_params = llCSV2List(llList2String(fields, 1));
  181. integer num_output_params = llGetListLength(output_params);
  182. string string_name = llList2String(fields, 2);
  183. list string_params = [];
  184. if (numfields > 3) {
  185. // Extract the string parameters (extra text added to the output)
  186. string string_param_text = llList2String(fields, 3);
  187. if (string_param_text != "") string_params = llCSV2List(string_param_text);
  188. }
  189. // // TRANSLATE STRING // //
  190. // This string will store the translation
  191. string trans = "";
  192. // Do nothing if the string name is empty
  193. if (string_name != "") {
  194. // If there are no string parameters, then it is only a basic translation
  195. if (llGetListLength(string_params) == 0) {
  196. // Get the basic translation
  197. trans = sloodle_get_string(string_name);
  198. } else {
  199. // Construct the formatted string
  200. trans = sloodle_get_string_f(string_name, string_params);
  201. }
  202. }
  203. // // OUTPUT STRING // //
  204. // Check what output method has been requested
  205. if (output_method == SLOODLE_TRANSLATE_LINK) {
  206. // Return the string via link message
  207. sloodle_translation_response(sender_num, string_name, trans);
  208. } else if (output_method == SLOODLE_TRANSLATE_SAY) {
  209. // Say the string
  210. if (num_output_params > 0) llSay(llList2Integer(output_params, 0), trans);
  211. else sloodle_debug("ERROR: Insufficient output parameters to say string \"" + string_name + "\".");
  212. } else if (output_method == SLOODLE_TRANSLATE_WHISPER) {
  213. // Whisper the string
  214. if (num_output_params > 0) llWhisper(llList2Integer(output_params, 0), trans);
  215. else sloodle_debug("ERROR: Insufficient output parameters to whisper string \"" + string_name + "\".");
  216. } else if (output_method == SLOODLE_TRANSLATE_SHOUT) {
  217. // Shout the string
  218. if (num_output_params > 0) llShout(llList2Integer(output_params, 0), trans);
  219. else sloodle_debug("ERROR: Insufficient output parameters to shout string \"" + string_name + "\".");
  220. } else if (output_method == SLOODLE_TRANSLATE_REGION_SAY) {
  221. // RegionSay the string
  222. if (num_output_params > 0) llRegionSay(llList2Integer(output_params, 0), trans);
  223. else sloodle_debug("ERROR: Insufficient output parameters to region-say string \"" + string_name + "\".");
  224. } else if (output_method == SLOODLE_TRANSLATE_OWNER_SAY) {
  225. // Ownersay the string
  226. llOwnerSay(trans);
  227. } else if (output_method == SLOODLE_TRANSLATE_DIALOG) {
  228. // Display a dialog - we need a valid key
  229. if (id == NULL_KEY) {
  230. sloodle_debug("ERROR: Non-null key value required to show dialog with string \"" + string_name + "\".");
  231. return;
  232. }
  233. // We need at least 2 additional output parameters (channel, and at least 1 button)
  234. if (num_output_params >= 2) {
  235. // Extract the channel number
  236. integer channel = llList2Integer(output_params, 0);
  237. // Extract up to 12 button values
  238. list buttons = llList2List(output_params, 1, 12);
  239. // Display the dialog
  240. llDialog(id, trans, buttons, channel);
  241. } else sloodle_debug("ERROR: Insufficient output parameters to show dialog with string \"" + string_name + "\".");
  242. } else if (output_method == SLOODLE_TRANSLATE_LOAD_URL) {
  243. // Display a URL - we need a valid key
  244. if (id == NULL_KEY) {
  245. sloodle_debug("ERROR: Non-null key value required to load URL with string \"" + string_name + "\".");
  246. return;
  247. }
  248. // We need 1 additional parameter, containing the URL to load
  249. if (num_output_params >= 1) llLoadURL(id, trans, llList2String(output_params, 0));
  250. else sloodle_debug("ERROR: Insufficient output parameters to load URL with string \"" + string_name + "\".");
  251. } else if (output_method == SLOODLE_TRANSLATE_LOAD_URL_PARALLEL) {
  252. // Display a URL - we need a valid key
  253. if (id == NULL_KEY) {
  254. sloodle_debug("ERROR: Non-null key value required to load URL with string \"" + string_name + "\".");
  255. return;
  256. }
  257. // NOTE: currently the parallel URL loaders will drop any text.
  258. // TODO: make parallel URL loaders use text as well.
  259. // We need 1 additional parameter, containing the URL to load
  260. if (num_output_params >= 1) llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_LOAD_URL, llList2String(output_params, 0), id);
  261. else sloodle_debug("ERROR: Insufficient output parameters to load URL with string \"" + string_name + "\".");
  262. } else if (output_method == SLOODLE_TRANSLATE_HOVER_TEXT) {
  263. // We need 1 additional parameter, containing the URL to load
  264. if (num_output_params >= 2) llSetText(trans, (vector)llList2String(output_params, 0), (float)llList2String(output_params, 1));
  265. else sloodle_debug("ERROR: Insufficient output parameters to show hover text with string \"" + string_name + "\".");
  266. } else if (output_method == SLOODLE_TRANSLATE_IM) {
  267. // Send an IM - we need a valid key
  268. if (id == NULL_KEY) {
  269. sloodle_debug("ERROR: Non-null key value required to send IM with string \"" + string_name + "\".");
  270. return;
  271. }
  272. // Send the IM
  273. llInstantMessage(id, trans);
  274. } else {
  275. // Don't know the output method
  276. sloodle_debug("ERROR: unrecognised output method \"" + output_method + "\".");
  277. }
  278. }
  279. }
  280. }
  281. // Please leave the following line intact to show where the script lives in Git:
  282. // SLOODLE LSL Script Git Location: mod/chat-1.0/objects/default/assets/lang/en/sloodle_translation_webintercom_en.lslp