11b42cf9a20f96d99bf9a3015a3b85acde524766
[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 Wikimedia 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 # substr doesn't support Unicode and mb_substr has issues,
51 # so break it to characters using preg_match_all and then use array_slice and join
52 $chars = array();
53 preg_match_all( '/./us', $word, $chars );
54 if ( !preg_match( "/[a-zA-Z_]/us", $word ) ) {
55 switch ( $case ) {
56 case 'genitive': # родительный падеж
57 if ( join( '', array_slice( $chars[0], -1 ) ) === 'ь' ) {
58 $word = join( '', array_slice( $chars[0], 0, -1 ) ) . 'я';
59 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ия' ) {
60 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ии';
61 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ка' ) {
62 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ки';
63 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ти' ) {
64 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'тей';
65 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ды' ) {
66 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'дов';
67 } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ник' ) {
68 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ника';
69 } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ные' ) {
70 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ных';
71 }
72 break;
73 case 'dative': # дательный падеж
74 # stub
75 break;
76 case 'accusative': # винительный падеж
77 # stub
78 break;
79 case 'instrumental': # творительный падеж
80 # stub
81 break;
82 case 'prepositional': # предложный падеж
83 if ( join( '', array_slice( $chars[0], -1 ) ) === 'ь' ) {
84 $word = join( '', array_slice( $chars[0], 0, -1 ) ) . 'е';
85 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ия' ) {
86 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ии';
87 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ка' ) {
88 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'ке';
89 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ти' ) {
90 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'тях';
91 } elseif ( join( '', array_slice( $chars[0], -2 ) ) === 'ды' ) {
92 $word = join( '', array_slice( $chars[0], 0, -2 ) ) . 'дах';
93 } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ник' ) {
94 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'нике';
95 } elseif ( join( '', array_slice( $chars[0], -3 ) ) === 'ные' ) {
96 $word = join( '', array_slice( $chars[0], 0, -3 ) ) . 'ных';
97 }
98 break;
99 }
100 }
101
102 return $word;
103 }
104
105 /**
106 * Plural form transformations
107 *
108 * $forms[0] - singular form (for 1, 21, 31, 41...)
109 * $forms[1] - paucal form (for 2, 3, 4, 22, 23, 24, 32, 33, 34...)
110 * $forms[2] - plural form (for 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26...)
111 *
112 * Examples:
113 * message with number
114 * "Сделано $1 {{PLURAL:$1|изменение|изменения|изменений}}"
115 * ("$1 change[s] were made)
116 * message without number
117 * "Действие не может быть выполнено по {{PLURAL:$1|следующей причине|следующим причинам}}:"
118 * ("The action cannot be performed for the following reason[s]")
119 * @param $count int
120 * @param $forms array
121 *
122 * @return string
123 */
124 function convertPlural( $count, $forms ) {
125 if ( !count( $forms ) ) {
126 return '';
127 }
128
129 // If the actual number is not mentioned in the expression, then just two forms are enough:
130 // singular for $count === 1
131 // plural for $count !== 1
132 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
133 if ( count( $forms ) === 2 ) {
134 return $count === 1 ? $forms[0] : $forms[1];
135 }
136
137 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
138 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
139 $forms = $this->preConvertPlural( $forms, 3 );
140
141 if ( $count > 10 && (int)floor( ( $count % 100 ) / 10 ) === 1 ) {
142 return $forms[2];
143 }
144
145 switch ( $count % 10 ) {
146 case 1:
147 return $forms[0];
148 case 2:
149 case 3:
150 case 4:
151 return $forms[1];
152 default:
153 return $forms[2];
154 }
155 }
156
157 /**
158 * Four-digit number should be without group commas (spaces)
159 * See manual of style at http://ru.wikipedia.org/wiki/Википедия:Оформление_статей
160 * So "1 234 567", "12 345" but "1234"
161 *
162 * @param $_ string
163 *
164 * @return string
165 */
166 function commafy( $_ ) {
167 if ( preg_match( '/^-?\d{1,4}(\.\d*)?$/', $_ ) ) {
168 return $_;
169 } else {
170 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
171 }
172 }
173 }