Skip to content

Commit

Permalink
closes #128: State is set back to translated even if source did not c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
martinroob committed Apr 6, 2019
1 parent ed151fd commit b98b238
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
28 changes: 28 additions & 0 deletions projects/xliffmerge/src/xliffmerge/xlf12-merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,34 @@ describe('XliffMerge XLIFF 1.2 format tests', () => {
done();
});

it('should not change translation state when source is unchanged but has different line ending (#128)', (done) => {
FileUtil.copy(SRCDIR + 'issue128master.xlf', MASTER);
const langfileName = WORKDIR + 'messages.en.xlf';
FileUtil.copy(SRCDIR + 'issue128translated.xlf', langfileName);
const originalContent = FileUtil.read(langfileName, 'UTF-8');
const contentWithoutCRLF = originalContent.replace(/[\n\r]/g, '');
expect(originalContent).not.toBe(contentWithoutCRLF);
FileUtil.replaceContent(langfileName, contentWithoutCRLF, 'UTF-8');
const ws: WriterToString = new WriterToString();
const commandOut = new CommandOutput(ws);
const profileContent: IConfigFile = {
xliffmergeOptions: {
defaultLanguage: 'de',
srcDir: WORKDIR,
genDir: WORKDIR,
i18nFile: MASTERFILE,
beautifyOutput: false
}
};
const xliffMergeCmd = XliffMerge.createFromOptions(commandOut, {languages: ['de', 'en']}, profileContent);
xliffMergeCmd.run();
expect(ws.writtenData()).not.toContain('ERROR');
const langFileEnglish: ITranslationMessagesFile = readXliff(xliffMergeCmd.generatedI18nFile('en'));
const tu: ITransUnit = langFileEnglish.transUnitWithId('issue128');
expect(tu.targetState()).toBe(STATE_FINAL);
done();
});

describe('autotranslate via google translate', () => {

let apikey: string;
Expand Down
25 changes: 19 additions & 6 deletions projects/xliffmerge/src/xliffmerge/xliff-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {AutoTranslateSummaryReport} from '../autotranslate/auto-translate-summar

export class XliffMerge {

private commandOutput: CommandOutput;
private readonly commandOutput: CommandOutput;

private options: ProgramOptions;
private readonly options: ProgramOptions;

private parameters: XliffMergeParameters;

Expand Down Expand Up @@ -309,7 +309,7 @@ export class XliffMerge {
const languageSpecificMessagesFile: ITranslationMessagesFile =
this.master.createTranslationFileForLang(lang, languageXliffFilePath, isDefaultLang, this.parameters.useSourceAsTarget());
return this.autoTranslate(this.master.sourceLanguage(), lang, languageSpecificMessagesFile).pipe(
map((summary) => {
map((/* summary */) => {
// write it to file
TranslationMessagesFileReader.save(languageSpecificMessagesFile, this.parameters.beautifyOutput());
this.commandOutput.info('created new file "%s" for target-language="%s"', languageXliffFilePath, lang);
Expand Down Expand Up @@ -375,7 +375,7 @@ export class XliffMerge {
} else {
// check for changed source content and change it if needed
// (can only happen if ID is explicitely set, otherwise ID would change if source content is changed.
if (transUnit.supportsSetSourceContent() && masterTransUnit.sourceContent() !== transUnit.sourceContent()) {
if (transUnit.supportsSetSourceContent() && !this.areSourcesNearlyEqual(masterTransUnit, transUnit)) {
transUnit.setSourceContent(masterTransUnit.sourceContent());
if (isDefaultLang) {
// #81 changed source must be copied to target for default lang
Expand Down Expand Up @@ -479,10 +479,9 @@ export class XliffMerge {
languageSpecificMessagesFile: ITranslationMessagesFile,
lastProcessedUnit: ITransUnit): ITransUnit {

const masterSourceString = masterTransUnit.sourceContentNormalized().asDisplayString(NORMALIZATION_FORMAT_DEFAULT).trim();
let changedTransUnit: ITransUnit = null;
languageSpecificMessagesFile.forEachTransUnit((languageTransUnit) => {
if (languageTransUnit.sourceContentNormalized().asDisplayString(NORMALIZATION_FORMAT_DEFAULT).trim() === masterSourceString) {
if (this.areSourcesNearlyEqual(languageTransUnit, masterTransUnit)) {
changedTransUnit = languageTransUnit;
}
});
Expand All @@ -502,6 +501,20 @@ export class XliffMerge {
return mergedTransUnit;
}

/**
* test wether the sources of 2 trans units are equal ignoring white spaces.
* @param tu1 tu1
* @param tu2 tu2
*/
private areSourcesNearlyEqual(tu1: ITransUnit, tu2: ITransUnit): boolean {
if ((tu1 && !tu2) || (tu2 && !tu1)) {
return false;
}
const s1Normalized = tu1.sourceContentNormalized().asDisplayString(NORMALIZATION_FORMAT_DEFAULT).trim();
const s2Normalized = tu2.sourceContentNormalized().asDisplayString(NORMALIZATION_FORMAT_DEFAULT).trim();
return s1Normalized === s2Normalized;
}

private areSourceReferencesEqual(
ref1: {sourcefile: string; linenumber: number; }[],
ref2: {sourcefile: string; linenumber: number; }[]): boolean {
Expand Down
16 changes: 16 additions & 0 deletions projects/xliffmerge/test/testdata/i18n/issue128master.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- issue 128 when source is unchanged but has different line ending translation state should not change -->
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="issue128" datatype="html">
<source>
Ein CR-LF-Problem</source>
<context-group purpose="location">
<context context-type="sourcefile">awesome.component.html</context>
<context context-type="linenumber">2</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
18 changes: 18 additions & 0 deletions projects/xliffmerge/test/testdata/i18n/issue128translated.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- issue 128 when source is unchanged but has different line ending translation state should not change -->
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file datatype="plaintext" date="2018-11-14T14:52:51.3060268Z" original="3223941_9" source-language="de" target-language="en">
<body>
<trans-unit id="issue128" datatype="html">
<source>
Ein CR-LF-Problem</source>
<target state="final">
A CR-LF problem</target>
<context-group purpose="location">
<context context-type="sourcefile">awesome.component.html</context>
<context context-type="linenumber">2</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit b98b238

Please sign in to comment.