Translate extension for MediaWiki
Loading...
Searching...
No Matches
MediaWikiPluralValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use MediaWiki\Extension\Translate\MessageLoading\Message;
7use MediaWiki\Extension\Translate\Validation\MessageValidator;
8use MediaWiki\Extension\Translate\Validation\ValidationIssue;
9use MediaWiki\Extension\Translate\Validation\ValidationIssues;
10use MediaWiki\Language\LanguageFactory;
11use MediaWiki\Parser\Parser;
12use MediaWiki\Parser\ParserFactory;
13use MediaWiki\Parser\ParserOptions;
14use MediaWiki\Parser\PPFrame;
15use MediaWiki\User\UserFactory;
16
24
25 public function __construct(
26 private readonly LanguageFactory $languageFactory,
27 private readonly ParserFactory $parserFactory,
28 private readonly UserFactory $userFactory,
29 ) {
30 }
31
32 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
33 $issues = new ValidationIssues();
34 $this->pluralCheck( $message, $issues );
35 $this->pluralFormsCheck( $message, $targetLanguage, $issues );
36
37 return $issues;
38 }
39
40 private function pluralCheck( Message $message, ValidationIssues $issues ): void {
41 $definition = $message->definition();
42 $translation = $message->translation();
43
44 if (
45 stripos( $definition, '{{plural:' ) !== false &&
46 stripos( $translation, '{{plural:' ) === false
47 ) {
48 $issue = new ValidationIssue( 'plural', 'missing', 'translate-checks-plural' );
49 $issues->add( $issue );
50 }
51 }
52
53 protected function pluralFormsCheck(
54 Message $message, string $code, ValidationIssues $issues
55 ): void {
56 $translation = $message->translation();
57 // Are there any plural forms for this language in this message?
58 if ( stripos( $translation, '{{plural:' ) === false ) {
59 return;
60 }
61
62 $plurals = $this->getPluralForms( $translation );
63 $allowed = $this->getPluralFormCount( $code );
64
65 foreach ( $plurals as $forms ) {
66 $forms = self::removeExplicitPluralForms( $forms );
67 $provided = count( $forms );
68
69 if ( $provided > $allowed ) {
70 $issue = new ValidationIssue(
71 'plural',
72 'forms',
73 'translate-checks-plural-forms',
74 [
75 [ 'COUNT', $provided ],
76 [ 'COUNT', $allowed ],
77 ]
78 );
79
80 $issues->add( $issue );
81 }
82
83 // Are the last two forms identical?
84 if ( $provided > 1 && $forms[$provided - 1] === $forms[$provided - 2] ) {
85 $issue = new ValidationIssue( 'plural', 'dupe', 'translate-checks-plural-dupe' );
86 $issues->add( $issue );
87 }
88 }
89 }
90
92 public function getPluralFormCount( string $code ): int {
93 $forms = $this->languageFactory->getLanguage( $code )->getPluralRules();
94
95 // +1 for the 'other' form
96 return count( $forms ) + 1;
97 }
98
105 public function getPluralForms( string $translation ): array {
106 // Stores the forms from plural invocations
107 $plurals = [];
108
109 $cb = static function ( $parser, $frame, $args ) use ( &$plurals ) {
110 $forms = [];
111
112 foreach ( $args as $index => $form ) {
113 // The first arg is the number, we skip it
114 if ( $index !== 0 ) {
115 // Collect the raw text
116 $forms[] = $frame->expand( $form, PPFrame::RECOVER_ORIG );
117 // Expand the text to process embedded plurals
118 $frame->expand( $form );
119 }
120 }
121 $plurals[] = $forms;
122
123 return '';
124 };
125
126 // Setup parser
127 $parser = $this->parserFactory->create();
128 $parser->setFunctionHook( 'plural', $cb, Parser::SFH_NO_HASH | Parser::SFH_OBJECT_ARGS );
129
130 // Setup things needed for preprocess
131 $title = null;
132 $options = ParserOptions::newFromUserAndLang(
133 $this->userFactory->newAnonymous(),
134 $this->languageFactory->getLanguage( 'en' )
135 );
136
137 $parser->preprocess( $translation, $title, $options );
138
139 return $plurals;
140 }
141
143 public static function removeExplicitPluralForms( array $forms ): array {
144 // Handle explicit 0= and 1= forms
145 foreach ( $forms as $index => $form ) {
146 if ( preg_match( '/^[0-9]+=/', $form ) ) {
147 unset( $forms[$index] );
148 }
149 }
150
151 return array_values( $forms );
152 }
153}
Interface for message objects used by MessageCollection.
Definition Message.php:13
add(ValidationIssue $issue)
Add a new validation issue to the collection.
Handles plural validation for MediaWiki inline plural syntax.
getPluralFormCount(string $code)
Returns the number of plural forms MediaWiki supports for a language.
getPluralForms(string $translation)
Ugly home made probably awfully slow looping parser that parses {{PLURAL}} instances from a message a...
static removeExplicitPluralForms(array $forms)
Remove forms that start with an explicit number.