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
|
<?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;
/**
* Handles brace balance validation
* @author Abijeet Patro
* @license GPL-2.0-or-later
* @since 2019.06
*/
class BraceBalanceValidator implements MessageValidator {
public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
$definition = $message->definition();
$translation = $message->translation();
$balanceIssues = [];
$braceTypes = [
[ '{', '}' ],
[ '[', ']' ],
[ '(', ')' ],
];
foreach ( $braceTypes as [ $open, $close ] ) {
$definitionBalance = $this->getBalance( $definition, $open, $close );
$translationBalance = $this->getBalance( $translation, $open, $close );
if ( $definitionBalance === 0 && $translationBalance !== 0 ) {
$balanceIssues[] = "$open$close: $translationBalance";
}
}
$issues = new ValidationIssues();
if ( $balanceIssues ) {
$params = [
[ 'PARAMS', $balanceIssues ],
[ 'COUNT', count( $balanceIssues ) ],
];
// Create an issue if braces are unbalanced in translation, but balanced in the definition
$issue = new ValidationIssue( 'balance', 'brace', 'translate-checks-balance', $params );
$issues->add( $issue );
}
return $issues;
}
private function getBalance( string $source, string $str1, string $str2 ): int {
return substr_count( $source, $str1 ) - substr_count( $source, $str2 );
}
}
|