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

Ternary operator parsing produces some false results

Open phw opened this issue 9 years ago • 2 comments

With the patch from PR #69 the code snippet below will generate both the false translation keys active and non-active in addition to the correct one STATE_LABEL.

<a ng-class=" state==3 ? 'active': 'non-active'">
    <span>3</span> {{'STATE_LABEL' | translate}}
</a>

If I disable the HtmlFilterTernary regex (by commenting it out in tasks/angular-translate.js the extraction works as expected (only extracting STATE_LABEL).

phw avatar Mar 29 '16 09:03 phw

I believe that this pattern for HtmlFilterTernary would work better:

\s*(?:::)?([^?|}]*\?[^:]*:[^|}]*)\s*\|\s*translate(:.*?)?\s*

The change is in the first character class which was [^?]* (match any characters not ?) to [^?|}]* (match any characters not ?, |, or }). This prevents the match from crossing too many other interpolation sequences.

rgant avatar Sep 13 '16 14:09 rgant

In the meantime, adding a comment like this will help to prevent this regex from matching incorrectly. I inserted it after a ternary before the next translate.

<!-- Prevent grunt-angular-translate from incorrectly matching: ?|: -->

Edited: with a more effective comment.

rgant avatar Oct 31 '16 16:10 rgant