Skip to content

Commit

Permalink
fix: correct handling of special chars in description and meaning - f…
Browse files Browse the repository at this point in the history
…ixes #87
  • Loading branch information
daniel-sc committed Nov 17, 2023
1 parent 88bec68 commit bdb592f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
41 changes: 41 additions & 0 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,47 @@ describe('Builder', () => {
'</xliff>'
});
});
test('extract-and-merge xlf 1.2 with ampersand in meaning', async () => {
await runTest(
{
messagesBefore: '<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">\n' +
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>source &amp; val</source>\n' +
' <note priority="1" from="description">description &amp; and</note>\n' +
' <note priority="1" from="meaning">meaning &amp; and</note>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>',
messagesFrBefore: '<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">\n' +
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>',
options: {
format: 'xlf',
targetFiles: ['messages.fr.xlf'],
outputPath: 'builder-test',
includeContext: true,
removeIdsWithPrefix: ['removeMe']
},
messagesFrExpected: '<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">\n' +
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>source &amp; val</source>\n' +
' <target state="new">source &amp; val</target>\n' +
' <note priority="1" from="description">description &amp; and</note>\n' +
' <note priority="1" from="meaning">meaning &amp; and</note>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>'
});
});
test('extract-and-merge xlf 1.2 with newTranslationTargetsBlank', async () => {
await runTest(
{
Expand Down
4 changes: 2 additions & 2 deletions src/model/translationFileSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('translationFileSerialization', () => {
target: 'target val',
state: 'initial',
meaning: 'greeting',
description: 'Greeting message that includes the user\'s name.',
description: 'Greeting message that includes the user&apos;s name.',
locations: [
{
file: 'app/app.component.ts',
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('translationFileSerialization', () => {
target: 'target val',
state: 'initial',
meaning: 'greeting',
description: 'Greeting message that includes the user\'s name.',
description: 'Greeting message that includes the user&apos;s name.',
locations: [
{
file: 'app/app.component.ts',
Expand Down
16 changes: 10 additions & 6 deletions src/model/translationFileSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export function fromXlf2(xlf2: string): TranslationFile {
return {
id: unit.attr.id,
source: toString(...segment.childNamed('source')!.children),
target: segment.childNamed('target') ? toString(...segment.childNamed('target')!.children) : undefined,
target: toStringOrUndefined(segment.childNamed('target')?.children),
state: segment.attr.state,
meaning: notes?.childWithAttribute('category', 'meaning')?.val,
description: notes?.childWithAttribute('category', 'description')?.val,
meaning: toStringOrUndefined(notes?.childWithAttribute('category', 'meaning')?.children),
description: toStringOrUndefined(notes?.childWithAttribute('category', 'description')?.children),
locations: notes?.children
.filter((n): n is XmlElement => n.type === 'element' && n.attr.category === 'location')
.map(note => {
Expand Down Expand Up @@ -49,10 +49,10 @@ export function fromXlf1(xlf1: string): TranslationFile {
return {
id: unit.attr.id,
source: toString(...unit.childNamed('source')!.children),
target: target ? toString(...target.children) : undefined,
target: toStringOrUndefined(target?.children),
state: target?.attr.state,
meaning: notes?.find(note => note.attr.from === 'meaning')?.val,
description: notes?.find(note => note.attr.from === 'description')?.val,
meaning: toStringOrUndefined(notes?.find(note => note.attr.from === 'meaning')?.children),
description: toStringOrUndefined(notes?.find(note => note.attr.from === 'description')?.children),
locations: unit.childrenNamed('context-group')
.map(contextGroup => ({
file: contextGroup.childWithAttribute('context-type', 'sourcefile')!.val,
Expand All @@ -67,6 +67,10 @@ function toString(...nodes: XmlNode[]): string {
return nodes.map(n => n.toString({preserveWhitespace: true, compressed: true})).join('');
}

function toStringOrUndefined(nodes: XmlNode[] | undefined): string | undefined {
return nodes ? toString(...nodes) : undefined;
}

export function toXlf2(translationFile: TranslationFile, options: Pick<Options, 'prettyNestedTags'>): string {
const doc = new XmlDocument(`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="${translationFile.sourceLang}">
<file id="ngi18n" original="ng.template">
Expand Down

0 comments on commit bdb592f

Please sign in to comment.