Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(compiler): ApplyMapping - to finish
  • Loading branch information
vicb authored and ocombe committed Jul 17, 2017
commit c5723711994ab010637d1cf03e2a8d5588d8c2a2
98 changes: 98 additions & 0 deletions packages/compiler/src/i18n/migration/v0_to_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@

import {I18nVersion} from '@angular/core';

import * as ml from '../../ml_parser/ast';
import {XmlParser} from '../../ml_parser/xml_parser';
import {Message} from '../i18n_ast';
import {MessageSpan} from '../i18n_ast';
import {MessageBundle} from '../message_bundle';
import {I18nError} from '../parse_util';
import {createSerializer} from '../serializers/factory';
import {getXliffMsgTextById} from '../serializers/xliff';
import {getXliff2MsgTextById} from '../serializers/xliff2';
import {getXtbMsgTextById} from '../serializers/xtb';


// Maps v1 ids to corresponding v0 ids and their MessageSpan
export type V1ToV0Map = {
[v1: string]: {ids: string[], sources?: MessageSpan[]}
};
// Map of v1 ids having conflicts
export type V1ToV0Conflicts = {
[v1: string]: {id: string, msg: string}[]
};
// Map of v0 ids and their new v1 ids or `null`.
// `null` means that the ID is not used and should be removed
export type V0ToV1Map = {
[v0: string]: string | null
};


/**
* A single V1 message could replace multiple V0 messages.
Expand Down Expand Up @@ -116,4 +128,90 @@ export function computeConflicts(map: V1ToV0Map, content: string, format: string
});

return conflicts;
}

/**
* `v0ToV1` contains the mapping from the old id to the new id. If the mapping is `null` then the
* old message should be removed.
*
*/
export function applyMapping(
v0toV1: V0ToV1Map, v1toV0: V1ToV0Map, content: string, format: string) {
// First step is to migrate the IDs and remove obsolete messages
const {msgInfos, errors} = new MsgInfosVisitor().parse(content, '');

if (errors.length) {
throw new Error(`Translations parse errors:\n${errors.join('\n')}`);
}

// Process messages in reverse order because modifying a message invalidate the following
// SourceSpan
Array.from(msgInfos.entries()).reverse().forEach(([id, info]) => {
if (v0toV1.hasOwnProperty(id)) {
const newId = v0toV1[id];
if (newId === null) {
// Remove the whole element when the target id is null
const startOfEl = info.el.sourceSpan.start.offset;
const endOfPreviousEl = content.lastIndexOf('>', startOfEl);
const removeFrom = endOfPreviousEl === -1 ? startOfEl : endOfPreviousEl + 1;
const removeTo = info.el.endSourceSpan !.end.offset;
content = content.slice(0, removeFrom) + content.slice(removeTo);
} else {
// Change the id attribute when there is a target id
const removeFrom = info.id.valueSpan !.start.offset + 1;
const removeTo = info.id.valueSpan !.end.offset - 1;
content = content.slice(0, removeFrom) + newId + content.slice(removeTo);
}
}
});


// Second step is to replace the references to the source file

// TODO(vicb)

return content;
}

type MsgInfos = Map<string, {el: ml.Element, id: ml.Attribute}>;

/**
* Parse a translation bundle and extract for each message:
* - the `id` attribute node,
* - the message element node.
*
* Supports: xtb, xliff and xliff2
*/
class MsgInfosVisitor extends ml.RecursiveVisitor {
private errors: I18nError[];
private msgInfos: MsgInfos = new Map();

parse(content: string, url: string): {msgInfos: MsgInfos, errors: I18nError[]} {
const xmlParser = new XmlParser().parse(content, url, false);
this.errors = xmlParser.errors;
ml.visitAll(this, xmlParser.rootNodes, null);
return {msgInfos: this.msgInfos, errors: this.errors};
}

visitElement(el: ml.Element, context: any): any {
switch (el.name) {
case 'trans-unit': // xliff

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use constants here const XLIFF_MSG_EL = 'trans-unit';

case 'unit': // xliff2
case 'translation': // xtb
const id = el.attrs.find(attr => attr.name === 'id');
if (id) {
this.msgInfos.set(id.value, {el, id});
} else {
this._addError(el, `<${el.name}> misses the "id" attribute`);
}
break;

default:
super.visitElement(el, context);
}
}

private _addError(node: ml.Node, message: string): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused but required since we extend ml.RecursiveVisitor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would it be required ?

this.errors.push(new I18nError(node.sourceSpan !, message));
}
}
4 changes: 1 addition & 3 deletions packages/compiler/src/ml_parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ export function visitAll(visitor: Visitor, nodes: Node[], context: any = null):
}

export class RecursiveVisitor implements Visitor {
constructor() {}

visitElement(ast: Element, context: any): any {
this.visitChildren(context, visit => {
visit(ast.attrs);
Expand Down Expand Up @@ -142,7 +140,7 @@ export function findNode(nodes: Node[], position: number): HtmlAstPath {
return true;
}
}
}
};

visitAll(visitor, nodes);

Expand Down
32 changes: 30 additions & 2 deletions packages/compiler/test/i18n/migration/v0_to_v1_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {MessageBundle} from '../../../src/i18n/message_bundle';
import {computeConflicts, generateV1ToV0Map} from '../../../src/i18n/migration/v0_to_v1';
import {applyMapping, computeConflicts, generateV1ToV0Map} from '../../../src/i18n/migration/v0_to_v1';
import {HtmlParser} from '../../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';

Expand Down Expand Up @@ -51,7 +51,7 @@ export function main(): void {
});
});

describe(' Compute conflicts', () => {
describe('Compute conflicts', () => {
const v1ToV0Map = {
'single.v1': {ids: ['single.v0']},
'mul_ok.v1': {ids: ['mul_ok.1.v0', 'mul_ok.2.v0']},
Expand Down Expand Up @@ -114,5 +114,33 @@ export function main(): void {
expect(computeConflicts(v1ToV0Map, TRANS, 'xliff2')).toEqual(EXPECTED_CONFLICTS);
});
});

// TODO(vicb): add more tests for different formats
describe('ApplyMapping', () => {
it('should work with XMB', () => {
const SRC = `
<translationbundle>
<translation id="ignore">a</translation>
<translation id="remove">b</translation>
<translation id="old">c</translation>
</translationbundle>`;

const DST = `
<translationbundle>
<translation id="ignore">a</translation>
<translation id="new">c</translation>
</translationbundle>`;

const mapping = {
'remove': null,
'old': 'new',
};

expect(applyMapping(mapping, null !, SRC, 'xmb')).toEqual(DST);
});



});
});
}