Skip to content

Commit

Permalink
fix: trim/normalize description + meaning
Browse files Browse the repository at this point in the history
fixes #81
  • Loading branch information
daniel-sc committed Oct 28, 2023
1 parent 93f08b2 commit fc0ea56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
37 changes: 37 additions & 0 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,43 @@ describe('Builder', () => {
}
);
});
test('remove newline in description of xlf2 when trim=false and collapseWhitespace=true', async () => {
await runTest({
messagesBefore: '<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="de">\n' +
' <file id="ngi18n" original="ng.template">\n' +
' <unit id="ID1">\n' +
' <notes>\n' +
' <note category="location">some-file.ts:281,286</note>\n' +
' <note category="location">some-other-file.ts:281</note>\n' +
' <note category="description">test description\n' +
'</note>\n' +
' </notes>\n' +
' <segment>\n' +
' <source>source val</source>\n' +
' </segment>\n' +
' </unit>\n' +
' </file>\n' +
'</xliff>',
options: {
format: 'xlf2',
includeContext: true
},
messagesExpected: '<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="de">\n' +
' <file id="ngi18n" original="ng.template">\n' +
' <unit id="ID1">\n' +
' <notes>\n' +
' <note category="location">some-file.ts:281,286</note>\n' +
' <note category="location">some-other-file.ts:281</note>\n' +
' <note category="description">test description </note>\n' +
' </notes>\n' +
' <segment>\n' +
' <source>source val</source>\n' +
' </segment>\n' +
' </unit>\n' +
' </file>\n' +
'</xliff>',
});
});
test('retain whitespaces when trim=false and collapseWhitespace=true', async () => {
await runTest(
{
Expand Down
12 changes: 6 additions & 6 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext
const translationSourceFile = fromXlf(await fs.readFile(sourcePath, 'utf8'));

const sort: Options['sort'] = options.sort ?? 'stableAppendNew';
const identityMapper = (x: string) => x;
const identityMapper = <T extends string | undefined>(x: T) => x;
const mapper = pipe(
(options.collapseWhitespace ?? true) ? doCollapseWhitespace : identityMapper,
(options.trim ?? false) ? ((text: string) => text.trim()) : identityMapper
options.trim ?? false ? <T extends string | undefined>(text: T): T => text?.trim() as T : identityMapper
);
const removeContextSource = options.includeContext !== true && options.includeContext !== 'sourceFileOnly';
const normalizedTranslationSourceFile = translationSourceFile.mapUnitsList(units => {
Expand All @@ -126,8 +126,8 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext
source: mapper(unit.source),
target: unit.target !== undefined ? mapper(unit.target) : undefined,
locations: removeContextSource ? [] : unit.locations,
description: removeContextSource ? undefined : unit.description,
meaning: removeContextSource ? undefined : unit.meaning
description: removeContextSource ? undefined : mapper(unit.description),
meaning: removeContextSource ? undefined : mapper(unit.meaning)
}));
if (sort === 'idAsc') {
return updatedUnits.sort((a, b) => a.id.localeCompare(b.id));
Expand Down Expand Up @@ -157,8 +157,8 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext
source: mapper(unit.source),
target: unit.target !== undefined ? mapper(unit.target) : undefined,
locations: options.includeContext === true ? unit.locations : [],
meaning: options.includeContext === true ? unit.meaning : undefined,
description: options.includeContext === true ? unit.description : undefined,
meaning: options.includeContext === true ? mapper(unit.meaning) : undefined,
description: options.includeContext === 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
6 changes: 2 additions & 4 deletions src/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


export function doCollapseWhitespace(destSourceText: string): string {
return destSourceText.replace(/\s+/g, ' ');
export function doCollapseWhitespace<T extends string | undefined>(destSourceText: T): T {
return destSourceText?.replace(/\s+/g, ' ') as T;
}

0 comments on commit fc0ea56

Please sign in to comment.