Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
NewlineValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
10
19 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
20 $translation = $message->translation();
21 $definition = $message->definition();
22
23 $definitionStartNewline = $this->getStartingNewLinesCount( $definition );
24 $translationStartNewline = $this->getStartingNewLinesCount( $translation );
25
26 $failingChecks = $this->validateStartingNewline(
27 $definitionStartNewline, $translationStartNewline
28 );
29
30 return $this->createIssues( $failingChecks );
31 }
32
33 protected function getStartingNewLinesCount( string $str ): int {
34 return strspn( $str, "\n" );
35 }
36
37 protected function getEndingNewLineCount( string $str ): int {
38 return strspn( strrev( $str ), "\n" );
39 }
40
41 protected function validateStartingNewline(
42 int $definitionStartNewline,
43 int $translationStartNewline
44 ): array {
45 $failingChecks = [];
46 if ( $definitionStartNewline < $translationStartNewline ) {
47 // Extra whitespace at beginning
48 $failingChecks[] = [
49 'extra-start',
50 $translationStartNewline - $definitionStartNewline
51 ];
52 } elseif ( $definitionStartNewline > $translationStartNewline ) {
53 // Missing whitespace at beginning
54 $failingChecks[] = [
55 'missing-start',
56 $definitionStartNewline - $translationStartNewline
57 ];
58 }
59
60 return $failingChecks;
61 }
62
63 protected function validateEndingNewline(
64 int $definitionEndNewline,
65 int $translationEndNewline
66 ): array {
67 $failingChecks = [];
68 if ( $definitionEndNewline < $translationEndNewline ) {
69 // Extra whitespace at end
70 $failingChecks[] = [
71 'extra-end',
72 $translationEndNewline - $definitionEndNewline
73 ];
74 } elseif ( $definitionEndNewline > $translationEndNewline ) {
75 // Missing whitespace at end
76 $failingChecks[] = [
77 'missing-end',
78 $definitionEndNewline - $translationEndNewline
79 ];
80 }
81
82 return $failingChecks;
83 }
84
85 protected function createIssues( array $failingChecks ): ValidationIssues {
86 $issues = new ValidationIssues();
87 foreach ( $failingChecks as [ $subType, $count ] ) {
88 $issue = new ValidationIssue(
89 'newline',
90 $subType,
91 "translate-checks-newline-$subType",
92 [ 'COUNT', $count ]
93 );
94
95 $issues->add( $issue );
96 }
97
98 return $issues;
99 }
100}
Interface for message objects used by MessageCollection.
Definition Message.php:13
Ensures that the translation has the same number of newlines as the source message at the beginning o...