BxDolLanguages.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDol');
  9. bx_import('BxDolLanguagesQuery');
  10. define('BX_DOL_LANGUAGE_CATEGORY_SYSTEM', 1);
  11. define('BX_DOL_LANGUAGE_CATEGORY_CUSTOM', 2);
  12. class BxDolLanguages extends BxDol {
  13. protected $oDb;
  14. protected $sCurrentLanguage;
  15. function __construct() {
  16. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  17. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  18. parent::BxDol();
  19. $this->oDb = BxDolLanguagesQuery::getInstance();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone() {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. /**
  29. * Get singleton instance of the class
  30. */
  31. static function getInstance() {
  32. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLanguages();
  34. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  35. }
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. function getCurrentLangName($bSetCookie = true) {
  39. $sLang = '';
  40. if(!$sLang && !empty($_GET['lang']))
  41. $sLang = $this->tryToGetLang($_GET['lang'], $bSetCookie);
  42. if(!$sLang && !empty($_POST['lang']))
  43. $sLang = $this->tryToGetLang($_POST['lang'], $bSetCookie);
  44. if(!$sLang && !empty($_COOKIE['lang']))
  45. $sLang = $this->tryToGetLang($_COOKIE['lang']);
  46. if(!$sLang && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  47. $sLang = $this->tryToGetLang($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  48. if(!$sLang)
  49. $sLang = $this->tryToGetLang(getParam('lang_default'));
  50. if(!$sLang)
  51. $sLang = $this->tryToGetLang('en');
  52. setlocale(LC_TIME, $sLang . '_' . strtoupper($sLang) . '.utf-8', $sLang . '_' . strtoupper($sLang) . '.utf8', $sLang . '.utf-8', $sLang . '.utf8', $sLang);
  53. return $sLang;
  54. }
  55. function getCurrentLangId() {
  56. return $this->getLangId($this->getCurrentLanguage());
  57. }
  58. function getLangId($sLang) {
  59. return $this->oDb->getLanguageId($sLang);
  60. }
  61. function getDefaultLangName() {
  62. return getParam('lang_default');
  63. }
  64. function getLanguages($bIdAsKey = false, $bActiveOnly = false) {
  65. return $this->oDb->getLanguages($bIdAsKey, $bActiveOnly);
  66. }
  67. function getLanguageCategory($sName) {
  68. $iId = 0;
  69. $this->oDb->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iId);
  70. return (int)$iId;
  71. }
  72. /**
  73. * It tries to match current language with provided languages array, if nothing is found $sFallbackLanguage is returned.
  74. * @param $aLangs - array of languages, example: array('ru' => 1, 'kg' => 1);
  75. * @param $sFallbackLanguage - language to return of nothis is found
  76. * @return language code string
  77. */
  78. function detectLanguageFromArray($aLangs, $sFallbackLanguage = 'en') {
  79. return isset($aLangs[$GLOBALS['sCurrentLanguage']]) ? $GLOBALS['sCurrentLanguage'] : $sFallbackLanguage;
  80. }
  81. /**
  82. * Get current language.
  83. */
  84. function getCurrentLanguage() {
  85. return $GLOBALS['sCurrentLanguage'];
  86. }
  87. function _t() {
  88. global $LANG;
  89. $key = func_get_arg(0);
  90. if(isset($LANG[$key])) {
  91. $str = $LANG[$key];
  92. if(($iNumArgs = func_num_args()) > 1)
  93. for($i = 1; $i < $iNumArgs; $i++)
  94. $str = str_replace('{' . ($i - 1) . '}', func_get_arg($i), $str);
  95. return $str;
  96. }
  97. else
  98. return $key;
  99. }
  100. function _t_err() {
  101. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  102. }
  103. function _t_action() {
  104. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  105. }
  106. function _t_ext($key, $args) {
  107. global $LANG;
  108. if(isset($LANG[$key])) {
  109. $str = $LANG[$key];
  110. if(!is_array($args))
  111. return str_replace('{0}', $args, $str);
  112. foreach ($args as $key => $val)
  113. $str = str_replace('{'.$key.'}', $val, $str);
  114. return $str;
  115. }
  116. else
  117. return $key;
  118. }
  119. function _t_format_size ($iSize) {
  120. $a = array (
  121. 1024 => '_sys_format_size_b',
  122. 1024*1024 => '_sys_format_size_kb',
  123. 1024*1024*1024 => '_sys_format_size_mb',
  124. 1024*1024*1024*1024 => '_sys_format_size_gb',
  125. 1024*1024*1024*1024*1024 => '_sys_format_size_tb',
  126. );
  127. foreach($a as $i => $sKey)
  128. if($iSize < $i)
  129. return $this->_t($sKey, round($iSize / ($i / 1024), 1));
  130. return $this->_t('_sys_format_size_b', 0);
  131. }
  132. function _t_format_currency ($fPrice) {
  133. return $this->_t('_sys_currency', getParam('currency_sign'), sprintf("%.2f", (float)$fPrice));
  134. }
  135. function _t_format_extensions ($mixedExtensions) {
  136. if (!is_array($mixedExtensions))
  137. $a = explode(',', $mixedExtensions);
  138. else
  139. $a = $mixedExtensions;
  140. if (!$a)
  141. return '';
  142. return '.' . implode(', .', $a);
  143. }
  144. protected function init() {
  145. /**
  146. * Trying to initialize default language.
  147. */
  148. $GLOBALS['sCurrentLanguage'] = $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(false);
  149. if($GLOBALS['sCurrentLanguage'] != '') {
  150. $sPath = BX_DIRECTORY_PATH_CACHE . 'lang-' . $GLOBALS['sCurrentLanguage'] . '.php';
  151. if(!file_exists($sPath)) {
  152. bx_import('BxDolStudioLanguagesUtils');
  153. BxDolStudioLanguagesUtils::getInstance()->compileLanguage();
  154. }
  155. require($sPath);
  156. }
  157. $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(true);
  158. if(isset($_GET['lang'])) {
  159. bx_import('BxDolPermalinks');
  160. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array('lang')))
  161. exit;
  162. }
  163. }
  164. protected function tryToGetLang($sLangs, $bSetCookie = false) {
  165. $sLangs = trim($sLangs);
  166. if(!$sLangs)
  167. return '';
  168. $sLangs = preg_replace( '/[^a-zA-Z0-9,;-]/m', '', $sLangs ); // we do not need 'q=0.3'. we are using live queue :)
  169. $sLangs = strtolower($sLangs);
  170. if(!$sLangs)
  171. return '';
  172. $aLangs = explode(',', $sLangs); // ru,en-us;q=0.7,en;q=0.3 => array( 'ru' , 'en-us;q=0.7' , 'en;q=0.3' );
  173. foreach($aLangs as $sLang) {
  174. if(!$sLang)
  175. continue;
  176. list($sLang) = explode(';', $sLang, 2); // en-us;q=0.7 => en-us
  177. if(!$sLang)
  178. continue;
  179. // check with country
  180. if($this->checkLangExists($sLang)) {
  181. if( $bSetCookie && (!isset($_COOKIE['lang']) || $_COOKIE['lang'] != $sLang) && (!isset($GLOBALS['glLangSet']) || $GLOBALS['glLangSet'] != $sLang)) {
  182. $this->setLangCookie( $sLang );
  183. $GLOBALS['glLangSet'] = $sLang;
  184. }
  185. return $sLang;
  186. }
  187. //drop country
  188. if(strpos($sLang, '-') === false)
  189. continue;
  190. list($sLang, $sCntr) = explode('-', $sLang, 2); // en-us => en
  191. if(!$sLang or !$sCntr)
  192. continue; //no lang or nothing changed
  193. //check again. without country
  194. if($this->checkLangExists($sLang)) {
  195. if($bSetCookie)
  196. $this->setLangCookie($sLang);
  197. return $sLang;
  198. }
  199. }
  200. return '';
  201. }
  202. protected function checkLangExists( $sLang ) {
  203. if(!preg_match('/^[A-Za-z0-9_]+$/', $sLang))
  204. return false;
  205. $iLangId = $this->oDb->getLanguageId($sLang);
  206. if(!$iLangId)
  207. return false;
  208. if(file_exists( BX_DIRECTORY_PATH_CACHE . "lang-{$sLang}.php"))
  209. return true;
  210. bx_import('BxDolStudioLanguagesUtils');
  211. if(BxDolStudioLanguagesUtils::getInstance()->compileLanguage($iLangId))
  212. return true;
  213. return false;
  214. }
  215. protected function setLangCookie( $sLang ) {
  216. $sLang = bx_process_input($sLang);
  217. if(isLogged()) {
  218. $iLangId = $this->oDb->getLanguageId($sLang, false);
  219. if(!$iLangId)
  220. $iLangId = 0;
  221. $iAccountId = getLoggedId();
  222. bx_import('BxDolAccountQuery');
  223. $oAccountQuery = BxDolAccountQuery::getInstance();
  224. $oAccountQuery->updateLanguage($iAccountId, $iLangId);
  225. }
  226. setcookie( 'lang', '', time() - 60*60*24, '/' );
  227. setcookie( 'lang', $sLang, time() + 60*60*24*365, '/' );
  228. }
  229. }
  230. function _t() {
  231. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t'), func_get_args());
  232. }
  233. function _t_err() {
  234. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_err'), func_get_args());
  235. }
  236. function _t_action() {
  237. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_action'), func_get_args());
  238. }
  239. function _t_ext($key, $args) {
  240. return BxDolLanguages::getInstance()->_t_ext($key, $args);
  241. }
  242. function _t_format_size($iSize) {
  243. return BxDolLanguages::getInstance()->_t_format_size($iSize);
  244. }
  245. function _t_format_currency($fPrice) {
  246. return BxDolLanguages::getInstance()->_t_format_currency($fPrice);
  247. }
  248. function _t_format_extensions($mixedExtensions) {
  249. return BxDolLanguages::getInstance()->_t_format_extensions($mixedExtensions);
  250. }
  251. function bx_lang_name()
  252. {
  253. return BxDolLanguages::getInstance()->getCurrentLanguage();
  254. }
  255. /** @} */