mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API New JS sprintf and inject replacement functions
Fix sprintf issues when replacement was at start of string + introduce new inject function using injection map similar to the PHP version of _t()
This commit is contained in:
parent
89333d575d
commit
8fb5e9c3a4
@ -375,10 +375,16 @@ format which can be processed more easily by external translation providers (see
|
|||||||
alert(ss.i18n._t('MYMODULE.MYENTITY'));
|
alert(ss.i18n._t('MYMODULE.MYENTITY'));
|
||||||
|
|
||||||
|
|
||||||
### Advanced Usage with sprintf()
|
### Advanced Use
|
||||||
|
|
||||||
|
The `ss.i18n` object contain a couple functions to help and replace dynamic variable from within a string.
|
||||||
|
|
||||||
|
#### Legacy sequential replacement with sprintf()
|
||||||
|
|
||||||
|
`sprintf()` will substitute occurencies of `%s` in the main string with each of the following arguments passed to the function. The substitution is done sequentially.
|
||||||
|
|
||||||
:::js
|
:::js
|
||||||
// MYMODULE.MYENTITY contains "Really delete %s articles by %s authors?"
|
// MYMODULE.MYENTITY contains "Really delete %s articles by %s?"
|
||||||
alert(ss.i18n.sprintf(
|
alert(ss.i18n.sprintf(
|
||||||
ss.i18n._t('MYMODULE.MYENTITY'),
|
ss.i18n._t('MYMODULE.MYENTITY'),
|
||||||
42,
|
42,
|
||||||
@ -387,6 +393,19 @@ format which can be processed more easily by external translation providers (see
|
|||||||
// Displays: "Really delete 42 articles by Douglas Adams?"
|
// Displays: "Really delete 42 articles by Douglas Adams?"
|
||||||
|
|
||||||
|
|
||||||
|
#### Variable injection with inject()
|
||||||
|
|
||||||
|
`inject()` will substitute variables in the main string like `{myVar}` by the keys in the object passed as second argument. Each variable can be in any order and appear multiple times.
|
||||||
|
|
||||||
|
:::js
|
||||||
|
// MYMODULE.MYENTITY contains "Really delete {count} articles by {author}?"
|
||||||
|
alert(ss.i18n.inject(
|
||||||
|
ss.i18n._t('MYMODULE.MYENTITY'),
|
||||||
|
{count: 42, author: 'Douglas Adams'}
|
||||||
|
));
|
||||||
|
// Displays: "Really delete 42 articles by Douglas Adams?"
|
||||||
|
|
||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
* No detecting/conversion of character encodings (we rely fully on UTF-8)
|
* No detecting/conversion of character encodings (we rely fully on UTF-8)
|
||||||
|
@ -138,33 +138,50 @@ ss.i18n = {
|
|||||||
return stripStr(parts.join(" "));
|
return stripStr(parts.join(" "));
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* printf()
|
* Substitutes %s with parameters
|
||||||
* C-printf like function, which substitutes %s with parameters
|
|
||||||
* given in list. %%s is used to escape %s.
|
* given in list. %%s is used to escape %s.
|
||||||
*
|
*
|
||||||
* Doesn't work in IE5.0 (splice)
|
* @param string S : The string to perform the substitutions on.
|
||||||
*
|
* @return string The new string with substitutions made
|
||||||
* @param string S : string to perform printf on.
|
|
||||||
* @param string L : Array of arguments for printf()
|
|
||||||
*/
|
*/
|
||||||
sprintf: function(S) {
|
sprintf: function(S) {
|
||||||
if (arguments.length == 1) return S;
|
if (arguments.length == 1) return S;
|
||||||
|
|
||||||
var nS = "";
|
var args = [],
|
||||||
var tS = S.split("%s");
|
len = arguments.length,
|
||||||
|
index = 0,
|
||||||
|
regx = new RegExp('(.?)(%s)', 'g'),
|
||||||
|
result;
|
||||||
|
|
||||||
var args = [];
|
for (var i=1; i<len; ++i) {
|
||||||
for (var i=1, len = arguments.length; i <len; ++i) {
|
|
||||||
args.push(arguments[i]);
|
args.push(arguments[i]);
|
||||||
};
|
};
|
||||||
|
|
||||||
for(var i=0; i<args.length; i++) {
|
result = S.replace(regx, function(match, subMatch1, subMatch2, offset, string){
|
||||||
if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != args.length-1)
|
if (subMatch1 == '%') return match; // skip %%s
|
||||||
tS[i] += "s"+tS.splice(i+1,1)[0];
|
return subMatch1 + args[index++];
|
||||||
nS += tS[i] + args[i];
|
});
|
||||||
}
|
|
||||||
return nS + tS[tS.length-1];
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitutes variables with a list of injections.
|
||||||
|
*
|
||||||
|
* @param string S : The string to perform the substitutions on.
|
||||||
|
* @param object map : An object with the substitions map e.g. {var: value}
|
||||||
|
* @return string The new string with substitutions made
|
||||||
|
*/
|
||||||
|
inject: function(S, map) {
|
||||||
|
var regx = new RegExp("\{([A-Za-z0-9_]*)\}", "g"),
|
||||||
|
result;
|
||||||
|
|
||||||
|
result = S.replace(regx, function(match, key, offset, string){
|
||||||
|
return (map[key]) ? map[key] : match;
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,20 +18,33 @@ ss.i18n = {
|
|||||||
sprintf: function(S) {
|
sprintf: function(S) {
|
||||||
if (arguments.length == 1) return S;
|
if (arguments.length == 1) return S;
|
||||||
|
|
||||||
var nS = "";
|
var args = [],
|
||||||
var tS = S.split("%s");
|
len = arguments.length,
|
||||||
|
index = 0,
|
||||||
|
regx = new RegExp('(.?)(%s)', 'g'),
|
||||||
|
result;
|
||||||
|
|
||||||
var args = [];
|
for (var i=1; i<len; ++i) {
|
||||||
for (var i=1, len = arguments.length; i <len; ++i) {
|
|
||||||
args.push(arguments[i]);
|
args.push(arguments[i]);
|
||||||
};
|
};
|
||||||
|
|
||||||
for(var i=0; i<args.length; i++) {
|
result = S.replace(regx, function(match, subMatch1, subMatch2, offset, string){
|
||||||
if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != args.length-1)
|
if (subMatch1 == '%') return match; // skip %%s
|
||||||
tS[i] += "s"+tS.splice(i+1,1)[0];
|
return subMatch1 + args[index++];
|
||||||
nS += tS[i] + args[i];
|
});
|
||||||
}
|
|
||||||
return nS + tS[tS.length-1];
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
inject: function(S, map) {
|
||||||
|
var regx = new RegExp("\{([A-Za-z0-9_]*)\}", "g"),
|
||||||
|
result;
|
||||||
|
|
||||||
|
result = S.replace(regx, function(match, key, offset, string){
|
||||||
|
return (map[key]) ? map[key] : match;
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
// stub methods
|
// stub methods
|
||||||
|
Loading…
Reference in New Issue
Block a user