Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SmartFormatPluralValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
13use TMessage;
14
20 public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
21 $issues = new ValidationIssues();
22
23 $expectedKeywords = UnicodePlural::getPluralKeywords( $targetLanguage );
24 // Skip validation for languages for which we do not know the plural rule
25 if ( $expectedKeywords === null ) {
26 return $issues;
27 }
28
29 $definition = $message->definition();
30 $translation = $message->translation();
31 $expectedPluralCount = count( $expectedKeywords );
32 $definitionPlurals = SmartFormatPlural::getPluralInstances( $definition );
33 $translationPlurals = SmartFormatPlural::getPluralInstances( $translation );
34
35 $unsupportedVariables = array_diff(
36 array_keys( $translationPlurals ), array_keys( $definitionPlurals )
37 );
38
39 foreach ( $unsupportedVariables as $unsupportedVariable ) {
40 $issue = new ValidationIssue(
41 'plural',
42 'unsupported',
43 'translate-checks-smartformat-plural-unsupported',
44 [
45 [ 'PLAIN', '{' . $unsupportedVariable . '}' ],
46 ]
47 );
48
49 $issues->add( $issue );
50 }
51
52 if ( $expectedPluralCount > 1 ) {
53 $missingVariables = array_diff(
54 array_keys( $definitionPlurals ), array_keys( $translationPlurals )
55 );
56
57 foreach ( $missingVariables as $missingVariable ) {
58 $issue = new ValidationIssue(
59 'plural',
60 'missing',
61 'translate-checks-smartformat-plural-missing',
62 [
63 [ 'PLAIN', '{' . $missingVariable . '}' ],
64 ]
65 );
66
67 $issues->add( $issue );
68 }
69 }
70
71 // This returns only translation plurals for variables that exists in source
72 $commonVariables = array_intersect_key( $translationPlurals, $definitionPlurals );
73 foreach ( $commonVariables as $pluralInstances ) {
74 foreach ( $pluralInstances as $pluralInstance ) {
75 $actualPluralCount = count( $pluralInstance[ 'forms' ] );
76 if ( $actualPluralCount !== $expectedPluralCount ) {
77 $issue = new ValidationIssue(
78 'plural',
79 'forms',
80 'translate-checks-smartformat-plural-count',
81 [
82 [ 'COUNT', $expectedPluralCount ],
83 [ 'COUNT', $actualPluralCount ],
84 [ 'PLAIN', $pluralInstance[ 'original' ] ],
85 ]
86 );
87
88 $issues->add( $issue );
89 }
90 }
91 }
92
93 return $issues;
94 }
95
96 public function getInsertables( string $text ): array {
97 $definitionPlurals = SmartFormatPlural::getPluralInstances( $text );
98 $insertables = [];
99
100 // This could be more language specific if we were given more information, but
101 // we only have text.
102 foreach ( array_keys( $definitionPlurals ) as $variable ) {
103 $pre = '{' . "$variable:";
104 $post = '|}';
105 $insertables[] = new Insertable( "$pre$post", $pre, $post );
106 }
107
108 return $insertables;
109 }
110}
Insertable is a string that usually does not need translation and is difficult to type manually.
Implements partial support for SmartFormat plural syntax parsing.
static getPluralInstances(string $text)
Example input: {0} {0:message|messages} older than {1} {1:week|weeks} {0:has|have} been deleted.
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.