Skip to content

Commit

Permalink
Merge pull request #28 from rvanbekkum/feature/25-consecutive-spaces
Browse files Browse the repository at this point in the history
Consecutive Spaces Checks
  • Loading branch information
rvanbekkum authored Oct 20, 2019
2 parents cb40269 + afc3308 commit 72eef24
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* Fix remove duplicate language tag entry `ja-JP` which shows up calling `XLIFF: Create New Target File(s)`.
* Changed `XLIFF: Create New Target File(s)` command adding two options **Select multiple..** (to select multiple target languages to create a file) and **Enter custom...** (to enter a custom target language tag).
* Added the following new technical checks (disabled by default):

* `ConsecutiveSpacesConsistent` - Checks that the 'consecutive space'-occurrences match in source and translation.
* `ConsecutiveSpacesExist` - Checks whether consecutive spaces exist in the source or translation text.

## [0.3.0] 15-09-2019

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ The currently implemented checks are the following:

| Rule ID | Check | Trigger | Example |
|-----------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------|
| `ConsecutiveSpacesConsistent` | 'Consecutive space'-occurrences are not matching. | N.A. | The source text includes 3 consecutive spaces, the translation text includes 2 consecutive spaces. |
| `ConsecutiveSpacesExist` | Source or translation text contain consecutive spaces. | N.A. | The source text includes 2 consecutive spaces, e.g., <pre>Hello World</pre> |
| `OptionMemberCount` | Number of options in caption are not matching. | Xliff Generator note with `Property OptionCaption` or `Property PromotedActionCategories`. | The source text includes 3 options, `A,B,C` , but the translation text includes 4 options, `A,B,C,D`. |
| `OptionLeadingSpaces` | Number of leading spaces in options are not matching. | Xliff Generator note with `Property OptionCaption` or `Property PromotedActionCategories`. | The source text includes a space, `A, B` , but the translation text does not, `A,B`. |
| `Placeholders` | Placeholders of source and translation are not matching. | Source/Translation text includes placeholders of the form `{0}` or `%1`. | The source text includes placeholders `%1 %2` , but the translation text only includes `%1`. |
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@
"items": {
"type" : "string",
"enum": [
"ConsecutiveSpacesConsistent",
"ConsecutiveSpacesExist",
"OptionMemberCount",
"OptionLeadingSpaces",
"Placeholders",
"SourceEqualsTarget"
],
"enumDescriptions": [
"Checks that the 'consecutive space'-occurrences match.",
"Checks whether consecutive spaces exist in the source or translation text.",
"Checks that the number of members of the OptionCaption or PromotedActionCategories property match.",
"Checks that the leading spaces of members of the OptionCaption or PromotedActionCategories property match.",
"Checks that the number of placeholders match.",
Expand Down
40 changes: 39 additions & 1 deletion src/features/trans-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ function checkForNeedWorkTranslation(targetDocument: XlfDocument, unit: XmlNode,
}
}

if (needWorkRules.indexOf('ConsecutiveSpacesExist') >= 0) {
if (checkForConsecutiveSpaces(sourceText)) {
targetDocument.setXliffSyncNote(unit, 'Problem detected: Consecutive spaces exist in the source text.');
return true;
}
if (checkForConsecutiveSpaces(translText)) {
targetDocument.setXliffSyncNote(unit, 'Problem detected: Consecutive spaces exist in the translation text.');
return true;
}
}

if (needWorkRules.indexOf('ConsecutiveSpacesConsistent') >= 0 && checkForConsecutiveSpacesInconsistency(sourceText, translText)) {
targetDocument.setXliffSyncNote(unit, 'Problem detected: The "consecutive space"-occurrences in source and translation text do not match.');
return true;
}

if (targetDocument.getTargetAttribute(unit, 'state') === 'needs-adaptation') {
return true;
}
Expand Down Expand Up @@ -414,7 +430,7 @@ function checkForOptionMemberCountMismatch(sourceText: string, translationText:
return noOfCommasSource !== noOfCommasTransl;
}

function checkForOptionMemberLeadingSpacesMismatch(sourceText: string, translationText: string) {
function checkForOptionMemberLeadingSpacesMismatch(sourceText: string, translationText: string): boolean {
const sourceValues: string[] = sourceText.split(',');
const translValues: string[] = translationText.split(',');
for (let i in sourceValues) {
Expand All @@ -426,3 +442,25 @@ function checkForOptionMemberLeadingSpacesMismatch(sourceText: string, translati
}
return false;
}

function getConsecutiveSpacesMatchesFromText(textToCheck: string): RegExpMatchArray {
return (textToCheck.match(/\s\s+/g) || []);
}

function checkForConsecutiveSpaces(textToCheck: string): boolean {
return getConsecutiveSpacesMatchesFromText(textToCheck).length !== 0;
}

function checkForConsecutiveSpacesInconsistency(sourceText: string, translationText: string): boolean {
const sourceTextConsecutiveSpaces = getConsecutiveSpacesMatchesFromText(sourceText);
const translTextConsecutiveSpaces = getConsecutiveSpacesMatchesFromText(translationText);
if (sourceTextConsecutiveSpaces.length !== translTextConsecutiveSpaces.length) {
return true;
}
for (let i in sourceTextConsecutiveSpaces) {
if (sourceTextConsecutiveSpaces[i].length !== translTextConsecutiveSpaces[i].length) {
return true;
}
}
return false;
}

0 comments on commit 72eef24

Please sign in to comment.