Skip to content

Commit

Permalink
Merge pull request #15 from medondo/main
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Dec 15, 2023
2 parents d13b1d6 + cf67ad1 commit 7b332f8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Options:
-d, --destination-file <destinationFile> merge destination
-o, --output-file <outputFile> output file, if not provided "merge destination" is overwritten
-e, -exclude-file <excludeFiles...> exclude all unit IDs of the provided file(s)
-w, --overwrite-with-translated overwrite target of destination with target of source, if it's translated and destination target not
--no-match-fuzzy prevent fuzzy matching of similar units with changed id
--no-collapse-whitespace prevent collapsing of multiple whitespaces when comparing translations sources
--no-reset-translation-state prevent (re-)setting the translation state to new/initial for new/changed units
Expand Down
59 changes: 59 additions & 0 deletions __tests__/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,65 @@ describe('merge', () => {
' </file>\n' +
'</xliff>'));
});

it('should update translation', () => {
const sourceFileContent = `
<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="appNotificationListSeeMore" datatype="html">
<source>see more...</source>
<target state="translated">mehr anzeigen...</target>
</trans-unit>
</body>
</file>
</xliff>`;

const destFileContent = `
<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="appNotificationListSeeMore" datatype="html">
<source>see more...</source>
<target state="new">see more...</target>
</trans-unit>
</body>
</file>
</xliff>`;

const result = merge(sourceFileContent, destFileContent, {overwriteTargetWithTranslated: true});

expect(norm(result)).toEqual(norm(sourceFileContent));
});

it('should update translation without target', () => {
const sourceFileContent = `
<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="appNotificationListSeeMore" datatype="html">
<source>see more...</source>
<target state="translated">mehr anzeigen...</target>
</trans-unit>
</body>
</file>
</xliff>`;

const destFileContent = `
<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="appNotificationListSeeMore" datatype="html">
<source>see more...</source>
</trans-unit>
</body>
</file>
</xliff>`;

const result = merge(sourceFileContent, destFileContent, {overwriteTargetWithTranslated: true});

expect(norm(result)).toEqual(norm(sourceFileContent));
});
});

function norm(xml: string): string {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const options = new Command()
.option('--no-collapse-whitespace', 'prevent collapsing of multiple whitespaces and trimming when comparing translations sources')
.option('--no-reset-translation-state', 'prevent (re-)setting the translation state to new/initial for new/changed units')
.option('--no-replace-apostrophe', 'prevent replacing of apostrophes (\') with "&apos;"')
.option('-w, --overwrite-with-translated', 'overwrite target of destination with target of source, if it\'s translated and destination target not')
.option('--debug', 'enable debug output')
.parse()
.opts();
Expand All @@ -30,6 +31,7 @@ const outString = merge(inFilesContent, destFileContent, {
collapseWhitespace: options.collapseWhitespace,
resetTranslationState: options.resetTranslationState,
replaceApostrophe: options.replaceApostrophe,
overwriteTargetWithTranslated: options.overwriteWithTranslated,
}, options.destinationFile);

fs.writeFileSync(options.outputFile ?? options.destinationFile, outString, {encoding: 'utf8'});
17 changes: 14 additions & 3 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MergeOptions = {
newTranslationTargetsBlank?: boolean | 'omit',
/** For untranslated units with initial state (state="initial" / state="new"), an updated source will be copied into the target (unless `newTranslationTargetsBlank='omit'/true`) */
syncTargetsWithInitialState?: boolean,
overwriteTargetWithTranslated?: boolean,
};

const FUZZY_THRESHOLD = 0.2;
Expand Down Expand Up @@ -234,6 +235,16 @@ export function mergeWithMapping(inFilesContent: string | string[], destFileCont
} else if (originTarget && !destTarget) {
const sourceIndex = destUnit.children.indexOf(destSource);
destUnit.children.splice(sourceIndex + 1, 0, originTarget);
} else if (options?.overwriteTargetWithTranslated === true && !isUntranslated(unit, xliffVersion, unitSourceText) && isUntranslated(destUnit, xliffVersion, destSourceText)) {
destSource.children = unitSource.children;
if (destTarget || options?.newTranslationTargetsBlank !== 'omit') {
const targetElement = destTarget ?? createTargetElement(destUnit, xliffVersion);
targetElement!.children = unitSource.children;
}
const translatedSource = originTarget?.children ? toString(...originTarget?.children) : '';
updateFirstAndLastChild(destSource);
syncOtherNodes(unit, destUnit, 'segment');
console.debug(`update element with id "${unit.attr.id}" with new target: ${unitSourceText} (was: ${translatedSource})`);
}
if (destUnit.attr.id !== unit.attr.id) {
console.debug(`matched unit with previous id "${destUnit.attr.id}" to new id: "${unit.attr.id}"`);
Expand All @@ -242,7 +253,6 @@ export function mergeWithMapping(inFilesContent: string | string[], destFileCont
destUnit.attr.id = unit.attr.id;
resetTranslationState(destUnit, xliffVersion, options);
}

syncOtherNodes(unit, destUnit, 'source', 'target', 'segment');
updateFirstAndLastChild(destUnit);
} else {
Expand Down Expand Up @@ -302,8 +312,9 @@ export function mergeWithMapping(inFilesContent: string | string[], destFileCont

const mergedContent = xmlDeclaration + revertApostrophes(destDoc.toString({
preserveWhitespace: true,
compressed: true
}), !options?.replaceApostrophe);
compressed: false
}), !options?.replaceApostrophe).replace(/^\s*[\r\n]/gm, '');

return [mergedContent, idMapping];
}

Expand Down

0 comments on commit 7b332f8

Please sign in to comment.