Merge "mediawiki.special.preferences: Support Back/Forward navigation"
[lhc/web/wiklou.git] / languages / classes / LanguageRu.php
1 <?php
2 /**
3 * Russian (русский язык) specific code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Language
22 */
23
24 /**
25 * Russian (русский язык)
26 *
27 * You can contact Alexander Sigachov (alexander.sigachov at Googgle Mail)
28 *
29 * @ingroup Language
30 */
31 class LanguageRu extends Language {
32
33 /**
34 * Convert from the nominative form of a noun to some other case
35 * Invoked with {{grammar:case|word}}
36 *
37 * @param $word string
38 * @param $case string
39 * @return string
40 */
41 function convertGrammar( $word, $case ) {
42 global $wgGrammarForms;
43 if ( isset( $wgGrammarForms['ru'][$case][$word] ) ) {
44 return $wgGrammarForms['ru'][$case][$word];
45 }
46
47 # These rules are not perfect, but they are currently only used for site names so it doesn't
48 # matter if they are wrong sometimes. Just add a special case for your site name if necessary.
49
50 # join and array_slice instead mb_substr
51 $ar = array();
52 preg_match_all( '/./us', $word, $ar );
53 if ( !preg_match( "/[a-zA-Z_]/us", $word ) )
54 switch ( $case ) {
55 case 'genitive': # родительный падеж
56 if ( ( join( '', array_slice( $ar[0], -4 ) ) == 'вики' ) || ( join( '', array_slice( $ar[0], -4 ) ) == 'Вики' ) )
57 { }
58 elseif ( join( '', array_slice( $ar[0], -1 ) ) == 'ь' )
59 $word = join( '', array_slice( $ar[0], 0, -1 ) ) . 'я';
60 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ия' )
61 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'ии';
62 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ка' )
63 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'ки';
64 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ти' )
65 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'тей';
66 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ды' )
67 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'дов';
68 elseif ( join( '', array_slice( $ar[0], -3 ) ) == 'ник' )
69 $word = join( '', array_slice( $ar[0], 0, -3 ) ) . 'ника';
70 break;
71 case 'dative': # дательный падеж
72 # stub
73 break;
74 case 'accusative': # винительный падеж
75 # stub
76 break;
77 case 'instrumental': # творительный падеж
78 # stub
79 break;
80 case 'prepositional': # предложный падеж
81 # stub
82 break;
83 }
84 return $word;
85 }
86
87 /**
88 * Plural form transformations
89 *
90 * $forms[0] - singular form (for 1, 21, 31, 41...)
91 * $forms[1] - paucal form (for 2, 3, 4, 22, 23, 24, 32, 33, 34...)
92 * $forms[2] - plural form (for 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26...)
93 *
94 * Examples:
95 * message with number
96 * "Сделано $1 {{PLURAL:$1|изменение|изменения|изменений}}"
97 * ("$1 change[s] were made)
98 * message without number
99 * "Действие не может быть выполнено по {{PLURAL:$1|следующей причине|следующим причинам}}:"
100 * ("The action cannot be performed for the following reason[s]")
101 * @param $count int
102 * @param $forms array
103 *
104 * @return string
105 */
106 function convertPlural( $count, $forms ) {
107 if ( !count( $forms ) ) { return ''; }
108
109 // If the actual number is not mentioned in the expression, then just two forms are enough:
110 // singular for $count == 1
111 // plural for $count != 1
112 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
113 if ( count( $forms ) === 2 ) return $count == 1 ? $forms[0] : $forms[1];
114
115 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
116 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
117 $forms = $this->preConvertPlural( $forms, 3 );
118
119 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
120 return $forms[2];
121 } else {
122 switch ( $count % 10 ) {
123 case 1: return $forms[0];
124 case 2:
125 case 3:
126 case 4: return $forms[1];
127 default: return $forms[2];
128 }
129 }
130 }
131
132 /**
133 * Four-digit number should be without group commas (spaces)
134 * See manual of style at http://ru.wikipedia.org/wiki/Википедия:Оформление_статей
135 * So "1 234 567", "12 345" but "1234"
136 *
137 * @param $_ string
138 *
139 * @return string
140 */
141 function commafy( $_ ) {
142 if ( preg_match( '/^-?\d{1,4}(\.\d*)?$/', $_ ) ) {
143 return $_;
144 } else {
145 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
146 }
147 }
148 }