Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
BraceBalanceValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
9use TMessage;
10
18 public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
19 $definition = $message->definition();
20 $translation = $message->translation();
21 $balanceIssues = [];
22 $braceTypes = [
23 [ '{', '}' ],
24 [ '[', ']' ],
25 [ '(', ')' ],
26 ];
27
28 foreach ( $braceTypes as [ $open, $close ] ) {
29 $definitionBalance = $this->getBalance( $definition, $open, $close );
30 $translationBalance = $this->getBalance( $translation, $open, $close );
31
32 if ( $definitionBalance === 0 && $translationBalance !== 0 ) {
33 $balanceIssues[] = "$open$close: $translationBalance";
34 }
35 }
36
37 $issues = new ValidationIssues();
38 if ( $balanceIssues ) {
39 $params = [
40 [ 'PARAMS', $balanceIssues ],
41 [ 'COUNT', count( $balanceIssues ) ],
42 ];
43
44 // Create an issue if braces are unbalanced in translation, but balanced in the definition
45 $issue = new ValidationIssue( 'balance', 'brace', 'translate-checks-balance', $params );
46 $issues->add( $issue );
47 }
48
49 return $issues;
50 }
51
52 private function getBalance( string $source, string $str1, string $str2 ): int {
53 return substr_count( $source, $str1 ) - substr_count( $source, $str2 );
54 }
55}
Interface for message objects used by MessageCollection.
Definition Message.php:14
definition()
Get the message definition.
Definition Message.php:51
translation()
Get the message translation.