Skip to content

Instantly share code, notes, and snippets.

@cvgellhorn
Last active December 3, 2015 17:41
Show Gist options
  • Save cvgellhorn/ef9c8f8da625f19721fb to your computer and use it in GitHub Desktop.
Save cvgellhorn/ef9c8f8da625f19721fb to your computer and use it in GitHub Desktop.
/*!
* 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