From 42c67e09d50b159edbc75ba31021e3a00c3caf0c Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Mon, 22 May 2017 20:20:45 +0200 Subject: [PATCH] feat(compiler): add location note to extracted xliff2 files (#16791) Add source location as a note tag as `path/to/file.ts:start_line[,end_line]`. `[,end_line]` part is optional and specified only if the end line is different from the start line. Fixes #16531 --- .../integrationtest/test/i18n_spec.ts | 8 +++ .../compiler/src/i18n/serializers/xliff2.ts | 17 +++-- .../test/i18n/serializers/xliff2_spec.ts | 70 +++++++++++++++++-- .../test/i18n/serializers/xliff_spec.ts | 26 ++++++- 4 files changed, 111 insertions(+), 10 deletions(-) diff --git a/packages/compiler-cli/integrationtest/test/i18n_spec.ts b/packages/compiler-cli/integrationtest/test/i18n_spec.ts index 787def3142f9e..533141fa91469 100644 --- a/packages/compiler-cli/integrationtest/test/i18n_spec.ts +++ b/packages/compiler-cli/integrationtest/test/i18n_spec.ts @@ -88,17 +88,25 @@ const EXPECTED_XLIFF2 = ` desc meaning + src/basic.ts:1 translate me + + src/basic.ts:5 + src/entry_components.ts:1 + Welcome + + node_modules/third_party/other_comp.d.ts:1,2 + other-3rdP-component multi-lines diff --git a/packages/compiler/src/i18n/serializers/xliff2.ts b/packages/compiler/src/i18n/serializers/xliff2.ts index e2940491ef77c..6db2de5d9cc9a 100644 --- a/packages/compiler/src/i18n/serializers/xliff2.ts +++ b/packages/compiler/src/i18n/serializers/xliff2.ts @@ -35,9 +35,9 @@ export class Xliff2 extends Serializer { messages.forEach(message => { const unit = new xml.Tag(_UNIT_TAG, {id: message.id}); + const notes = new xml.Tag('notes'); if (message.description || message.meaning) { - const notes = new xml.Tag('notes'); if (message.description) { notes.children.push( new xml.CR(8), @@ -49,11 +49,18 @@ export class Xliff2 extends Serializer { new xml.CR(8), new xml.Tag('note', {category: 'meaning'}, [new xml.Text(message.meaning)])); } - - notes.children.push(new xml.CR(6)); - unit.children.push(new xml.CR(6), notes); } + message.sources.forEach((source: i18n.MessageSpan) => { + notes.children.push(new xml.CR(8), new xml.Tag('note', {category: 'location'}, [ + new xml.Text( + `${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`) + ])); + }); + + notes.children.push(new xml.CR(6)); + unit.children.push(new xml.CR(6), notes); + const segment = new xml.Tag('segment'); segment.children.push( @@ -367,4 +374,4 @@ function getTypeForTag(tag: string): string { default: return 'other'; } -} \ No newline at end of file +} diff --git a/packages/compiler/test/i18n/serializers/xliff2_spec.ts b/packages/compiler/test/i18n/serializers/xliff2_spec.ts index d20548c48deb3..5c10135c9252a 100644 --- a/packages/compiler/test/i18n/serializers/xliff2_spec.ts +++ b/packages/compiler/test/i18n/serializers/xliff2_spec.ts @@ -24,22 +24,33 @@ const HTML = `

hello

{ count, plural, =0 { { sex, select, other {

deeply nested

}} }}

{ count, plural, =0 { { sex, select, other {

deeply nested

}} }}

+

multi +lines

`; const WRITE_XLIFF = ` + + file.ts:2 + translatable attribute + + file.ts:3 + translatable element with placeholders + + file.ts:4 + {VAR_PLURAL, plural, =0 {test} } @@ -48,6 +59,7 @@ const WRITE_XLIFF = ` d m + file.ts:5 foo @@ -56,6 +68,7 @@ const WRITE_XLIFF = ` nested + file.ts:6 Text @@ -64,6 +77,7 @@ const WRITE_XLIFF = ` ph names + file.ts:7 @@ -72,21 +86,37 @@ const WRITE_XLIFF = ` empty element + file.ts:8 hello + + file.ts:9 + {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } + + file.ts:10 + {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } + + + file.ts:11,12 + + + multi +lines + + `; @@ -95,18 +125,27 @@ const LOAD_XLIFF = ` + + file.ts:2 + translatable attribute etubirtta elbatalsnart + + file.ts:3 + translatable element with placeholders sredlohecalp htiw tnemele elbatalsnart + + file.ts:4 + {VAR_PLURAL, plural, =0 {test} } {VAR_PLURAL, plural, =0 {TEST} } @@ -116,6 +155,7 @@ const LOAD_XLIFF = ` d m + file.ts:5 foo @@ -125,6 +165,7 @@ const LOAD_XLIFF = ` nested + file.ts:6 Text @@ -134,6 +175,7 @@ const LOAD_XLIFF = ` ph names + file.ts:7 @@ -143,6 +185,7 @@ const LOAD_XLIFF = ` empty element + file.ts:8 hello @@ -150,17 +193,34 @@ const LOAD_XLIFF = ` + + file.ts:9 + {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } } + + file.ts:10 + {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } } + + + file.ts:11,12 + + + multi +lines + multi +lignes + + `; @@ -170,7 +230,7 @@ export function main(): void { function toXliff(html: string, locale: string | null = null): string { const catalog = new MessageBundle(new HtmlParser, [], {}, locale); - catalog.updateFromTemplate(html, '', DEFAULT_INTERPOLATION_CONFIG); + catalog.updateFromTemplate(html, 'file.ts', DEFAULT_INTERPOLATION_CONFIG); return catalog.write(serializer); } @@ -208,8 +268,10 @@ export function main(): void { '6536355551500405293': ' olleh', 'baz': '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}', - '2015957479576096115': - '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}' + '2015957479576096115': '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}', + '2340165783990709777': `multi +lignes` }); }); @@ -333,4 +395,4 @@ export function main(): void { }); }); }); -} \ No newline at end of file +} diff --git a/packages/compiler/test/i18n/serializers/xliff_spec.ts b/packages/compiler/test/i18n/serializers/xliff_spec.ts index c6d01231f4bec..56e929f3967ac 100644 --- a/packages/compiler/test/i18n/serializers/xliff_spec.ts +++ b/packages/compiler/test/i18n/serializers/xliff_spec.ts @@ -25,6 +25,8 @@ const HTML = `


{ count, plural, =0 { { sex, select, other {

deeply nested

}} }}

{ count, plural, =0 { { sex, select, other {

deeply nested

}} }}

+

multi +lines

`; const WRITE_XLIFF = ` @@ -112,6 +114,15 @@ const WRITE_XLIFF = ` 11 + + multi +lines + + + file.ts + 12 + + @@ -195,6 +206,16 @@ const LOAD_XLIFF = ` {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } } + + multi +lines + multi +lignes + + file.ts + 12 + + @@ -243,7 +264,10 @@ export function main(): void { 'baz': '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}', '0e16a673a5a7a135c9f7b957ec2c5c6f6ee6e2c4': - '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}' + '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}', + 'fcfa109b0e152d4c217dbc02530be0bcb8123ad1': `multi +lignes` }); });