Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
GettextPlural.php
Go to the documentation of this file.
1<?php
8
9use InvalidArgumentException;
11
14 private const PRE = '{{PLURAL:GETTEXT|';
15 private const POST = '}}';
16
23 public static function getPluralRule( $code ) {
24 global $wgTranslateDocumentationLanguageCode;
25
26 if ( $code === $wgTranslateDocumentationLanguageCode ) {
27 return 'nplurals=1; plural=0;';
28 }
29
30 $rulefile = __DIR__ . '/../../data/plural-gettext.txt';
31 $rules = file_get_contents( $rulefile );
32 foreach ( explode( "\n", $rules ) as $line ) {
33 if ( trim( $line ) === '' ) {
34 continue;
35 }
36 [ $rulecode, $rule ] = explode( "\t", $line );
37 if ( $rulecode === $code ) {
38 return $rule;
39 }
40 }
41
42 return '';
43 }
44
52 public static function getPluralCount( $rule ) {
53 $m = [];
54 $ok = preg_match( '/nplurals=([0-9]+).*;/', $rule, $m );
55 if ( !$ok ) {
56 throw new InvalidArgumentException( "Rule $rule is malformed" );
57 }
58 return (int)$m[ 1 ];
59 }
60
67 public static function hasPlural( $text ) {
68 return str_contains( $text, self::PRE );
69 }
70
77 public static function flatten( array $forms ) {
78 return self::PRE . implode( '|', $forms ) . self::POST;
79 }
80
92 public static function unflatten( $text, $expectedPluralCount ) {
93 [ $template, $instanceMap ] = self::parsePluralForms( $text );
94 return self::expandTemplate( $template, $instanceMap, $expectedPluralCount );
95 }
96
103 private static function armour( $text ) {
104 // |/| is commonly used in KDE to support inflections. It needs to be escaped
105 // to avoid it messing up the plural markup.
106 $replacements = [
107 '|/|' => Utilities::getPlaceholder(),
108 ];
109 // {0} is a common variable format
110 preg_match_all( '/\{\d+\}/', $text, $matches );
111 foreach ( $matches[0] as $m ) {
112 $replacements[$m] = Utilities::getPlaceholder();
113 }
114
115 $text = strtr( $text, $replacements );
116 $map = array_flip( $replacements );
117
118 return [ $text, $map ];
119 }
120
128 private static function unarmour( $text, array $map ) {
129 return strtr( $text, $map );
130 }
131
138 public static function parsePluralForms( $text ) {
139 $m = [];
140 $pre = preg_quote( self::PRE, '/' );
141 $post = preg_quote( self::POST, '/' );
142
143 [ $armouredText, $armourMap ] = self::armour( $text );
144
145 $ok = preg_match_all( "/$pre(.*)$post/Us", $armouredText, $m );
146 if ( $ok === false ) {
147 throw new GettextPluralException( "Plural regular expression failed for text: $text" );
148 }
149
150 $template = $armouredText;
151 $instanceMap = [];
152
153 foreach ( $m[0] as $instanceIndex => $instanceText ) {
155
156 // Using preg_replace instead of str_replace because of the limit parameter
157 $pattern = '/' . preg_quote( $instanceText, '/' ) . '/';
158 $template = preg_replace( $pattern, $ph, $template, 1 );
159
160 $instanceForms = explode( '|', $m[ 1 ][ $instanceIndex ] );
161 foreach ( $instanceForms as $i => $v ) {
162 $instanceForms[ $i ] = self::unarmour( $v, $armourMap );
163 }
164
165 $instanceMap[$ph] = $instanceForms;
166 }
167
168 $template = self::unarmour( $template, $armourMap );
169 return [ $template, $instanceMap ];
170 }
171
180 public static function expandTemplate( $template, array $instanceMap, $expectedPluralCount ) {
181 $formArray = [];
182 for ( $formIndex = 0; $formIndex < $expectedPluralCount; $formIndex++ ) {
183 // Start with the whole string
184 $form = $template;
185
186 // Loop over each plural markup instance and replace it with the plural form belonging
187 // to the current index
188 foreach ( $instanceMap as $ph => $instanceForms ) {
189 // For missing forms, fall back to empty text.
190 // Extra forms are excluded because $formIndex < $expectedPluralCount
191 $replacement = $instanceForms[ $formIndex ] ?? '';
192 $form = str_replace( $ph, $replacement, $form );
193 }
194
195 $formArray[ $formIndex ] = $form;
196 }
197
198 return $formArray;
199 }
200}
static parsePluralForms( $text)
Parses plural markup into a structure form.
static unflatten( $text, $expectedPluralCount)
Format translation with plural forms as array of forms.
static expandTemplate( $template, array $instanceMap, $expectedPluralCount)
Gives fully expanded forms given a template and parsed plural markup instances.
static getPluralRule( $code)
Returns Gettext plural rule for given language.
static flatten(array $forms)
Format plural forms as single string suitable for translation.
static getPluralCount( $rule)
Returns how many plural forms are expected by a given plural rule.
static hasPlural( $text)
Quick way to check if the text contains plural syntax.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31
static getPlaceholder()
Returns a random string that can be used as placeholder in strings.