Language.php 4.5 KB

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