fix(ivy): handle expressions in i18n attributes properly (#32309)
Prior to this commit, complex expressions (that require additional statements to be generated) were handled incorrectly in case they were used in attributes annotated with i18n flags. The problem was caused by the fact that extra statements were not appended to the temporary vars block, so they were missing in generated code. This commit updated the logic to use the `convertPropertyBinding`, which contains the necessary code to append extra statements. The `convertExpressionBinding` function was removed as it duplicates the `convertPropertyBinding` one (for the most part) and is no longer used. PR Close #32309
This commit is contained in:

committed by
Matias Niemelä

parent
36d613dd67
commit
f00d03356f
@ -75,14 +75,27 @@ onlyInIvy('Ivy i18n logic').describe('runtime i18n', () => {
|
||||
});
|
||||
|
||||
it('should support interpolations with complex expressions', () => {
|
||||
loadTranslations(
|
||||
{'{$interpolation} - {$interpolation_1}': '{$interpolation} - {$interpolation_1} (fr)'});
|
||||
const fixture =
|
||||
initWithTemplate(AppComp, `<div i18n>{{ name | uppercase }} - {{ obj?.a?.b }}</div>`);
|
||||
expect(fixture.nativeElement.innerHTML).toEqual(`<div>ANGULAR - (fr)</div>`);
|
||||
fixture.componentRef.instance.obj = {a: {b: 'value'}};
|
||||
loadTranslations({
|
||||
' {$interpolation} - {$interpolation_1} - {$interpolation_2} ':
|
||||
' {$interpolation} - {$interpolation_1} - {$interpolation_2} (fr) '
|
||||
});
|
||||
const fixture = initWithTemplate(AppComp, `
|
||||
<div i18n>
|
||||
{{ name | uppercase }} -
|
||||
{{ obj?.a?.b }} -
|
||||
{{ obj?.getA()?.b }}
|
||||
</div>
|
||||
`);
|
||||
// the `obj` field is not yet defined, so 2nd and 3rd interpolations return empty strings
|
||||
expect(fixture.nativeElement.innerHTML).toEqual(`<div> ANGULAR - - (fr) </div>`);
|
||||
|
||||
fixture.componentRef.instance.obj = {
|
||||
a: {b: 'value 1'},
|
||||
getA: () => ({b: 'value 2'}),
|
||||
};
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.innerHTML).toEqual(`<div>ANGULAR - value (fr)</div>`);
|
||||
expect(fixture.nativeElement.innerHTML)
|
||||
.toEqual(`<div> ANGULAR - value 1 - value 2 (fr) </div>`);
|
||||
});
|
||||
|
||||
it('should support elements', () => {
|
||||
@ -1057,6 +1070,25 @@ onlyInIvy('Ivy i18n logic').describe('runtime i18n', () => {
|
||||
expect(fixture.debugElement.children[0].children[0].references.ref.test).toBe('Set');
|
||||
expect(fixture.debugElement.children[1].children[0].references.ref.test).toBe('Set');
|
||||
});
|
||||
|
||||
it('with complex expressions', () => {
|
||||
loadTranslations({
|
||||
'{$interpolation} - {$interpolation_1} - {$interpolation_2}':
|
||||
'{$interpolation} - {$interpolation_1} - {$interpolation_2} (fr)'
|
||||
});
|
||||
const fixture = initWithTemplate(AppComp, `
|
||||
<div i18n-title title="{{ name | uppercase }} - {{ obj?.a?.b }} - {{ obj?.getA()?.b }}"></div>
|
||||
`);
|
||||
// the `obj` field is not yet defined, so 2nd and 3rd interpolations return empty strings
|
||||
expect(fixture.nativeElement.firstChild.title).toEqual(`ANGULAR - - (fr)`);
|
||||
|
||||
fixture.componentRef.instance.obj = {
|
||||
a: {b: 'value 1'},
|
||||
getA: () => ({b: 'value 2'}),
|
||||
};
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.firstChild.title).toEqual(`ANGULAR - value 1 - value 2 (fr)`);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with directives and host bindings', () => {
|
||||
|
Reference in New Issue
Block a user