diff --git a/lib/javascript-static.js b/lib/javascript-static.js index e356e3945af..7d151f8425a 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -646,6 +646,72 @@ M.util.init_block_hider = function(Y, config) { }); }; +/** + * Returns a string registered in advance for usage in JavaScript + * + * If you do not pass the third parameter, the function will just return + * the corresponding value from the M.str object. If the third parameter is + * provided, the function performs {$a} placeholder substitution in the + * same way as PHP get_string() in Moodle does. + * + * @param {String} identifier string identifier + * @param {String} component the component providing the string + * @param {Object|String} a optional variable to populate placeholder with + */ +M.util.get_string = function(identifier, component, a) { + var stringvalue; + + if (M.cfg.developerdebug) { + // creating new instance if YUI is not optimal but it seems to be better way then + // require the instance via the function API - note that it is used in rare cases + // for debugging only anyway + var Y = new YUI({ debug : true }); + } + + if (!M.str.hasOwnProperty(component) || !M.str[component].hasOwnProperty(identifier)) { + stringvalue = '[[' + identifier + ',' + component + ']]'; + if (M.cfg.developerdebug) { + Y.log('undefined string ' + stringvalue, 'warn', 'M.util.get_string'); + } + return stringvalue; + } + + stringvalue = M.str[component][identifier]; + + if (typeof a == 'undefined') { + // no placeholder substitution requested + return stringvalue; + } + + if (typeof a == 'number' || typeof a == 'string') { + // replace all occurrences of {$a} with the placeholder value + stringvalue = stringvalue.replace(/\{\$a\}/g, a); + return stringvalue; + } + + if (typeof a == 'object') { + // replace {$a->key} placeholders + for (var key in a) { + if (typeof a[key] != 'number' && typeof a[key] != 'string') { + if (M.cfg.developerdebug) { + Y.log('invalid value type for $a->' + key, 'warn', 'M.util.get_string'); + } + continue; + } + var search = '{$a->' + key + '}'; + search = search.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + search = new RegExp(search, 'g'); + stringvalue = stringvalue.replace(search, a[key]); + } + return stringvalue; + } + + if (M.cfg.developerdebug) { + Y.log('incorrect placeholder type', 'warn', 'M.util.get_string'); + } + return stringvalue; +}; + //=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code === function popupchecker(msg) { diff --git a/version.php b/version.php index 80845cc8f19..19efacd5f0d 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2010110200; // YYYYMMDD = date of the last version bump +$version = 2010110201; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 RC1 (Build: 20101102)'; // Human-friendly version name