broadcast translate event
It would be nice if we could selectively tell translate filter (or directive) to broadcast when a translation occurs (code below is just a (bad?) example).
angular.module('pascalprecht.translate').filter('translate', [
'$parse',
'$translate',
'$rootScope',
function ($parse, $translate, $rootScope) {
return function (translationId, interpolateParams, interpolation) {
if (interpolateParams === 'broadcast') {
$rootScope.$broadcast('$translationCalled');
}
if (!angular.isObject(interpolateParams)) {
interpolateParams = $parse(interpolateParams)();
}
return $translate(translationId, interpolateParams, interpolation);
};
}
]);
{{'KEY' | translate:'broadcast'}}
This is useful if you want to do some dom manipulation after translation occurs.
A callback might be a smaller performance impact. Keep in mind that this can be triggered several times a second because it is called with each digest cycle. Maybe multiple times every time you type something into an input field with ng-model.
I would like to know what kind of manipulation you think of?
@Grigorius @maklemenz angular-translate already supports several broadcasts. You can find more about'em here: http://pascalprecht.github.io/angular-translate/docs/en/#/guide/16_events
To clarify: Almost all events will be broadcasted from a service level. So, no directive or filter should care about broadcasting any events.
Yes but those events only fire after you select a language - they don't fire on first (default language) translate.
As for DOM manipulation, when translating something <div>{{'KEY' | translate}}</div> you alter the div size - I would like to know when translation occured or completed to re-check div sizes or do other things.
Why can't you just listen for the first (if only that one needed) $translateChangeSuccess event?
And BTW: of course they get fired on first default language translate.
Yes they do! I did not get them because the directive which tried to listen to them got executed after the broadcast event. (needed a couple of hours to figure it out)
My solution was to wait for the broadcast in the mainCtrl, and when the directive was loaded rebroadcast it.
@Grigorius Okay so... can I close this now?