Merge pull request #24 from owncloud/js_variable_interpolation

A suggestion for simple, yet efficient variable interpolation in js translations
This commit is contained in:
Thomas Müller 2012-10-10 00:40:47 -07:00
commit a0c53619ff
1 changed files with 19 additions and 3 deletions

View File

@ -5,7 +5,7 @@
* @return string * @return string
*/ */
function t(app,text){ function t(app,text, vars){
if( !( t.cache[app] )){ if( !( t.cache[app] )){
$.ajax(OC.filePath('core','ajax','translations.php'),{ $.ajax(OC.filePath('core','ajax','translations.php'),{
async:false,//todo a proper sollution for this without sync ajax calls async:false,//todo a proper sollution for this without sync ajax calls
@ -21,13 +21,29 @@ function t(app,text){
t.cache[app] = []; 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' ){ if( typeof( t.cache[app][text] ) !== 'undefined' ){
if(typeof vars === 'object') {
return _build(t.cache[app][text], vars);
} else {
return t.cache[app][text]; return t.cache[app][text];
} }
}
else{ else{
if(typeof vars === 'object') {
return _build(text, vars);
} else {
return text; return text;
} }
} }
}
t.cache={}; t.cache={};
/** /**