Generic way to translate $ngBootbox messages with $translate service -


i'm using $ngbootbox module display alerts , confirm dialogs. also, i'm using angular-translate module translate string resources.

i wanted generic way implement these translations shown in dialogs avoid repetitive , dirty code follows:

$scope.displaymsg = function(){     $translate('message').then(function(translated_msg){         $ngbootbox.alert(translated_msg);     }); } 

i wanted share 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 delegated method $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 $translate string resource:

$scope.displaymsg = function(){     $ngbootbox.alert('my_string_resource'); } 

also can use custom text:

$scope.displaymsg = function(){     $ngbootbox.alert('my custom not_translated message!'); } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -