Skip to content

Commit

Permalink
Merge pull request #24 from owncloud/js_variable_interpolation
Browse files Browse the repository at this point in the history
A suggestion for simple, yet efficient variable interpolation in js translations
  • Loading branch information
DeepDiver1975 committed Oct 10, 2012
2 parents fce6ce9 + bb34577 commit a0c5361
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @return string
*/

function t(app,text){
function t(app,text, vars){
if( !( t.cache[app] )){
$.ajax(OC.filePath('core','ajax','translations.php'),{
async:false,//todo a proper sollution for this without sync ajax calls
Expand All @@ -21,11 +21,27 @@ function t(app,text){
t.cache[app] = [];
}
}
var _build = function(text, vars) {
return text.replace(/{([^{}]*)}/g,
function (a, b) {
var r = vars[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
}
if( typeof( t.cache[app][text] ) !== 'undefined' ){
return t.cache[app][text];
if(typeof vars === 'object') {
return _build(t.cache[app][text], vars);
} else {
return t.cache[app][text];
}
}
else{
return text;
if(typeof vars === 'object') {
return _build(text, vars);
} else {
return text;
}
}
}
t.cache={};
Expand Down

0 comments on commit a0c5361

Please sign in to comment.