Skip to content

Commit

Permalink
normalize translation target files before merging
Browse files Browse the repository at this point in the history
  • Loading branch information
mbey-mw committed Feb 21, 2024
1 parent 1ec2744 commit b6db5e4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
47 changes: 47 additions & 0 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,53 @@ describe('Builder', () => {
}
);
});
test('retain target state when equal after normalization', async () => {
await runTest(
{
messagesBefore: '<?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&apos; <x equiv-text="{{c}}" id="INTERPOLATION"/></source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>',
messagesFrBefore: '<?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 datatype="html" id="ID1">\n' +
' <source>source val\' <x id="INTERPOLATION" equiv-text="{{c}}"/>\n' +
' </source>\n' +
' <target state="signed-off">SOURCE VAL\' <x id="INTERPOLATION" equiv-text="{{c}}"/></target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>',
options: {
format: 'xlf',
targetFiles: ['messages.fr.xlf'],
outputPath: 'builder-test',
sortNestedTagAttributes: true,
collapseWhitespace: true,
trim: true,
},
messagesFrExpected: '<?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&apos; <x equiv-text="{{c}}" id="INTERPOLATION"/></source>\n' +
' <target state="signed-off">SOURCE VAL&apos; <x equiv-text="{{c}}" id="INTERPOLATION"/></target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
'</xliff>'
}
);
});
describe('trim', () => {
test('retain whitespaces when trim=false and collapseWhitespace=false', async () => {
await runTest(
Expand Down
17 changes: 11 additions & 6 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,25 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext
const targetPath = join(normalize(outputPath), targetFile);
context.logger.info(`merge and normalize ${targetPath} ...`);
const translationTargetFileContent = await readFileIfExists(targetPath);
const translationTargetFile = translationTargetFileContent ? fromXlf(translationTargetFileContent) : new TranslationFile([], translationSourceFile.sourceLang, targetPath?.match(/\.([a-zA-Z-]+)\.xlf$/)?.[1] ?? 'en');
const translationTargetFileRaw = translationTargetFileContent ? fromXlf(translationTargetFileContent) : new TranslationFile([], translationSourceFile.sourceLang, targetPath?.match(/\.([a-zA-Z-]+)\.xlf$/)?.[1] ?? 'en');
const translationTargetFile = translationTargetFileRaw.mapUnitsList(units => units
.filter(filterUnits)
.map(unit => ({
...unit,
source: mapper(unit.source),
target: unit.target !== undefined ? mapper(unit.target) : undefined,
meaning: (options.includeMeaningAndDescription ?? true) ? mapper(unit.meaning) : undefined,
description: (options.includeMeaningAndDescription ?? true) ? mapper(unit.description) : undefined,
}))
);
const isSourceLang = targetFile === options.sourceLanguageTargetFile;

const mergedTarget = merger.mergeWithMapping(translationTargetFile, isSourceLang);
const normalizedTarget = mergedTarget.mapUnitsList(units => {
const updatedUnits = units
.filter(filterUnits)
.map(unit => ({
...unit,
source: mapper(unit.source),
target: unit.target !== undefined ? mapper(unit.target) : undefined,
locations: options.includeContext === true ? unit.locations : [],
meaning: (options.includeMeaningAndDescription ?? true) ? mapper(unit.meaning) : undefined,
description: (options.includeMeaningAndDescription ?? true) ? mapper(unit.description) : undefined,
// reset to original state, if source was changed to target from sourceLangTarget:
state: idsOfUnitsWithSourceChangedToSourceLangTarget.has(unit.id) ? (translationTargetFile.units.find(u => u.id === unit.id)?.state ?? unit.state) : unit.state
}));
Expand Down

0 comments on commit b6db5e4

Please sign in to comment.