Added result properties to action=paraminfo
[lhc/web/wiklou.git] / languages / classes / LanguageRu.php
1 <?php
2
3 /** Russian (русский язык)
4 *
5 * You can contact Alexander Sigachov (alexander.sigachov at Googgle Mail)
6 *
7 * @ingroup Language
8 */
9 class LanguageRu extends Language {
10
11 /**
12 * Convert from the nominative form of a noun to some other case
13 * Invoked with {{grammar:case|word}}
14 *
15 * @param $word string
16 * @param $case string
17 * @return string
18 */
19 function convertGrammar( $word, $case ) {
20 global $wgGrammarForms;
21 if ( isset( $wgGrammarForms['ru'][$case][$word] ) ) {
22 return $wgGrammarForms['ru'][$case][$word];
23 }
24
25 # These rules are not perfect, but they are currently only used for site names so it doesn't
26 # matter if they are wrong sometimes. Just add a special case for your site name if necessary.
27
28 # join and array_slice instead mb_substr
29 $ar = array();
30 preg_match_all( '/./us', $word, $ar );
31 if ( !preg_match( "/[a-zA-Z_]/us", $word ) )
32 switch ( $case ) {
33 case 'genitive': # родительный падеж
34 if ( ( join( '', array_slice( $ar[0], -4 ) ) == 'вики' ) || ( join( '', array_slice( $ar[0], -4 ) ) == 'Вики' ) )
35 { }
36 elseif ( join( '', array_slice( $ar[0], -1 ) ) == 'ь' )
37 $word = join( '', array_slice( $ar[0], 0, -1 ) ) . 'я';
38 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ия' )
39 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'ии';
40 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ка' )
41 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'ки';
42 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ти' )
43 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'тей';
44 elseif ( join( '', array_slice( $ar[0], -2 ) ) == 'ды' )
45 $word = join( '', array_slice( $ar[0], 0, -2 ) ) . 'дов';
46 elseif ( join( '', array_slice( $ar[0], -3 ) ) == 'ник' )
47 $word = join( '', array_slice( $ar[0], 0, -3 ) ) . 'ника';
48 break;
49 case 'dative': # дательный падеж
50 # stub
51 break;
52 case 'accusative': # винительный падеж
53 # stub
54 break;
55 case 'instrumental': # творительный падеж
56 # stub
57 break;
58 case 'prepositional': # предложный падеж
59 # stub
60 break;
61 }
62 return $word;
63 }
64
65 /**
66 * Plural form transformations
67 *
68 * $forms[0] - singular form (for 1, 21, 31, 41...)
69 * $forms[1] - paucal form (for 2, 3, 4, 22, 23, 24, 32, 33, 34...)
70 * $forms[2] - plural form (for 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26...)
71 *
72 * Examples:
73 * message with number
74 * "Сделано $1 {{PLURAL:$1|изменение|изменения|изменений}}"
75 * ("$1 change[s] were made)
76 * message without number
77 * "Действие не может быть выполнено по {{PLURAL:$1|следующей причине|следующим причинам}}:"
78 * ("The action cannot be performed for the following reason[s]")
79 * @param $count int
80 * @param $forms array
81 *
82 * @return string
83 */
84 function convertPlural( $count, $forms ) {
85 if ( !count( $forms ) ) { return ''; }
86
87 // If the actual number is not mentioned in the expression, then just two forms are enough:
88 // singular for $count == 1
89 // plural for $count != 1
90 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
91 if ( count( $forms ) === 2 ) return $count == 1 ? $forms[0] : $forms[1];
92
93 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
94 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
95 $forms = $this->preConvertPlural( $forms, 3 );
96
97 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
98 return $forms[2];
99 } else {
100 switch ( $count % 10 ) {
101 case 1: return $forms[0];
102 case 2:
103 case 3:
104 case 4: return $forms[1];
105 default: return $forms[2];
106 }
107 }
108 }
109
110 /**
111 * Four-digit number should be without group commas (spaces)
112 * See manual of style at http://ru.wikipedia.org/wiki/Википедия:Оформление_статей
113 * So "1 234 567", "12 345" but "1234"
114 *
115 * @param $_ string
116 *
117 * @return string
118 */
119 function commafy( $_ ) {
120 if ( preg_match( '/^-?\d{1,4}(\.\d*)?$/', $_ ) ) {
121 return $_;
122 } else {
123 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
124 }
125 }
126 }