BxDolLanguages.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_LANGUAGE_DEFAULT', 'en');
  10. define('BX_DOL_LANGUAGE_DIRECTION_LTR', 'LTR');
  11. define('BX_DOL_LANGUAGE_DIRECTION_RTL', 'RTL');
  12. define('BX_DOL_LANGUAGE_CATEGORY_SYSTEM', 1);
  13. define('BX_DOL_LANGUAGE_CATEGORY_CUSTOM', 2);
  14. class BxDolLanguages extends BxDolFactory implements iBxDolSingleton
  15. {
  16. protected $oDb;
  17. protected $sCurrentLanguage;
  18. protected function __construct()
  19. {
  20. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  21. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  22. parent::__construct();
  23. $this->oDb = BxDolLanguagesQuery::getInstance();
  24. }
  25. /**
  26. * Prevent cloning the instance
  27. */
  28. public function __clone()
  29. {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. /**
  34. * Get singleton instance of the class
  35. */
  36. static function getInstance()
  37. {
  38. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  39. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLanguages();
  40. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  41. }
  42. return $GLOBALS['bxDolClasses'][__CLASS__];
  43. }
  44. function getCurrentLangName($bSetCookie = true)
  45. {
  46. $sLang = '';
  47. if(!$sLang && !empty($_GET['lang']))
  48. $sLang = $this->tryToGetLang($_GET['lang'], $bSetCookie);
  49. if(!$sLang && !empty($_POST['lang']))
  50. $sLang = $this->tryToGetLang($_POST['lang'], $bSetCookie);
  51. if(!$sLang && !empty($_COOKIE['lang']))
  52. $sLang = $this->tryToGetLang($_COOKIE['lang']);
  53. if(!$sLang && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  54. $sLang = $this->tryToGetLang($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  55. if(!$sLang)
  56. $sLang = $this->tryToGetLang(getParam('lang_default'));
  57. if(!$sLang)
  58. $sLang = $this->tryToGetLang(BX_DOL_LANGUAGE_DEFAULT);
  59. $sLocale = $this->getLangCountryCode($sLang);
  60. if (!$sLocale)
  61. $sLocale = $sLang . '_' . strtoupper($sLang);
  62. setlocale(LC_TIME,
  63. $sLocale . '.utf-8',
  64. $sLocale . '.utf8',
  65. $sLang . '.utf-8',
  66. $sLang . '.utf8',
  67. $sLang);
  68. return $sLang;
  69. }
  70. function getCurrentLangId()
  71. {
  72. return $this->getLangId($this->getCurrentLanguage());
  73. }
  74. function getLangId($sLang)
  75. {
  76. return $this->oDb->getLanguageId($sLang);
  77. }
  78. function getLangName($iLangId)
  79. {
  80. return $this->oDb->getLanguageName($iLangId);
  81. }
  82. function getLangTitle($iLangId)
  83. {
  84. return $this->oDb->getLanguageTitle($iLangId);
  85. }
  86. function getLangFlag($sLang = '')
  87. {
  88. if (!$sLang)
  89. $sLang = $this->getCurrentLanguage();
  90. return $this->oDb->getLanguageFlag($sLang);
  91. }
  92. function getLangDirection($sLang = '')
  93. {
  94. if (!$sLang)
  95. $sLang = $this->getCurrentLanguage();
  96. return strtoupper($this->oDb->getLanguageDirection($sLang));
  97. }
  98. function getLangCountryCode($sLang = '')
  99. {
  100. if (!$sLang)
  101. $sLang = $this->getCurrentLanguage();
  102. return $this->oDb->getLangCountryCode($sLang);
  103. }
  104. function getDefaultLangName()
  105. {
  106. return getParam('lang_default');
  107. }
  108. function getLanguages($bIdAsKey = false, $bActiveOnly = false)
  109. {
  110. return $this->oDb->getLanguages($bIdAsKey, $bActiveOnly);
  111. }
  112. function getLanguagesExt($bIdAsKey = false, $bActiveOnly = false)
  113. {
  114. $aParams = array('type' => 'all_key_' . ($bIdAsKey ? 'id' : 'name'));
  115. if($bActiveOnly)
  116. $aParams['enabled'] = 1;
  117. $aResults = array();
  118. $this->oDb->getLanguagesBy($aParams, $aResults, false);
  119. return $aResults;
  120. }
  121. function getLanguageCategory($sName)
  122. {
  123. $iId = 0;
  124. $this->oDb->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iId);
  125. return (int)$iId;
  126. }
  127. /**
  128. * It tries to match current language with provided languages array, if nothing is found $sFallbackLanguage is returned.
  129. * @param $aLangs - array of languages, example: array('ru' => 1, 'kg' => 1);
  130. * @param $sFallbackLanguage - language to return of nothis is found
  131. * @return language code string
  132. */
  133. function detectLanguageFromArray($aLangs, $sFallbackLanguage = 'en', $bLowercase = false)
  134. {
  135. if (isset($aLangs[$GLOBALS['sCurrentLanguage']])) {
  136. return $GLOBALS['sCurrentLanguage'];
  137. }
  138. elseif ($sLangCountry = $this->getLangCountryCode()) {
  139. if ($bLowercase)
  140. $sLangCountry = strtolower($sLangCountry);
  141. if (isset($aLangs[$sLangCountry]))
  142. return $sLangCountry;
  143. $sLangCountry = str_replace('_', '-', $sLangCountry);
  144. if (isset($aLangs[$sLangCountry]))
  145. return $sLangCountry;
  146. }
  147. return $sFallbackLanguage;
  148. }
  149. /**
  150. * Get current language.
  151. */
  152. function getCurrentLanguage()
  153. {
  154. return $GLOBALS['sCurrentLanguage'];
  155. }
  156. function _t()
  157. {
  158. global $LANG;
  159. $key = func_get_arg(0);
  160. if(isset($LANG[$key])) {
  161. $str = $LANG[$key];
  162. if(($iNumArgs = func_num_args()) > 1)
  163. for($i = 1; $i < $iNumArgs; $i++)
  164. $str = str_replace('{' . ($i - 1) . '}', func_get_arg($i), $str);
  165. return $str;
  166. } else
  167. return $key;
  168. }
  169. function _t_err()
  170. {
  171. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  172. }
  173. function _t_action()
  174. {
  175. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  176. }
  177. function _t_ext($key, $args)
  178. {
  179. global $LANG;
  180. if(isset($LANG[$key])) {
  181. $str = $LANG[$key];
  182. if(!is_array($args))
  183. return str_replace('{0}', $args, $str);
  184. foreach ($args as $key => $val)
  185. $str = str_replace('{'.$key.'}', $val, $str);
  186. return $str;
  187. } else
  188. return $key;
  189. }
  190. function _t_format_size ($iSize)
  191. {
  192. $a = array (
  193. '_sys_format_size_b' => 1024,
  194. '_sys_format_size_kb' => 1024*1024,
  195. '_sys_format_size_mb' => 1024*1024*1024,
  196. '_sys_format_size_gb' => 1024*1024*1024*1024,
  197. '_sys_format_size_tb' => 1024*1024*1024*1024*1024,
  198. );
  199. foreach($a as $sKey => $i)
  200. if ($iSize < $i)
  201. return $this->_t($sKey, round($iSize / ($i / 1024), 1));
  202. return $this->_t('_sys_format_size_b', 0);
  203. }
  204. function _t_format_duration ($iTime)
  205. {
  206. $iTime = (int)$iTime;
  207. $sFormat = '%M:%S';
  208. if($iTime > 3600)
  209. $sFormat = '%H:' . $sFormat;
  210. return strftime($sFormat, $iTime);
  211. }
  212. function _t_format_currency ($fPrice, $iPrecision = 2, $bFormatThousands = false)
  213. {
  214. $sSign = BxDolPayments::getInstance()->getOption('default_currency_sign');
  215. if(empty($sSign))
  216. $sSign = getParam('currency_sign');
  217. if($bFormatThousands)
  218. $fPrice = number_format((float)$fPrice, $iPrecision);
  219. else
  220. $fPrice = sprintf("%." . $iPrecision . "f", (float)$fPrice);
  221. return $this->_t('_sys_currency', $sSign, $fPrice);
  222. }
  223. function _t_format_extensions ($mixedExtensions)
  224. {
  225. if (!is_array($mixedExtensions))
  226. $a = explode(',', $mixedExtensions);
  227. else
  228. $a = $mixedExtensions;
  229. if (!$a)
  230. return '';
  231. return '.' . implode(', .', $a);
  232. }
  233. protected function init()
  234. {
  235. /**
  236. * Trying to initialize default language.
  237. */
  238. $GLOBALS['sCurrentLanguage'] = $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(false);
  239. if($GLOBALS['sCurrentLanguage'] != '') {
  240. $sPath = BX_DIRECTORY_PATH_CACHE . 'lang-' . $GLOBALS['sCurrentLanguage'] . '.php';
  241. if(!file_exists($sPath))
  242. BxDolStudioLanguagesUtils::getInstance()->compileLanguage();
  243. require($sPath);
  244. if($this->getLangDirection($GLOBALS['sCurrentLanguage']) == BX_DOL_LANGUAGE_DIRECTION_RTL)
  245. BxDolTemplate::getInstance()->addCss('rtl.css');
  246. }
  247. $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(true);
  248. if(isset($_GET['lang'])) {
  249. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array('lang')))
  250. exit;
  251. }
  252. }
  253. protected function tryToGetLang($sLangs, $bSetCookie = false)
  254. {
  255. $sLangs = trim($sLangs);
  256. if(!$sLangs)
  257. return '';
  258. $sLangs = preg_replace( '/[^a-zA-Z0-9,;-]/m', '', $sLangs ); // we do not need 'q=0.3'. we are using live queue :)
  259. $sLangs = strtolower($sLangs);
  260. if(!$sLangs)
  261. return '';
  262. $aLangs = explode(',', $sLangs); // ru,en-us;q=0.7,en;q=0.3 => array( 'ru' , 'en-us;q=0.7' , 'en;q=0.3' );
  263. foreach($aLangs as $sLang) {
  264. if(!$sLang)
  265. continue;
  266. list($sLang) = explode(';', $sLang, 2); // en-us;q=0.7 => en-us
  267. if(!$sLang)
  268. continue;
  269. // check with country
  270. if($this->checkLangExists($sLang)) {
  271. if( $bSetCookie && (!isset($_COOKIE['lang']) || $_COOKIE['lang'] != $sLang) && (!isset($GLOBALS['glLangSet']) || $GLOBALS['glLangSet'] != $sLang)) {
  272. $this->setLangCookie( $sLang );
  273. $GLOBALS['glLangSet'] = $sLang;
  274. }
  275. return $sLang;
  276. }
  277. //drop country
  278. if(strpos($sLang, '-') === false)
  279. continue;
  280. list($sLang, $sCntr) = explode('-', $sLang, 2); // en-us => en
  281. if(!$sLang or !$sCntr)
  282. continue; //no lang or nothing changed
  283. //check again. without country
  284. if($this->checkLangExists($sLang)) {
  285. if($bSetCookie)
  286. $this->setLangCookie($sLang);
  287. return $sLang;
  288. }
  289. }
  290. return '';
  291. }
  292. protected function checkLangExists( $sLang )
  293. {
  294. if(!preg_match('/^[A-Za-z0-9_]+$/', $sLang))
  295. return false;
  296. $iLangId = $this->oDb->getLanguageId($sLang, false);
  297. if(!$iLangId)
  298. return false;
  299. if(file_exists( BX_DIRECTORY_PATH_CACHE . "lang-{$sLang}.php"))
  300. return true;
  301. if(BxDolStudioLanguagesUtils::getInstance()->compileLanguage($iLangId))
  302. return true;
  303. return false;
  304. }
  305. protected function setLangCookie( $sLang )
  306. {
  307. $sLang = bx_process_input($sLang);
  308. if(isLogged()) {
  309. $iLangId = $this->oDb->getLanguageId($sLang, false);
  310. if(!$iLangId)
  311. $iLangId = 0;
  312. $iAccountId = getLoggedId();
  313. $oAccountQuery = BxDolAccountQuery::getInstance();
  314. $oAccountQuery->updateLanguage($iAccountId, $iLangId);
  315. }
  316. setcookie( 'lang', '', time() - 60*60*24, '/' );
  317. setcookie( 'lang', $sLang, time() + 60*60*24*365, '/' );
  318. }
  319. }
  320. if (!function_exists('_t')) {
  321. function _t()
  322. {
  323. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t'), func_get_args());
  324. }
  325. }
  326. function _t_err()
  327. {
  328. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_err'), func_get_args());
  329. }
  330. function _t_action()
  331. {
  332. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_action'), func_get_args());
  333. }
  334. function _t_ext($key, $args)
  335. {
  336. return BxDolLanguages::getInstance()->_t_ext($key, $args);
  337. }
  338. function _t_format_size($iSize)
  339. {
  340. return BxDolLanguages::getInstance()->_t_format_size($iSize);
  341. }
  342. function _t_format_duration($iTime)
  343. {
  344. return BxDolLanguages::getInstance()->_t_format_duration($iTime);
  345. }
  346. function _t_format_currency($fPrice, $iPrecision = 2, $bFormatThousands = false)
  347. {
  348. return BxDolLanguages::getInstance()->_t_format_currency($fPrice, $iPrecision, $bFormatThousands);
  349. }
  350. function _t_format_extensions($mixedExtensions)
  351. {
  352. return BxDolLanguages::getInstance()->_t_format_extensions($mixedExtensions);
  353. }
  354. function bx_lang_name()
  355. {
  356. return BxDolLanguages::getInstance()->getCurrentLanguage();
  357. }
  358. function bx_lang_direction($sLanguage = '')
  359. {
  360. return BxDolLanguages::getInstance()->getLangDirection($sLanguage);
  361. }
  362. /** @} */