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
101
102
103
104
105
106
107
108
109
110
111
112
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\Validation\Validators;
use MediaWiki\Extension\Translate\Utilities\UnicodePlural;
use MediaWiki\Extension\Translate\Validation\MessageValidator;
use MediaWiki\Extension\Translate\Validation\ValidationIssue;
use MediaWiki\Extension\Translate\Validation\ValidationIssues;
use TMessage;
/**
* This is a very strict validator class for Unicode CLDR based plural markup.
*
* It requires all forms to be present and in correct order. Whitespace around keywords
* and values is trimmed. The keyword `other` is left out, though it is allowed in input.
* @since 2019.09
* @license GPL-2.0-or-later
*/
class UnicodePluralValidator implements MessageValidator {
public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
$issues = new ValidationIssues();
$expectedKeywords = UnicodePlural::getPluralKeywords( $targetLanguage );
// Skip validation for languages for which we do not know the plural rule
if ( $expectedKeywords === null ) {
return $issues;
}
$definition = $message->definition();
$translation = $message->translation();
$definitionHasPlural = UnicodePlural::hasPlural( $definition );
$translationHasPlural = UnicodePlural::hasPlural( $translation );
$presence = $this->pluralPresenceCheck(
$definitionHasPlural,
$translationHasPlural
);
// Using same check keys as MediaWikiPluralValidator
if ( $presence === 'missing' ) {
$issue = new ValidationIssue( 'plural', 'missing', 'translate-checks-unicode-plural-missing' );
$issues->add( $issue );
} elseif ( $presence === 'unsupported' ) {
$issue = new ValidationIssue( 'plural', 'unsupported', 'translate-checks-unicode-plural-unsupported' );
$issues->add( $issue );
} elseif ( $presence === 'ok' ) {
[ $msgcode, $actualKeywords ] =
$this->pluralFormCheck( $translation, $expectedKeywords );
if ( $msgcode === 'invalid' ) {
$expectedExample = UnicodePlural::flattenList(
array_map( [ $this, 'createFormExample' ], $expectedKeywords )
);
$actualExample = UnicodePlural::flattenList(
array_map( [ $this, 'createFormExample' ], $actualKeywords )
);
$issue = new ValidationIssue(
'plural',
'forms',
'translate-checks-unicode-plural-invalid',
[
[ 'PLAIN', $expectedExample ],
[ 'PLAIN', $actualExample ],
]
);
$issues->add( $issue );
}
} // else: not-applicable
return $issues;
}
private function createFormExample( string $keyword ): array {
return [ $keyword, '…' ];
}
private function pluralPresenceCheck(
bool $definitionHasPlural,
bool $translationHasPlural
): string {
if ( !$definitionHasPlural && $translationHasPlural ) {
return 'unsupported';
} elseif ( $definitionHasPlural && !$translationHasPlural ) {
return 'missing';
} elseif ( !$definitionHasPlural && !$translationHasPlural ) {
return 'not-applicable';
}
// Both have plural
return 'ok';
}
private function pluralFormCheck( string $text, array $expectedKeywords ): array {
[ , $instanceMap ] = UnicodePlural::parsePluralForms( $text );
foreach ( $instanceMap as $forms ) {
$actualKeywords = [];
foreach ( $forms as [ $keyword, ] ) {
$actualKeywords[] = $keyword;
}
if ( $actualKeywords !== $expectedKeywords ) {
return [ 'invalid', $actualKeywords ];
}
}
return [ 'ok', [] ];
}
}
class_alias( UnicodePluralValidator::class, '\MediaWiki\Extensions\Translate\UnicodePluralValidator' );
|