LanguageSr.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Serbian (Српски / Srpski) 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. * There are two levels of conversion for Serbian: the script level
  25. * (Cyrillics <-> Latin), and the variant level (ekavian
  26. * <->iyekavian). The two are orthogonal. So we really only need two
  27. * dictionaries: one for Cyrillics and Latin, and one for ekavian and
  28. * iyekavian.
  29. *
  30. * @ingroup Language
  31. */
  32. class SrConverter extends LanguageConverter {
  33. public $mToLatin = [
  34. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
  35. 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
  36. 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
  37. 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
  38. 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
  39. 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
  40. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
  41. 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
  42. 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
  43. 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
  44. 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
  45. 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
  46. ];
  47. public $mToCyrillics = [
  48. 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
  49. 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
  50. 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
  51. 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
  52. 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
  53. 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
  54. 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
  55. 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
  56. 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
  57. 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
  58. 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
  59. 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
  60. 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
  61. 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
  62. 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
  63. ];
  64. function loadDefaultTables() {
  65. $this->mTables = [
  66. 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
  67. 'sr-el' => new ReplacementArray( $this->mToLatin ),
  68. 'sr' => new ReplacementArray()
  69. ];
  70. }
  71. /**
  72. * A function wrapper:
  73. * - if there is no selected variant, leave the link
  74. * names as they were
  75. * - do not try to find variants for usernames
  76. *
  77. * @param string &$link
  78. * @param Title &$nt
  79. * @param bool $ignoreOtherCond
  80. */
  81. function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
  82. // check for user namespace
  83. if ( is_object( $nt ) ) {
  84. $ns = $nt->getNamespace();
  85. if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
  86. return;
  87. }
  88. }
  89. $oldlink = $link;
  90. parent::findVariantLink( $link, $nt, $ignoreOtherCond );
  91. if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
  92. $link = $oldlink;
  93. }
  94. }
  95. /**
  96. * It translates text into variant, specials:
  97. * - ommiting roman numbers
  98. *
  99. * @param string $text
  100. * @param string $toVariant
  101. *
  102. * @throws MWException
  103. * @return string
  104. */
  105. function translate( $text, $toVariant ) {
  106. $breaks = '[^\w\x80-\xff]';
  107. // regexp for roman numbers
  108. // Lookahead assertion ensures $roman doesn't match the empty string
  109. $roman = '(?=[MDCLXVI])M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
  110. $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
  111. . $roman . '$|' . $breaks . $roman . $breaks . '/';
  112. $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
  113. $m = array_shift( $matches );
  114. $this->loadTables();
  115. if ( !isset( $this->mTables[$toVariant] ) ) {
  116. throw new MWException( "Broken variant table: "
  117. . implode( ',', array_keys( $this->mTables ) ) );
  118. }
  119. $ret = $this->mTables[$toVariant]->replace( $m[0] );
  120. $mstart = $m[1] + strlen( $m[0] );
  121. foreach ( $matches as $m ) {
  122. $ret .= substr( $text, $mstart, $m[1] - $mstart );
  123. $ret .= parent::translate( $m[0], $toVariant );
  124. $mstart = $m[1] + strlen( $m[0] );
  125. }
  126. return $ret;
  127. }
  128. /**
  129. * Guess if a text is written in Cyrillic or Latin.
  130. * Overrides LanguageConverter::guessVariant()
  131. *
  132. * @param string $text The text to be checked
  133. * @param string $variant Language code of the variant to be checked for
  134. * @return bool True if $text appears to be written in $variant
  135. *
  136. * @author Nikola Smolenski <smolensk@eunet.rs>
  137. * @since 1.19
  138. */
  139. public function guessVariant( $text, $variant ) {
  140. $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
  141. $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
  142. if ( $variant == 'sr-ec' ) {
  143. return $numCyrillic > $numLatin;
  144. } elseif ( $variant == 'sr-el' ) {
  145. return $numLatin > $numCyrillic;
  146. } else {
  147. return false;
  148. }
  149. }
  150. }
  151. /**
  152. * Serbian (Српски / Srpski)
  153. *
  154. * @ingroup Language
  155. */
  156. class LanguageSr extends Language {
  157. function __construct() {
  158. parent::__construct();
  159. $variants = [ 'sr', 'sr-ec', 'sr-el' ];
  160. $variantfallbacks = [
  161. 'sr' => 'sr-ec',
  162. 'sr-ec' => 'sr',
  163. 'sr-el' => 'sr',
  164. ];
  165. $flags = [
  166. 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
  167. 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
  168. ];
  169. $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
  170. }
  171. }