LanguageShi.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Shilha 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. * Conversion script between Latin and Tifinagh for Tachelhit.
  25. * - Tifinagh -> lowercase Latin
  26. * - lowercase/uppercase Latin -> Tifinagh
  27. *
  28. *
  29. * Based on:
  30. * - https://en.wikipedia.org/wiki/Shilha_language
  31. * - LanguageSr.php
  32. *
  33. * @ingroup Language
  34. */
  35. class ShiConverter extends LanguageConverter {
  36. protected $mDoContentConvert;
  37. public $mToLatin = [
  38. 'ⴰ' => 'a', 'ⴱ' => 'b', 'ⴳ' => 'g', 'ⴷ' => 'd', 'ⴹ' => 'ḍ', 'ⴻ' => 'e',
  39. 'ⴼ' => 'f', 'ⴽ' => 'k', 'ⵀ' => 'h', 'ⵃ' => 'ḥ', 'ⵄ' => 'ε', 'ⵅ' => 'x',
  40. 'ⵇ' => 'q', 'ⵉ' => 'i', 'ⵊ' => 'j', 'ⵍ' => 'l', 'ⵎ' => 'm', 'ⵏ' => 'n',
  41. 'ⵓ' => 'u', 'ⵔ' => 'r', 'ⵕ' => 'ṛ', 'ⵖ' => 'γ', 'ⵙ' => 's', 'ⵚ' => 'ṣ',
  42. 'ⵛ' => 'š', 'ⵜ' => 't', 'ⵟ' => 'ṭ', 'ⵡ' => 'w', 'ⵢ' => 'y', 'ⵣ' => 'z',
  43. 'ⵥ' => 'ẓ', 'ⵯ' => 'ʷ', 'ⵖ' => 'ɣ', 'ⵠ' => 'v', 'ⵒ' => 'p',
  44. ];
  45. public $mUpperToLowerCaseLatin = [
  46. 'A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e',
  47. 'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j',
  48. 'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o',
  49. 'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't',
  50. 'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y',
  51. 'Z' => 'z', 'Ɣ' => 'ɣ',
  52. ];
  53. public $mToTifinagh = [
  54. 'a' => 'ⴰ', 'b' => 'ⴱ', 'g' => 'ⴳ', 'd' => 'ⴷ', 'ḍ' => 'ⴹ', 'e' => 'ⴻ',
  55. 'f' => 'ⴼ', 'k' => 'ⴽ', 'h' => 'ⵀ', 'ḥ' => 'ⵃ', 'ε' => 'ⵄ', 'x' => 'ⵅ',
  56. 'q' => 'ⵇ', 'i' => 'ⵉ', 'j' => 'ⵊ', 'l' => 'ⵍ', 'm' => 'ⵎ', 'n' => 'ⵏ',
  57. 'u' => 'ⵓ', 'r' => 'ⵔ', 'ṛ' => 'ⵕ', 'γ' => 'ⵖ', 's' => 'ⵙ', 'ṣ' => 'ⵚ',
  58. 'š' => 'ⵛ', 't' => 'ⵜ', 'ṭ' => 'ⵟ', 'w' => 'ⵡ', 'y' => 'ⵢ', 'z' => 'ⵣ',
  59. 'ẓ' => 'ⵥ', 'ʷ' => 'ⵯ', 'ɣ' => 'ⵖ', 'v' => 'ⵠ', 'p' => 'ⵒ',
  60. ];
  61. function loadDefaultTables() {
  62. $this->mTables = [
  63. 'lowercase' => new ReplacementArray( $this->mUpperToLowerCaseLatin ),
  64. 'shi-tfng' => new ReplacementArray( $this->mToTifinagh ),
  65. 'shi-latn' => new ReplacementArray( $this->mToLatin ),
  66. 'shi' => new ReplacementArray()
  67. ];
  68. }
  69. /**
  70. * A function wrapper:
  71. * - if there is no selected variant, leave the link
  72. * names as they were
  73. * - do not try to find variants for usernames
  74. *
  75. * @param string &$link
  76. * @param Title &$nt
  77. * @param bool $ignoreOtherCond
  78. */
  79. function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
  80. // check for user namespace
  81. if ( is_object( $nt ) ) {
  82. $ns = $nt->getNamespace();
  83. if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
  84. return;
  85. }
  86. }
  87. $oldlink = $link;
  88. parent::findVariantLink( $link, $nt, $ignoreOtherCond );
  89. if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
  90. $link = $oldlink;
  91. }
  92. }
  93. /**
  94. * It translates text into variant
  95. *
  96. * @param string $text
  97. * @param string $toVariant
  98. *
  99. * @return string
  100. */
  101. function translate( $text, $toVariant ) {
  102. // If $text is empty or only includes spaces, do nothing
  103. // Otherwise translate it
  104. if ( trim( $text ) ) {
  105. $this->loadTables();
  106. // To Tifinagh, first translate uppercase to lowercase Latin
  107. if ( $toVariant == 'shi-tfng' ) {
  108. $text = $this->mTables['lowercase']->replace( $text );
  109. }
  110. $text = $this->mTables[$toVariant]->replace( $text );
  111. }
  112. return $text;
  113. }
  114. }
  115. /**
  116. * Tachelhit
  117. *
  118. * @ingroup Language
  119. */
  120. class LanguageShi extends Language {
  121. function __construct() {
  122. parent::__construct();
  123. $variants = [ 'shi', 'shi-tfng', 'shi-latn' ];
  124. $variantfallbacks = [
  125. 'shi' => 'shi-tfng',
  126. 'shi-tfng' => 'shi',
  127. 'shi-latn' => 'shi',
  128. ];
  129. $flags = [];
  130. $this->mConverter = new ShiConverter( $this, 'shi', $variants, $variantfallbacks, $flags );
  131. }
  132. }