05b3af22-0fe6-7bad-0a45-00f8b21eebae_script.lsl 17 KB

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