Start by restructuring your logEntry to highlight interpolation
var translationId = 'Log.' + msg.context.entity_type) + '.' + msg.context.action; var interpolateParams = { 'object_name': msg.context.object_name, 'user': msg.context.user_name }; var translated = $translate(translationId, interpolateParams); return $sce.trustAsHtml(translated);
You want to avoid all the HTML from interpolateParams , but leave the HTML in your translation templates. Use this code to copy an object, iterate over its values, and replace it with escaped HTML.
var safeParams = angular.copy(interpolateParams); angular.forEach(safeParams, function(value, key, obj) { obj[key] = encodeEntities(value)
Finally, the encodeEntities angular function is not displayed, so we had to borrow the source from angular -sanitize.js
var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, // Match everything outside of normal chars and " (quote character) NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g; function encodeEntities(value) { return value. replace(/&/g, '&'). replace(SURROGATE_PAIR_REGEXP, function(value) { var hi = value.charCodeAt(0); var low = value.charCodeAt(1); return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';'; }). replace(NON_ALPHANUMERIC_REGEXP, function(value) { return '&#' + value.charCodeAt(0) + ';'; }). replace(/</g, '<'). replace(/>/g, '>'); }
Update: After upgrading to angular -translate 2.7.0 this message appeared:
pascalprecht.translate. $ translateSanitization: Without sanitation, a strategy has been set up. This can have serious safety consequences. See http://angular-translate.imtqy.com/docs/#/guide/19_security for details.
Sp instead of the trustlate answer above, angular -translate can accomplish the same result with:
$translateProvider.useSanitizeValueStrategy('escapeParameters');
See docs for more effective security strategies for more information.