Skip to content

Commit

Permalink
Copy from Source for Languages setting (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanbekkum authored May 18, 2020
1 parent ce82091 commit e98ca0a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* New setting `xliffSync.detectSourceTextChanges` (see README)
* New setting `xliffSync.detectSourceTextChanges` (GitHub issue [#41](https://github.com/rvanbekkum/vsc-xliff-sync/issues/41))
* New setting `xliffSync.copyFromSourceForLanguages` (GitHub issue [#44](https://github.com/rvanbekkum/vsc-xliff-sync/issues/44))

## [0.3.6] 28-02-2020

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Apart from synchronizing trans-units from a base-XLIFF file, this extension cont
| xliffSync.findBySource | `false` | Specifies whether or not the extension will try to find translations by source. If there are multiple trans-units with the same source, then the translation of the first translation unit is used for all units. |
| xliffSync.parseFromDeveloperNote | `false` | Specifies whether translations should be parsed from the developer note. Translations can be retrieved from a Developer note in the following format: <code>en-US=My translation&#124;nl-NL=Mijn vertaling</code>. |
| xliffSync.parseFromDeveloperNoteSeparator | <code>&#124;</code> | Specifies the separator that is used when translations are parsed from the developer note. |
| xliffSync.copyFromSourceForLanguages | `[]` | Specifies the languages for which translations should be copied from the source text of trans-units. |
| xliffSync.copyFromSourceForSameLanguage | `false` | Specifies whether translations should be copied from the source text if source-language = target-language. This will **not** overwrite existing translations of trans-units in target files. |
| xliffSync.detectSourceTextChanges | `true` | Specifies whether changes in the source text of a trans-unit should be detected. If a change is detected, the target state is changed to needs-adaptation and a note is added to indicate the translation should be reviewed. |
| xliffSync.ignoreLineEndingTypeChanges | `false` | Specifies whether changes in line ending type (CRLF vs. LF) should not be considered as changes to the source text of a trans-unit. |
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
"description": "Specifies the separator that is used when translations are parsed from the developer note.",
"scope": "resource"
},
"xliffSync.copyFromSourceForLanguages": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"uniqueItems": true,
"description": "Specifies the languages for which translations should be copied from the source text of trans-units.",
"scope": "resource"
},
"xliffSync.copyFromSourceForSameLanguage": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -167,7 +177,7 @@
"Checks that the leading spaces of members of the OptionCaption or PromotedActionCategories property match.",
"Checks that the number of placeholders match.",
"Checks that the meaning of placeholders are explained in the Developer note.",
"Checks that the source and target are the same for files where source and target language are the same."
"Checks that the source and target are the same for files with target language for which translations are copied from the source text."
]
},
"default": [
Expand Down
2 changes: 2 additions & 0 deletions src/features/tools/xlf-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class XlfTranslator {
const findByXliffGeneratorNote: boolean = xliffWorkspaceConfiguration['findByXliffGeneratorNote'];
const findBySourceAndDeveloperNote: boolean = xliffWorkspaceConfiguration['findBySourceAndDeveloperNote'];
const findBySource: boolean = xliffWorkspaceConfiguration['findBySource'];
const copyFromSourceForLanguages: string[] = xliffWorkspaceConfiguration['copyFromSourceForLanguages'];
const copyFromSourceForSameLanguage: boolean = xliffWorkspaceConfiguration['copyFromSourceForSameLanguage'];
const parseFromDeveloperNote: boolean = xliffWorkspaceConfiguration['parseFromDeveloperNote'];
const detectSourceTextChanges: boolean = xliffWorkspaceConfiguration['detectSourceTextChanges'];
Expand All @@ -62,6 +63,7 @@ export class XlfTranslator {
if (language) {
mergedDocument.targetLanguage = language;
copyFromSource = copyFromSourceForSameLanguage && (mergedDocument.sourceLanguage === language);
copyFromSource = copyFromSource || (copyFromSourceForLanguages.indexOf(language) >= 0);
}

let sourceTranslations: { [key: string]: string | undefined; } = {};
Expand Down
24 changes: 18 additions & 6 deletions src/features/trans-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export async function runTranslationChecksForWorkspaceFolder(checkWorkspaceFolde
return needWorkRulesEnableAll || needWorkRules.indexOf(ruleName) >= 0;
}

const copyFromSourceForLanguages: string[] = workspace.getConfiguration('xliffSync', checkWorkspaceFolder.uri)[
'copyFromSourceForLanguages'
];
let sourceEqualsTargetExpected: boolean = false;

let noMissingTranslations: boolean = true;
let noNeedWorkTranslations: boolean = true;
for (let index = 0; index < targetUris.length; index++) {
Expand All @@ -319,12 +324,17 @@ export async function runTranslationChecksForWorkspaceFolder(checkWorkspaceFolde
let needWorkCount: number = 0;
let problemResolvedInFile: boolean = false;
const targetDocument = await XlfDocument.load(checkWorkspaceFolder.uri, target);
sourceEqualsTargetExpected = targetDocument.sourceLanguage === targetDocument.targetLanguage;
if (targetDocument.targetLanguage) {
sourceEqualsTargetExpected = sourceEqualsTargetExpected || (copyFromSourceForLanguages.indexOf(targetDocument.targetLanguage) >= 0);
}

targetDocument.translationUnitNodes.forEach((unit) => {
if (shouldCheckForMissingTranslations && checkForMissingTranslation(targetDocument, unit, missingTranslationText)) {
targetDocument.setTargetAttribute(unit, 'state', 'needs-translation');
missingCount += 1;
}
if (shouldCheckForNeedWorkTranslations && checkForNeedWorkTranslation(targetDocument, unit, isRuleEnabledChecker)) {
if (shouldCheckForNeedWorkTranslations && checkForNeedWorkTranslation(targetDocument, unit, isRuleEnabledChecker, sourceEqualsTargetExpected)) {
targetDocument.setTargetAttribute(unit, 'state', 'needs-adaptation');
needWorkCount += 1;
}
Expand Down Expand Up @@ -393,18 +403,20 @@ function checkForMissingTranslation(targetDocument: XlfDocument, unit: XmlNode,
return false;
}

function checkForNeedWorkTranslation(targetDocument: XlfDocument, unit: XmlNode, isRuleEnabled: (ruleName: string) => boolean) : boolean {
function checkForNeedWorkTranslation(targetDocument: XlfDocument, unit: XmlNode, isRuleEnabled: (ruleName: string) => boolean, sourceEqualsTargetExpected: boolean) : boolean {
const sourceText = targetDocument.getUnitSourceText(unit);
const translText = targetDocument.getUnitTranslation(unit);
const devNoteText = targetDocument.getUnitDeveloperNote(unit) || '';
if (!sourceText || !translText) {
return false;
}

if (targetDocument.sourceLanguage === targetDocument.targetLanguage) {
if (isRuleEnabled('SourceEqualsTarget') && sourceText !== translText) {
targetDocument.setXliffSyncNote(unit, 'Problem detected: The source text is not the same as the translation, but the source-language is the same as the target-language.');
return true;
if (isRuleEnabled('SourceEqualsTarget')) {
if (sourceEqualsTargetExpected) {
if (sourceText !== translText) {
targetDocument.setXliffSyncNote(unit, 'Problem detected: The source text is not the same as the translation, but the source-language is the same as the target-language.');
return true;
}
}
}

Expand Down

0 comments on commit e98ca0a

Please sign in to comment.