Search code examples
angularjsinternationalizationdirectiveangular-translate

AngularJS: Interpolate string in template after directive


I am applying translation on the templates and using "translate" directive for it like so:

<span translate>Hello {{name}}!</span>` 

In separate de.json file for German language I have translation key-value pairs like {"Hello {{name}}!": "Hallo {{name}}!"}. In runtime my "translate" directive suppose to replace content inside the "span" to "Hallo {{name}}!" and then AngularJS needs to interpolate template string.

Unfortunately today its working opposite way: 1st AngJS interpolates and then my directive applied.

Question: is there way in AngJS to setup my directive to be applied BEFORE AngularJs runs interpolation?


Solution

  • From the AngularJS Interpolation Guide:

    the interpolateDirective has a priority of 100 and sets up the watch in the preLink` function

    From the AngularJS $compile Docs:

    priority

    When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied. The priority is used to sort the directives before their compile functions get called. Priority is defined as a number. Directives with greater numerical priority are compiled first. Pre-link functions are also run in priority order, but post-link functions are run in reverse order. The order of directives with the same priority is undefined. The default priority is 0.

    The Angular Translate library has a directivePriority(priority) function in the $translateProvider API that allows you to configure the directive to run at a higher priority, like 101. That should get your translate directives to run prior to interpolation.