MDL-25015 get_string() logic implementation in javascript
The new function allows to substitute {$a} placeholders on client side.
Version bump needed to clear javascript caches.
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user