Created
June 23, 2016 08:07
-
-
Save anonymous/e2d5af30a0832d6fd143ed159ae0b7a7 to your computer and use it in GitHub Desktop.
This file contains 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
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