Language.php 4.3 KB

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