fix(compiler): use attribute id to merge translations (#15302)

We extracted ids from i18n attributes but forgot to use them when merging the translations, resulting in an error about missing translations even when they were correctly defined.

Fixes #15234

PR Close #15302
This commit is contained in:
Olivier Combe 2017-03-20 14:52:24 +01:00 committed by Miško Hevery
parent 16e0423085
commit 1d7693c1e1
2 changed files with 14 additions and 8 deletions

View File

@ -347,12 +347,13 @@ class _Visitor implements html.Visitor {
// translate the attributes of an element and remove i18n specific attributes
private _translateAttributes(el: html.Element): html.Attribute[] {
const attributes = el.attrs;
const i18nAttributeMeanings: {[name: string]: string} = {};
const i18nParsedMessageMeta:
{[name: string]: {meaning: string, description: string, id: string}} = {};
attributes.forEach(attr => {
if (attr.name.startsWith(_I18N_ATTR_PREFIX)) {
i18nAttributeMeanings[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
_parseMessageMeta(attr.value).meaning;
i18nParsedMessageMeta[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
_parseMessageMeta(attr.value);
}
});
@ -364,9 +365,9 @@ class _Visitor implements html.Visitor {
return;
}
if (attr.value && attr.value != '' && i18nAttributeMeanings.hasOwnProperty(attr.name)) {
const meaning = i18nAttributeMeanings[attr.name];
const message: i18n.Message = this._createI18nMessage([attr], meaning, '', '');
if (attr.value && attr.value != '' && i18nParsedMessageMeta.hasOwnProperty(attr.name)) {
const {meaning, description, id} = i18nParsedMessageMeta[attr.name];
const message: i18n.Message = this._createI18nMessage([attr], meaning, description, id);
const nodes = this._translations.get(message);
if (nodes) {
if (nodes.length == 0) {
@ -377,12 +378,12 @@ class _Visitor implements html.Visitor {
} else {
this._reportError(
el,
`Unexpected translation for attribute "${attr.name}" (id="${this._translations.digest(message)}")`);
`Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
}
} else {
this._reportError(
el,
`Translation unavailable for attribute "${attr.name}" (id="${this._translations.digest(message)}")`);
`Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
}
} else {
translatedAttributes.push(attr);

View File

@ -430,6 +430,11 @@ export function main() {
expect(fakeTranslate(HTML)).toEqual('<p title="**foo**"></p>');
});
it('should merge attributes with ids', () => {
const HTML = `<p i18n-title="@@id" title="foo"></p>`;
expect(fakeTranslate(HTML)).toEqual('<p title="**foo**"></p>');
});
it('should merge nested attributes', () => {
const HTML = `<div>{count, plural, =0 {<p i18n-title title="foo"></p>}}</div>`;
expect(fakeTranslate(HTML))