Skip to content

Commit b03bb7a

Browse files
irontaekmattrbeck
authored andcommitted
fix(core): check every i18n attribute translation for ICUs
`ICU_REGEXP` is only used with `.test()` but carried the `g` flag, so a match left `lastIndex` past the ICU. The check is followed by a throw, so the regex was never reset, and the next attribute message was searched from that offset. A shorter translation containing an ICU then passed the check and rendered instead of throwing. Dropping the `g` flag makes each check start at index 0.
1 parent a0702f0 commit b03bb7a

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/core/src/render3/i18n/i18n_parse.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import {splitNsName} from '../util/tags';
7878
import {NAMESPACE_URIS} from '../namespaces';
7979

8080
const BINDING_REGEXP = /�(\d+):?\d*�/gi;
81-
const ICU_REGEXP = /({\s*�\d+:?\d*�\s*,\s*\S{6}\s*,[\s\S]*})/gi;
81+
const ICU_REGEXP = /({\s*�\d+:?\d*�\s*,\s*\S{6}\s*,[\s\S]*})/i;
8282
const NESTED_ICU = /�(\d+)�/;
8383
const ICU_BLOCK_REGEXP = /^\s*(�\d+:?\d*�)\s*,\s*(select|plural)\s*,/;
8484

‎packages/core/test/acceptance/i18n_spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,35 @@ describe('runtime i18n', () => {
29292929
/Unable to parse ICU expression in "Text { count }" message./,
29302930
);
29312931
});
2932+
2933+
it('should throw for every ICU added into an attribute translation', () => {
2934+
loadTranslations({
2935+
[computeMsgId('Title with enough text before the {$INTERPOLATION}')]:
2936+
'Title with enough text before the {{$INTERPOLATION}, select, other {name}}',
2937+
[computeMsgId('Hi {$INTERPOLATION}')]: '{{$INTERPOLATION}, select, other {name}}',
2938+
});
2939+
2940+
@Component({
2941+
selector: 'long-title',
2942+
template: '<div i18n-title title="Title with enough text before the {{name}}"></div>',
2943+
})
2944+
class LongTitle {
2945+
name = 'Angular';
2946+
}
2947+
2948+
@Component({
2949+
selector: 'short-title',
2950+
template: '<div i18n-title title="Hi {{name}}"></div>',
2951+
})
2952+
class ShortTitle {
2953+
name = 'Angular';
2954+
}
2955+
2956+
// The second check must not resume from where the first match ended.
2957+
const error = /ICU expressions are not supported in attributes/;
2958+
expect(() => TestBed.createComponent(LongTitle).detectChanges()).toThrowError(error);
2959+
expect(() => TestBed.createComponent(ShortTitle).detectChanges()).toThrowError(error);
2960+
});
29322961
});
29332962

29342963
it('should handle extra HTML in translation as plain text', () => {

0 commit comments

Comments
 (0)