1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extensions\Translate\MessageValidator\Validators;
use MediaWiki\Extensions\Translate\Validation\MessageValidator;
use MediaWiki\Extensions\Translate\Validation\ValidationIssue;
use MediaWiki\Extensions\Translate\Validation\ValidationIssues;
use TMessage;
/**
* Ensures that the translation has the same number of newlines as the source
* message at the beginning of the string.
* @author Abijeet Patro
* @license GPL-2.0-or-later
* @since 2019.09
*/
class NewlineValidator implements MessageValidator {
public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
$translation = $message->translation();
$definition = $message->definition();
$definitionStartNewline = $this->getStartingNewLinesCount( $definition );
$translationStartNewline = $this->getStartingNewLinesCount( $translation );
$failingChecks = $this->validateStartingNewline(
$definitionStartNewline, $translationStartNewline
);
return $this->createIssues( $failingChecks );
}
protected function getStartingNewLinesCount( string $str ): int {
return strspn( $str, "\n" );
}
protected function getEndingNewLineCount( string $str ): int {
return strspn( strrev( $str ), "\n" );
}
protected function validateStartingNewline(
int $definitionStartNewline,
int $translationStartNewline
): array {
$failingChecks = [];
if ( $definitionStartNewline < $translationStartNewline ) {
// Extra whitespace at beginning
$failingChecks[] = [
'extra-start',
$translationStartNewline - $definitionStartNewline
];
} elseif ( $definitionStartNewline > $translationStartNewline ) {
// Missing whitespace at beginnning
$failingChecks[] = [
'missing-start',
$definitionStartNewline - $translationStartNewline
];
}
return $failingChecks;
}
protected function validateEndingNewline(
int $definitionEndNewline,
int $translationEndNewline
): array {
$failingChecks = [];
if ( $definitionEndNewline < $translationEndNewline ) {
// Extra whitespace at end
$failingChecks[] = [
'extra-end',
$translationEndNewline - $definitionEndNewline
];
} elseif ( $definitionEndNewline > $translationEndNewline ) {
// Missing whitespace at end
$failingChecks[] = [
'missing-end',
$definitionEndNewline - $translationEndNewline
];
}
return $failingChecks;
}
protected function createIssues( array $failingChecks ): ValidationIssues {
$issues = new ValidationIssues();
foreach ( $failingChecks as [ $subType, $count ] ) {
$issue = new ValidationIssue(
'newline',
$subType,
"translate-checks-newline-$subType",
[ 'COUNT', $count ]
);
$issues->add( $issue );
}
return $issues;
}
}
|