Language.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use function _bindtextdomain;
  5. use function _setlocale;
  6. use function _textdomain;
  7. use function addcslashes;
  8. use function function_exists;
  9. use function in_array;
  10. use function preg_match;
  11. use function setlocale;
  12. use function str_contains;
  13. use function str_replace;
  14. use function strcmp;
  15. /**
  16. * Language object
  17. */
  18. class Language
  19. {
  20. /** @var string */
  21. protected $code;
  22. /** @var string */
  23. protected $name;
  24. /** @var string */
  25. protected $native;
  26. /** @var string */
  27. protected $regex;
  28. /** @var string */
  29. protected $mysql;
  30. /**
  31. * Constructs the Language object
  32. *
  33. * @param string $code Language code
  34. * @param string $name English name
  35. * @param string $native Native name
  36. * @param string $regex Match regular expression
  37. * @param string $mysql MySQL locale code
  38. */
  39. public function __construct($code, $name, $native, $regex, $mysql)
  40. {
  41. $this->code = $code;
  42. $this->name = $name;
  43. $this->native = $native;
  44. if (! str_contains($regex, '[-_]')) {
  45. $regex = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $regex);
  46. }
  47. $this->regex = $regex;
  48. $this->mysql = $mysql;
  49. }
  50. /**
  51. * Returns native name for language
  52. *
  53. * @return string
  54. */
  55. public function getNativeName()
  56. {
  57. return $this->native;
  58. }
  59. /**
  60. * Returns English name for language
  61. *
  62. * @return string
  63. */
  64. public function getEnglishName()
  65. {
  66. return $this->name;
  67. }
  68. /**
  69. * Returns verbose name for language
  70. *
  71. * @return string
  72. */
  73. public function getName()
  74. {
  75. if (! empty($this->native)) {
  76. return $this->native . ' - ' . $this->name;
  77. }
  78. return $this->name;
  79. }
  80. /**
  81. * Returns language code
  82. *
  83. * @return string
  84. */
  85. public function getCode()
  86. {
  87. return $this->code;
  88. }
  89. /**
  90. * Returns MySQL locale code, can be empty
  91. *
  92. * @return string
  93. */
  94. public function getMySQLLocale()
  95. {
  96. return $this->mysql;
  97. }
  98. /**
  99. * Compare function used for sorting
  100. *
  101. * @param Language $other Other object to compare
  102. *
  103. * @return int same as strcmp
  104. */
  105. public function cmp(Language $other): int
  106. {
  107. return strcmp($this->name, $other->name);
  108. }
  109. /**
  110. * Checks whether language is currently active.
  111. */
  112. public function isActive(): bool
  113. {
  114. return $GLOBALS['lang'] == $this->code;
  115. }
  116. /**
  117. * Checks whether language matches HTTP header Accept-Language.
  118. *
  119. * @param string $header Header content
  120. */
  121. public function matchesAcceptLanguage($header): bool
  122. {
  123. $pattern = '/^('
  124. . addcslashes($this->regex, '/')
  125. . ')(;q=[0-9]\\.[0-9])?$/i';
  126. return (bool) preg_match($pattern, $header);
  127. }
  128. /**
  129. * Checks whether language matches HTTP header User-Agent
  130. *
  131. * @param string $header Header content
  132. */
  133. public function matchesUserAgent($header): bool
  134. {
  135. $pattern = '/(\(|\[|;[[:space:]])('
  136. . addcslashes($this->regex, '/')
  137. . ')(;|\]|\))/i';
  138. return (bool) preg_match($pattern, $header);
  139. }
  140. /**
  141. * Checks whether language is RTL
  142. */
  143. public function isRTL(): bool
  144. {
  145. return in_array($this->code, ['ar', 'fa', 'he', 'ur']);
  146. }
  147. /**
  148. * Activates given translation
  149. */
  150. public function activate(): void
  151. {
  152. $GLOBALS['lang'] = $this->code;
  153. // Set locale
  154. _setlocale(0, $this->code);
  155. _bindtextdomain('phpmyadmin', LOCALE_PATH);
  156. _textdomain('phpmyadmin');
  157. // Set PHP locale as well
  158. if (function_exists('setlocale')) {
  159. setlocale(0, $this->code);
  160. }
  161. /* Text direction for language */
  162. if ($this->isRTL()) {
  163. $GLOBALS['text_dir'] = 'rtl';
  164. } else {
  165. $GLOBALS['text_dir'] = 'ltr';
  166. }
  167. /* Show possible warnings from langauge selection */
  168. LanguageManager::getInstance()->showWarnings();
  169. }
  170. }