BxDolLanguages.php 10.0 KB

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