Merge "Add CollationFa"
[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:
28 * Alexander Sigachov (alexander.sigachov at Googgle Mail)
29 * Amir E. Aharoni (amir.aharoni@mail.huji.ac.il)
30 *
31 * @ingroup Language
32 */
33 class LanguageRu extends Language {
34 /**
35 * Convert from the nominative form of a noun to some other case
36 * Invoked with {{grammar:case|word}}
37 *
38 * @param string $word
39 * @param string $case
40 * @return string
41 */
42 function convertGrammar( $word, $case ) {
43 global $wgGrammarForms;
44 if ( isset( $wgGrammarForms['ru'][$case][$word] ) ) {
45 return $wgGrammarForms['ru'][$case][$word];
46 }
47
48 $grammarTransformations = $this->getGrammarTransformations();
49
50 if ( isset( $grammarTransformations[$case] ) ) {
51 foreach ( array_values( $grammarTransformations[$case] ) as $rule ) {
52 $form = $rule[0];
53
54 if ( $form === '@metadata' ) {
55 continue;
56 }
57
58 $replacement = $rule[1];
59
60 $regex = "/$form/";
61
62 if ( preg_match( $regex, $word ) ) {
63 $word = preg_replace( $regex, $replacement, $word );
64
65 break;
66 }
67 }
68 }
69
70 return $word;
71 }
72
73 /**
74 * Four-digit number should be without group commas (spaces)
75 * See manual of style at https://ru.wikipedia.org/wiki/Википедия:Оформление_статей
76 * So "1 234 567", "12 345" but "1234"
77 *
78 * @param string $_
79 *
80 * @return string
81 */
82 function commafy( $_ ) {
83 if ( preg_match( '/^-?\d{1,4}(\.\d*)?$/', $_ ) ) {
84 return $_;
85 } else {
86 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
87 }
88 }
89 }