Skip to content

Commit

Permalink
Merge pull request #29 from daniel-sc/28-fix-trimming
Browse files Browse the repository at this point in the history
28 fix trimming - fixes #28
  • Loading branch information
daniel-sc authored May 24, 2022
2 parents a921dac + 513c274 commit 450b0a9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ In your `angular.json` you'll find a new target `extract-i18n-merge` that can be
| `removeIdsWithPrefix` | `[]` | List of prefix strings. All translation units with matching `id` attribute are removed. Useful for excluding duplicate library translations. |
| `fuzzyMatch` | `true` | Whether translation units without matching IDs are fuzzy matched by source text. |
| `resetTranslationState` | `true` | Reset the translation state to new/initial for new/changed units. |
| `collapseWhitespace` | `true` | Collapsing of multiple whitespaces and trimming when comparing translations sources. |
| `collapseWhitespace` | `true` | Collapsing of multiple whitespaces/line breaks in translation sources and targets. |
| `includeContext` | `false` | Whether to include the context information (like notes) in the translation files. This is useful for sending the target translation files to translation agencies/services. |
| `newTranslationTargetsBlank` | `false` | When `false` (default) the "target" of new translation units is set to the "source" value. When `true`, an empty string is used. When `'omit'`, no target element is created. |
| `sort` | `"idAsc"` | Sorting of all translation units in source and target translation files. Supported: `"idAsc"` (sort by translation IDs), `"stableAppendNew"` (keep existing sorting, append new translations at the end) |
| `sort` | `"stableAppendNew"` | Sorting of all translation units in source and target translation files. Supported: `"idAsc"` (sort by translation IDs), `"stableAppendNew"` (keep existing sorting, append new translations at the end) |

## Contribute

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-extract-i18n-merge",
"version": "1.4.0",
"version": "2.0.0-0",
"description": "Extract and merge i18n xliff translation files for angular projects.",
"main": "index.js",
"builders": "builders.json",
Expand Down Expand Up @@ -35,7 +35,7 @@
"@angular-devkit/schematics": "^13.0.0",
"@schematics/angular": "^13.0.0",
"xliff-simple-merge": "~0.12.0",
"xml_normalize": "~0.8.3",
"xml_normalize": "~0.8.4",
"xmldoc": "~1.1.2"
},
"devDependencies": {
Expand Down
64 changes: 62 additions & 2 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,66 @@ describe('Builder', () => {
' </file>\n' +
'</xliff>');
});
test('retain leading and trailing whitespaces', async () => {
await fs.writeFile('builder-test/messages.xlf', '<?xml version="1.0"?>\n' +
'<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 val </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>', 'utf8');
await fs.writeFile('builder-test/messages.fr.xlf', '<?xml version="1.0"?>\n' +
'<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>', 'utf8');

// A "run" can have multiple outputs, and contains progress information.
const run = await architect.scheduleTarget({project: 'builder-test', target: 'extract-i18n-merge'}, {
format: 'xlf',
targetFiles: ['messages.fr.xlf'],
outputPath: 'builder-test'
});

// The "result" member (of type BuilderOutput) is the next output.
await run.result;
const result = await run.result;
expect(result.success).toBeTruthy();

// Stop the builder from running. This stops Architect from keeping
// the builder-associated states in memory, since builders keep waiting
// to be scheduled.
await run.stop();

const sourceContent = await fs.readFile('builder-test/messages.xlf', 'utf8');
expect(sourceContent).toEqual('<?xml version="1.0"?>\n' +
'<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 val </source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>');
const targetContent = await fs.readFile('builder-test/messages.fr.xlf', 'utf8');
expect(targetContent).toEqual('<?xml version="1.0"?>\n' +
'<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 val </source>\n' +
' <target state="new"> source val </target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>');
});
describe('multiple context groups', () => {
beforeEach(async () => {
await fs.writeFile('builder-test/messages.xlf', '<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">\n' +
Expand Down Expand Up @@ -824,7 +884,7 @@ describe('Builder', () => {
format: 'xlf',
targetFiles: ['messages.fr.xlf'],
outputPath: 'builder-test',
// default: sort: 'idAsc'
sort: 'idAsc'
});
await run.result;
const result = await run.result;
Expand Down Expand Up @@ -881,7 +941,7 @@ describe('Builder', () => {
format: 'xlf',
targetFiles: ['messages.fr.xlf'],
outputPath: 'builder-test',
sort: 'stableAppendNew'
// default: sort: 'stableAppendNew'
});
await run.result;
const result = await run.result;
Expand Down
5 changes: 4 additions & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function resetSortOrder(originalTranslationSourceFile: string, updatedTranslatio
// we need to reformat the xml (whitespaces are messed up by the sort):
return xmlNormalize({
in: xmlDeclaration + updatedTranslationSourceDoc.toString({preserveWhitespace: true, compressed: true}),
trim: false,
normalizeWhitespace: true
});
}
Expand Down Expand Up @@ -95,9 +96,10 @@ async function copyFileBuilder(options: Options, context: BuilderContext): Promi
...(options.removeIdsWithPrefix ?? []).map(removePrefix => isXliffV2 ? `/xliff/file/unit[starts-with(@id,"${removePrefix}")]` : `/xliff/file/body/trans-unit[starts-with(@id,"${removePrefix}")]`)
];
const idPath = isXliffV2 ? '/xliff/file/unit/@id' : '/xliff/file/body/trans-unit/@id';
const sort: Options['sort'] = options.sort ?? 'idAsc';
const sort: Options['sort'] = options.sort ?? 'stableAppendNew';
const normalizedTranslationSourceFile = xmlNormalize({
in: translationSourceFile,
trim: false,
normalizeWhitespace: true,
sortPath: sort === 'idAsc' ? idPath : undefined,
removePath: removePaths
Expand All @@ -116,6 +118,7 @@ async function copyFileBuilder(options: Options, context: BuilderContext): Promi
});
const normalizedTarget = xmlNormalize({
in: mergedTarget,
trim: false,
normalizeWhitespace: true,
// no sorting for 'stableAppendNew' as this is the default merge behaviour:
sortPath: sort === 'idAsc' ? idPath : undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@
"idAsc",
"stableAppendNew"
],
"default": "idAsc",
"default": "stableAppendNew",
"description": "Sorting of all translation units in source and target translation files."
},
"collapseWhitespace": {
"type": "boolean",
"default": true,
"description": "Collapsing of multiple whitespaces and trimming when comparing translations sources."
"description": "Collapsing of multiple whitespaces/line breaks in translation sources and targets."
},
"includeContext": {
"type": "boolean",
Expand Down

0 comments on commit 450b0a9

Please sign in to comment.