angular-translate icon indicating copy to clipboard operation
angular-translate copied to clipboard

broadcast translate event

Open Grigorius opened this issue 13 years ago • 7 comments

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.

Grigorius avatar Oct 05 '13 19:10 Grigorius

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?

ghost avatar Oct 06 '13 07:10 ghost

@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.

0x-r4bbit avatar Oct 06 '13 08:10 0x-r4bbit

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.

Grigorius avatar Oct 06 '13 10:10 Grigorius

Why can't you just listen for the first (if only that one needed) $translateChangeSuccess event?

0x-r4bbit avatar Oct 06 '13 11:10 0x-r4bbit

And BTW: of course they get fired on first default language translate.

0x-r4bbit avatar Oct 06 '13 11:10 0x-r4bbit

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 avatar Oct 06 '13 20:10 Grigorius

@Grigorius Okay so... can I close this now?

0x-r4bbit avatar Oct 07 '13 16:10 0x-r4bbit