Search code examples
angularjsangular-translate

Angular translate attribute vs filter


Which of these two is better?

<span translate="key">Key</span>

or

<span>{{'key' | translate}}</span>

They are both good and work fine but in first case I must fill the content of the element.


Solution

  • Using the attribute is better performance-wise, especially if you intend to use translations on elements that will be inside ngRepeats. This is because the way filter work internally.

    Every time there is a digest cycle, angularjs reloads all the expressions containing filters. This is because angular can't possibly know if a filter has changed or not. What this means is that, even if the key of the translation has not changed, but some other value on the scope has, angular will look through every translation and translate it again, just to come to the conclusion that they all remain the same.

    Attributes are smarter, because the developer of the directive has explicit control over when it should re-render and what watcher should be created.

    Edit: And as far as I know, there is no need to fill the content in the first use-case. You can just leave it empty.