fix(ivy): use container i18n meta if a message is a single ICU (#33191)

Prior to this commit, metadata defined on ICU container element was not inherited by the ICU if the whole message is a single ICU (for example: `<ng-container i18n="meaning|description@@id">{count, select, ...}</ng-container>). This commit updates the logic to use parent container i18n meta information for the cases when a message consists of a single ICU.

Fixes #33171

PR Close #33191
This commit is contained in:
Andrew Kushnir 2019-10-15 15:54:14 -07:00 committed by Matias Niemelä
parent 1a8bd22fa3
commit 7e64bbe5a8
3 changed files with 49 additions and 3 deletions

View File

@ -3385,6 +3385,30 @@ describe('i18n support in the template compiler', () => {
verify(input, output); verify(input, output);
}); });
it('should attach metadata in case an ICU represents the whole message', () => {
const input = `
<div i18n="meaningA|descA@@idA">{count, select, 1 {one} other {more than one}}</div>
`;
const output = String.raw `
var $I18N_0$;
if (ngI18nClosureMode) {
/**
* @desc descA
* @meaning meaningA
*/
const $MSG_EXTERNAL_idA$$APP_SPEC_TS_1$ = goog.getMsg("{VAR_SELECT, select, 1 {one} other {more than one}}");
$I18N_0$ = $MSG_EXTERNAL_idA$$APP_SPEC_TS_1$;
}
else {
$I18N_0$ = $localize \`:meaningA|descA@@idA:{VAR_SELECT, select, 1 {one} other {more than one}}\`;
}
$I18N_0$ = i0.ɵɵi18nPostprocess($I18N_0$, { "VAR_SELECT": "\uFFFD0\uFFFD" });
`;
verify(input, output);
});
}); });
describe('errors', () => { describe('errors', () => {

View File

@ -111,7 +111,7 @@ export class I18nMetaVisitor implements html.Visitor {
element.attrs = attrs; element.attrs = attrs;
} }
} }
html.visitAll(this, element.children); html.visitAll(this, element.children, element.i18n);
return element; return element;
} }
@ -127,8 +127,10 @@ export class I18nMetaVisitor implements html.Visitor {
const icu = icuFromI18nMessage(message); const icu = icuFromI18nMessage(message);
icu.name = name; icu.name = name;
} else { } else {
// when ICU is a root level translation // ICU is a top level message, try to use metadata from container element if provided via
message = this._generateI18nMessage([expansion], meta); // `context` argument. Note: context may not be available for standalone ICUs (without
// wrapping element), so fallback to ICU metadata in this case.
message = this._generateI18nMessage([expansion], context || meta);
} }
expansion.i18n = message; expansion.i18n = message;
return expansion; return expansion;

View File

@ -1031,6 +1031,26 @@ onlyInIvy('Ivy i18n logic').describe('runtime i18n', () => {
expect(fixture.debugElement.nativeElement.innerHTML).toContain('A'); expect(fixture.debugElement.nativeElement.innerHTML).toContain('A');
expect(fixture.debugElement.nativeElement.innerHTML).toContain('B'); expect(fixture.debugElement.nativeElement.innerHTML).toContain('B');
}); });
it('should use metadata from container element if a message is a single ICU', () => {
loadTranslations({idA: '{VAR_SELECT, select, 1 {un} other {plus d\'un}}'});
@Component({
selector: 'app',
template: `
<div i18n="@@idA">{count, select, 1 {one} other {more than one}}</div>
`
})
class AppComponent {
count = 2;
}
TestBed.configureTestingModule({declarations: [AppComponent]});
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.innerHTML).toContain('plus d\'un');
});
}); });
describe('should support attributes', () => { describe('should support attributes', () => {