I'm using $ngBootbox module to display alerts and confirm dialogs. Also, I'm using angular-translate module to translate my string resources.
I wanted a generic way to implement these translations shown in dialogs to avoid repetitive and dirty code as follows:
$scope.displayMsg = function(){
$translate('message').then(function(translated_msg){
$ngBootbox.alert(translated_msg);
});
}
I wanted to share this solution using $provide.decorator
app.config(function($provide){
$provide.decorator('$ngBootbox', function($delegate, $translate, $q){
return angular.extend($delegate, {
alert: customMessageHandler($delegate.alert),
confirm: customMessageHandler($delegate.confirm),
prompt: customMessageHandler($delegate.prompt)
});
//Receives the delegated method from $ngBootbox
function customMessageHandler(method){
return function(message){
var deferred = $q.defer();
$translate(message).then(function(translated_message){
deferred.resolve(method(translated_message));
}, function(){
deferred.resolve(method(message));
});
return deferred.promise;
};
}
});
});
Example of use with $translate string resource:
$scope.displayMsg = function(){
$ngBootbox.alert('my_string_resource');
}
Also I can use custom text:
$scope.displayMsg = function(){
$ngBootbox.alert('My custom not_translated message!');
}