style(I18N): Carriage returns in serialized files

This commit is contained in:
Victor Berchet 2016-09-30 14:52:12 -07:00 committed by Chuck Jazdzewski
parent 756ef09d12
commit 65a60b7456
5 changed files with 68 additions and 60 deletions

View File

@ -27,7 +27,6 @@ const _PLACEHOLDER_TAG = 'x';
const _SOURCE_TAG = 'source'; const _SOURCE_TAG = 'source';
const _TARGET_TAG = 'target'; const _TARGET_TAG = 'target';
const _UNIT_TAG = 'trans-unit'; const _UNIT_TAG = 'trans-unit';
const _CR = (ws: number = 0) => new xml.Text(`\n${new Array(ws).join(' ')}`);
// http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
// http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html // http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
@ -44,34 +43,37 @@ export class Xliff implements Serializer {
let transUnit = new xml.Tag(_UNIT_TAG, {id: id, datatype: 'html'}); let transUnit = new xml.Tag(_UNIT_TAG, {id: id, datatype: 'html'});
transUnit.children.push( transUnit.children.push(
_CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), _CR(8), new xml.CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)),
new xml.Tag(_TARGET_TAG)); new xml.CR(8), new xml.Tag(_TARGET_TAG));
if (message.description) { if (message.description) {
transUnit.children.push( transUnit.children.push(
_CR(8), new xml.CR(8),
new xml.Tag( new xml.Tag(
'note', {priority: '1', from: 'description'}, [new xml.Text(message.description)])); 'note', {priority: '1', from: 'description'}, [new xml.Text(message.description)]));
} }
if (message.meaning) { if (message.meaning) {
transUnit.children.push( transUnit.children.push(
_CR(8), new xml.CR(8),
new xml.Tag('note', {priority: '1', from: 'meaning'}, [new xml.Text(message.meaning)])); new xml.Tag('note', {priority: '1', from: 'meaning'}, [new xml.Text(message.meaning)]));
} }
transUnit.children.push(_CR(6)); transUnit.children.push(new xml.CR(6));
transUnits.push(_CR(6), transUnit); transUnits.push(new xml.CR(6), transUnit);
}); });
const body = new xml.Tag('body', {}, [...transUnits, _CR(4)]); const body = new xml.Tag('body', {}, [...transUnits, new xml.CR(4)]);
const file = new xml.Tag( const file = new xml.Tag(
'file', {'source-language': _SOURCE_LANG, datatype: 'plaintext', original: 'ng2.template'}, 'file', {'source-language': _SOURCE_LANG, datatype: 'plaintext', original: 'ng2.template'},
[_CR(4), body, _CR(2)]); [new xml.CR(4), body, new xml.CR(2)]);
const xliff = new xml.Tag('xliff', {version: _VERSION, xmlns: _XMLNS}, [_CR(2), file, _CR()]); const xliff = new xml.Tag(
'xliff', {version: _VERSION, xmlns: _XMLNS}, [new xml.CR(2), file, new xml.CR()]);
return xml.serialize([new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), _CR(), xliff]); return xml.serialize([
new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), new xml.CR(), xliff, new xml.CR()
]);
} }
load(content: string, url: string, messageBundle: MessageBundle): {[id: string]: ml.Node[]} { load(content: string, url: string, messageBundle: MessageBundle): {[id: string]: ml.Node[]} {

View File

@ -43,7 +43,6 @@ export class Xmb implements Serializer {
write(messageMap: {[k: string]: i18n.Message}): string { write(messageMap: {[k: string]: i18n.Message}): string {
const visitor = new _Visitor(); const visitor = new _Visitor();
let rootNode = new xml.Tag(_MESSAGES_TAG); let rootNode = new xml.Tag(_MESSAGES_TAG);
rootNode.children.push(new xml.Text('\n'));
Object.keys(messageMap).forEach((id) => { Object.keys(messageMap).forEach((id) => {
const message = messageMap[id]; const message = messageMap[id];
@ -58,16 +57,18 @@ export class Xmb implements Serializer {
} }
rootNode.children.push( rootNode.children.push(
new xml.Text(' '), new xml.Tag(_MESSAGE_TAG, attrs, visitor.serialize(message.nodes)), new xml.CR(2), new xml.Tag(_MESSAGE_TAG, attrs, visitor.serialize(message.nodes)));
new xml.Text('\n'));
}); });
rootNode.children.push(new xml.CR());
return xml.serialize([ return xml.serialize([
new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), new xml.Declaration({version: '1.0', encoding: 'UTF-8'}),
new xml.Text('\n'), new xml.CR(),
new xml.Doctype(_MESSAGES_TAG, _DOCTYPE), new xml.Doctype(_MESSAGES_TAG, _DOCTYPE),
new xml.Text('\n'), new xml.CR(),
rootNode, rootNode,
new xml.CR(),
]); ]);
} }

View File

@ -88,6 +88,10 @@ export class Text implements Node {
visit(visitor: IVisitor): any { return visitor.visitText(this); } visit(visitor: IVisitor): any { return visitor.visitText(this); }
} }
export class CR extends Text {
constructor(ws: number = 0) { super(`\n${new Array(ws + 1).join(' ')}`); }
}
const _ESCAPED_CHARS: [RegExp, string][] = [ const _ESCAPED_CHARS: [RegExp, string][] = [
[/&/g, '&'], [/&/g, '&'],
[/"/g, '"'], [/"/g, '"'],

View File

@ -21,47 +21,49 @@ const HTML = `
const WRITE_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?> const WRITE_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template"> <file source-language="en" datatype="plaintext" original="ng2.template">
<body> <body>
<trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html"> <trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html">
<source>translatable attribute</source> <source>translatable attribute</source>
<target/> <target/>
</trans-unit> </trans-unit>
<trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html"> <trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html">
<source>translatable element <x id="START_BOLD_TEXT" ctype="b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="b"/> <x id="INTERPOLATION"/></source> <source>translatable element <x id="START_BOLD_TEXT" ctype="b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="b"/> <x id="INTERPOLATION"/></source>
<target/> <target/>
</trans-unit> </trans-unit>
<trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html"> <trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html">
<source>foo</source> <source>foo</source>
<target/> <target/>
<note priority="1" from="description">d</note> <note priority="1" from="description">d</note>
<note priority="1" from="meaning">m</note> <note priority="1" from="meaning">m</note>
</trans-unit> </trans-unit>
</body> </body>
</file> </file>
</xliff>`; </xliff>
`;
const LOAD_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?> const LOAD_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template"> <file source-language="en" datatype="plaintext" original="ng2.template">
<body> <body>
<trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html"> <trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html">
<source>translatable attribute</source> <source>translatable attribute</source>
<target>etubirtta elbatalsnart</target> <target>etubirtta elbatalsnart</target>
</trans-unit> </trans-unit>
<trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html"> <trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html">
<source>translatable element <x id="START_BOLD_TEXT" ctype="b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="b"/> <x id="INTERPOLATION"/></source> <source>translatable element <x id="START_BOLD_TEXT" ctype="b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="b"/> <x id="INTERPOLATION"/></source>
<target><x id="INTERPOLATION"/> footnemele elbatalsnart <x id="START_BOLD_TEXT" ctype="b"/>sredlohecalp htiw<x id="CLOSE_BOLD_TEXT" ctype="b"/></target> <target><x id="INTERPOLATION"/> footnemele elbatalsnart <x id="START_BOLD_TEXT" ctype="b"/>sredlohecalp htiw<x id="CLOSE_BOLD_TEXT" ctype="b"/></target>
</trans-unit> </trans-unit>
<trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html"> <trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html">
<source>foo</source> <source>foo</source>
<target>oof</target> <target>oof</target>
<note priority="1" from="description">d</note> <note priority="1" from="description">d</note>
<note priority="1" from="meaning">m</note> <note priority="1" from="meaning">m</note>
</trans-unit> </trans-unit>
</body> </body>
</file> </file>
</xliff>`; </xliff>
`;
export function main(): void { export function main(): void {
let serializer: Xliff; let serializer: Xliff;

View File

@ -6,12 +6,10 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {MessageBundle} from '@angular/compiler/src/i18n/message_bundle';
import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb'; import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb';
import {describe, expect, it} from '@angular/core/testing/testing_internal'; import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config';
import {MessageBundle} from '../../../src/i18n/message_bundle';
import {HtmlParser} from '../../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';
export function main(): void { export function main(): void {
describe('XMB serializer', () => { describe('XMB serializer', () => {
@ -49,7 +47,8 @@ export function main(): void {
<msg id="e2ccf3d131b15f54aa1fcf1314b1ca77c14bfcc2">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} }</msg> <msg id="e2ccf3d131b15f54aa1fcf1314b1ca77c14bfcc2">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} }</msg>
<msg id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" desc="d" meaning="m">foo</msg> <msg id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" desc="d" meaning="m">foo</msg>
<msg id="83dd87699b8c1779dd72277ef6e2d46ca58be042">{ count, plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} } } }</msg> <msg id="83dd87699b8c1779dd72277ef6e2d46ca58be042">{ count, plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} } } }</msg>
</messagebundle>`; </messagebundle>
`;
it('should write a valid xmb file', () => { expect(toXmb(HTML)).toEqual(XMB); }); it('should write a valid xmb file', () => { expect(toXmb(HTML)).toEqual(XMB); });