fix(i18n extractor): array manipulation

This commit is contained in:
Victor Berchet 2016-08-01 13:11:48 -07:00
parent cdb1a237e5
commit df44e3e425
2 changed files with 23 additions and 7 deletions

View File

@ -172,17 +172,17 @@ class _ExtractVisitor implements html.Visitor {
} }
private _extractFromAttributes(el: html.Element, messages: Message[]): void { private _extractFromAttributes(el: html.Element, messages: Message[]): void {
const explicitAttrNameToValue: Map<string, string> = new Map(); const explicitAttrNameToValue: {[k: string]: string} = {};
const implicitAttrNames: string[] = this._implicitAttrs[el.name] || []; const implicitAttrNames: string[] = this._implicitAttrs[el.name] || [];
el.attrs.filter(attr => attr.name.startsWith(_I18N_ATTR_PREFIX)) el.attrs.filter(attr => attr.name.startsWith(_I18N_ATTR_PREFIX))
.forEach( .forEach(
attr => explicitAttrNameToValue.set( attr => explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
attr.name.substring(_I18N_ATTR_PREFIX.length), attr.value)); attr.value);
el.attrs.forEach(attr => { el.attrs.forEach(attr => {
if (explicitAttrNameToValue.has(attr.name)) { if (attr.name in explicitAttrNameToValue) {
this._addMessage(messages, [attr], explicitAttrNameToValue.get(attr.name)); this._addMessage(messages, [attr], explicitAttrNameToValue[attr.name]);
} else if (implicitAttrNames.some(name => attr.name === name)) { } else if (implicitAttrNames.some(name => attr.name === name)) {
this._addMessage(messages, [attr]); this._addMessage(messages, [attr]);
} }
@ -250,8 +250,8 @@ class _ExtractVisitor implements html.Visitor {
0); 0);
if (significantChildren == 1) { if (significantChildren == 1) {
for (let i = startIndex; i < messages.length; i++) { for (let i = messages.length - 1; i >= startIndex; i--) {
let ast = messages[i].nodes; const ast = messages[i].nodes;
if (!(ast.length == 1 && ast[0] instanceof html.Attribute)) { if (!(ast.length == 1 && ast[0] instanceof html.Attribute)) {
messages.splice(i, 1); messages.splice(i, 1);
break; break;

View File

@ -21,6 +21,22 @@ export function main() {
]); ]);
}); });
it('should extract from elements', () => {
expect(
extract(
'<div i18n="m|d">{count, plural, =0 { <p i18n-title i18n-desc title="title" desc="desc"></p>}}</div>'))
.toEqual([
[
[
'{count, plural, =0 {<p i18n-title="" i18n-desc="" title="title" desc="desc"></p>}}'
],
'm', 'd'
],
[['title="title"'], '', ''],
[['desc="desc"'], '', ''],
]);
});
it('should not create a message for empty elements', it('should not create a message for empty elements',
() => { expect(extract('<div i18n="m|d"></div>')).toEqual([]); }); () => { expect(extract('<div i18n="m|d"></div>')).toEqual([]); });
}); });