MediaWiki  REL1_21
LanguageRu.php
Go to the documentation of this file.
00001 <?php
00031 class LanguageRu extends Language {
00032 
00041         function convertGrammar( $word, $case ) {
00042                 global $wgGrammarForms;
00043                 if ( isset( $wgGrammarForms['ru'][$case][$word] ) ) {
00044                         return $wgGrammarForms['ru'][$case][$word];
00045                 }
00046 
00047                 # These rules are not perfect, but they are currently only used for Wikimedia site names so it doesn't
00048                 # matter if they are wrong sometimes. Just add a special case for your site name if necessary.
00049 
00050                 # substr doesn't support Unicode and mb_substr has issues,
00051                 # so break it to characters using preg_match_all and then use array_slice and join
00052                 $chars = array();
00053                 preg_match_all( '/./us', $word, $chars );
00054                 if ( !preg_match( "/[a-zA-Z_]/us", $word ) ) {
00055                         switch ( $case ) {
00056                                 case 'genitive': # родительный падеж
00057                                         if ( join( '', array_slice( $chars[0], -1 ) ) === 'ь' ) {
00058                                                 $word = join( '', array_slice( $chars[0], 0, -1 ) ) . 'я';
00059                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ия' ) {
00060                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ии';
00061                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ка' ) {
00062                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ки';
00063                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ти' ) {
00064                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'тей';
00065                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ды' ) {
00066                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'дов';
00067                                         } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ник' ) {
00068                                                 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ника';
00069                                         } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ные' ) {
00070                                                 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ных';
00071                                         }
00072                                         break;
00073                                 case 'dative': # дательный падеж
00074                                         # stub
00075                                         break;
00076                                 case 'accusative': # винительный падеж
00077                                         # stub
00078                                         break;
00079                                 case 'instrumental': # творительный падеж
00080                                         # stub
00081                                         break;
00082                                 case 'prepositional': # предложный падеж
00083                                         if ( join( '', array_slice( $chars[0], -1 ) ) === 'ь' ) {
00084                                                 $word = join( '', array_slice( $chars[0], 0, -1 ) ) . 'е';
00085                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ия' ) {
00086                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ии';
00087                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ка' ) {
00088                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ке';
00089                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ти' ) {
00090                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'тях';
00091                                         } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ды' ) {
00092                                                 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'дах';
00093                                         } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ник' ) {
00094                                                 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'нике';
00095                                         } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ные' ) {
00096                                                 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ных';
00097                                         }
00098                                         break;
00099                         }
00100                 }
00101 
00102                 return $word;
00103         }
00104 
00124         function convertPlural( $count, $forms ) {
00125                 if ( !count( $forms ) ) {
00126                         return '';
00127                 }
00128 
00129                 // If the actual number is not mentioned in the expression, then just two forms are enough:
00130                 // singular for $count === 1
00131                 // plural   for $count !== 1
00132                 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
00133                 if ( count( $forms ) === 2 ) {
00134                         return $count === 1 ? $forms[0] : $forms[1];
00135                 }
00136 
00137                 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
00138                 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
00139                 $forms = $this->preConvertPlural( $forms, 3 );
00140 
00141                 if ( $count > 10 && (int)floor( ( $count % 100 ) / 10 ) === 1 ) {
00142                         return $forms[2];
00143                 }
00144 
00145                 switch ( $count % 10 ) {
00146                         case 1:
00147                                 return $forms[0];
00148                         case 2:
00149                         case 3:
00150                         case 4:
00151                                 return $forms[1];
00152                         default:
00153                                 return $forms[2];
00154                 }
00155         }
00156 
00166         function commafy( $_ ) {
00167                 if ( preg_match( '/^-?\d{1,4}(\.\d*)?$/', $_ ) ) {
00168                         return $_;
00169                 } else {
00170                         return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
00171                 }
00172         }
00173 }