Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2016 08:07
Show Gist options
  • Save anonymous/e2d5af30a0832d6fd143ed159ae0b7a7 to your computer and use it in GitHub Desktop.
Save anonymous/e2d5af30a0832d6fd143ed159ae0b7a7 to your computer and use it in GitHub Desktop.
var i18nModule = angular.module('i18nModule', []);
i18nModule.factory('i18n', function() {
var tokenRE = /\$\{(?:(?:(\d+)|([a-z_][\w\-]*))(?::([a-z_\.]+)(?:\(([^\)]*?)?\))?)?)\}/gi;
var evaluate = function(text, values) {
var fn = function(match, index, name, formatFn, args) {
if (name === null || name === '') {
name = index;
}
if (values[name]) {
return values[name];
}
return match;
};
return text.replace(tokenRE, fn);
};
return {
translate: function(msgId, params) {
var ret = msgId;
if (TRANSLATIONS[msgId]) {
ret = TRANSLATIONS[msgId];
}
if (params) {
ret = evaluate(ret, params);
}
return ret;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment