Last active
December 3, 2015 17:41
-
-
Save cvgellhorn/ef9c8f8da625f19721fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Translator util | |
*/ | |
var Translator = { | |
data: {}, | |
__: function() { | |
var args = (arguments.length === 1) ? arguments[0] : arguments; | |
if (args[1] && typeof args[1] === 'object') { | |
return this.template(args[0], args[1]); | |
} else { | |
return this.sprintf.apply(this, args); | |
} | |
}, | |
sprintf: function() { | |
var args = Array.prototype.slice.call(arguments); | |
return args.shift().replace(/%s/g, function() { | |
return args.shift(); | |
}); | |
}, | |
template: function(text, value) { | |
for (var p in value) { | |
text = text.replace(new RegExp('{' + p + '}', 'g'), value[p]); | |
} | |
return text; | |
} | |
}; | |
/** | |
* Translator wrapper function | |
* | |
* @returns {string} | |
*/ | |
function __() { | |
return Translator.__(arguments); | |
} | |
// Optional | |
window.__ = Translator.__.bind(Translator); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment