Language.php 4.3 KB

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