OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxlang.php
Go to the documentation of this file.
1 <?php
2 
6 class oxLang extends oxSuperCfg
7 {
8 
14  protected $_sName = 'lang';
15 
21  protected $_iBaseLanguageId = null;
22 
28  protected $_iTplLanguageId = null;
29 
35  protected $_iEditLanguageId = null;
36 
42  protected $_aLangCache = array();
43 
49  protected $_aAdminTplLanguageArray = null;
50 
56  protected $_aLangAbbr = null;
57 
63  protected $_aAdditionalLangFiles = array();
64 
70  protected $_aLangMap = array();
71 
77  protected $_aActiveModuleInfo = null;
78 
84  protected $_aDisabledModuleInfo = null;
85 
91  protected $_blIsTranslated = true;
92 
98  protected $_iObjectTplLanguageId = null;
99 
105  public function setIsTranslated($blIsTranslated = true)
106  {
107  $this->_blIsTranslated = $blIsTranslated;
108  }
109 
115  public function isTranslated()
116  {
117  return $this->_blIsTranslated;
118  }
119 
125  public function resetBaseLanguage()
126  {
127  $this->_iBaseLanguageId = null;
128  }
129 
135  public function getBaseLanguage()
136  {
137  if ($this->_iBaseLanguageId === null) {
138 
139  $myConfig = $this->getConfig();
140  $blAdmin = $this->isAdmin();
141 
142  // languages and search engines
143  if ($blAdmin && (($iSeLang = oxRegistry::getConfig()->getRequestParameter('changelang')) !== null)) {
144  $this->_iBaseLanguageId = $iSeLang;
145  }
146 
147  if (is_null($this->_iBaseLanguageId)) {
148  $this->_iBaseLanguageId = oxRegistry::getConfig()->getRequestParameter('lang');
149  }
150 
151  //or determining by domain
152  $aLanguageUrls = $myConfig->getConfigParam('aLanguageURLs');
153 
154  if (!$blAdmin && is_array($aLanguageUrls)) {
155  foreach ($aLanguageUrls as $iId => $sUrl) {
156  if ($sUrl && $myConfig->isCurrentUrl($sUrl)) {
157  $this->_iBaseLanguageId = $iId;
158  break;
159  }
160  }
161  }
162 
163  if (is_null($this->_iBaseLanguageId)) {
164  $this->_iBaseLanguageId = oxRegistry::getConfig()->getRequestParameter('language');
165  if (!isset($this->_iBaseLanguageId)) {
166  $this->_iBaseLanguageId = oxRegistry::getSession()->getVariable('language');
167  }
168  }
169 
170  // if language still not set and not search engine browsing,
171  // getting language from browser
172  if (is_null($this->_iBaseLanguageId) && !$blAdmin && !oxRegistry::getUtils()->isSearchEngine()) {
173 
174  // getting from cookie
175  $this->_iBaseLanguageId = oxRegistry::get("oxUtilsServer")->getOxCookie('language');
176 
177  // getting from browser
178  if (is_null($this->_iBaseLanguageId)) {
179  $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
180  }
181  }
182 
183  if (is_null($this->_iBaseLanguageId)) {
184  $this->_iBaseLanguageId = $myConfig->getConfigParam('sDefaultLang');
185  }
186 
187  $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
188 
189  // validating language
190  $this->_iBaseLanguageId = $this->validateLanguage($this->_iBaseLanguageId);
191 
192  oxRegistry::get("oxUtilsServer")->setOxCookie('language', $this->_iBaseLanguageId);
193  }
194 
196  }
197 
203  public function getObjectTplLanguage()
204  {
205  if ($this->_iObjectTplLanguageId === null) {
206  $this->_iObjectTplLanguageId = $this->getTplLanguage();
208  if (!isset($aLanguages[$this->_iObjectTplLanguageId]) ||
209  $aLanguages[$this->_iObjectTplLanguageId]->active == 0
210  ) {
211  $this->_iObjectTplLanguageId = key($aLanguages);
212  }
213  }
214 
216  }
217 
225  public function getTplLanguage()
226  {
227  if ($this->_iTplLanguageId === null) {
228  $iSessLang = oxRegistry::getSession()->getVariable('tpllanguage');
229  $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage($iSessLang) : $this->getBaseLanguage();
230  }
231 
232  return $this->_iTplLanguageId;
233  }
234 
240  public function getEditLanguage()
241  {
242  if ($this->_iEditLanguageId === null) {
243 
244  if (!$this->isAdmin()) {
245  $this->_iEditLanguageId = $this->getBaseLanguage();
246  } else {
247 
248  $iLang = null;
249  // choosing language ident
250  // check if we really need to set the new language
251  if ("saveinnlang" == $this->getConfig()->getActiveView()->getFncName()) {
252  $iLang = oxRegistry::getConfig()->getRequestParameter("new_lang");
253  }
254  $iLang = ($iLang === null) ? oxRegistry::getConfig()->getRequestParameter('editlanguage') : $iLang;
255  $iLang = ($iLang === null) ? oxRegistry::getSession()->getVariable('editlanguage') : $iLang;
256  $iLang = ($iLang === null) ? $this->getBaseLanguage() : $iLang;
257 
258  // validating language
259  $this->_iEditLanguageId = $this->validateLanguage($iLang);
260 
261  // writing to session
262  oxRegistry::getSession()->setVariable('editlanguage', $this->_iEditLanguageId);
263  }
264  }
265 
267  }
268 
278  public function getLanguageArray($iLanguage = null, $blOnlyActive = false, $blSort = false)
279  {
280  $myConfig = $this->getConfig();
281 
282  if (is_null($iLanguage)) {
283  $iLanguage = $this->_iBaseLanguageId;
284  }
285 
286  $aLanguages = array();
287  $aConfLanguages = $myConfig->getConfigParam('aLanguages');
288  $aLangParams = $myConfig->getConfigParam('aLanguageParams');
289 
290  if (is_array($aConfLanguages)) {
291  $i = 0;
292  reset($aConfLanguages);
293  while (list($key, $val) = each($aConfLanguages)) {
294 
295  if ($blOnlyActive && is_array($aLangParams)) {
296  //skipping non active languages
297  if (!$aLangParams[$key]['active']) {
298  $i++;
299  continue;
300  }
301  }
302 
303  if ($val) {
304  $oLang = new stdClass();
305  $oLang->id = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
306  $oLang->oxid = $key;
307  $oLang->abbr = $key;
308  $oLang->name = $val;
309 
310  if (is_array($aLangParams)) {
311  $oLang->active = $aLangParams[$key]['active'];
312  $oLang->sort = $aLangParams[$key]['sort'];
313  }
314 
315  $oLang->selected = (isset($iLanguage) && $oLang->id == $iLanguage) ? 1 : 0;
316  $aLanguages[$oLang->id] = $oLang;
317  }
318  ++$i;
319  }
320  }
321 
322  if ($blSort && is_array($aLangParams)) {
323  uasort($aLanguages, array($this, '_sortLanguagesCallback'));
324  }
325 
326  return $aLanguages;
327  }
328 
334  public function getAdminTplLanguageArray()
335  {
336  if ($this->_aAdminTplLanguageArray === null) {
337  $myConfig = $this->getConfig();
338 
339  $aLangArray = $this->getLanguageArray();
340  $this->_aAdminTplLanguageArray = array();
341 
342  $sSourceDir = $myConfig->getAppDir() . 'views/admin/';
343  foreach ($aLangArray as $iLangKey => $oLang) {
344  $sFilePath = "{$sSourceDir}{$oLang->abbr}/lang.php";
345  if (file_exists($sFilePath) && is_readable($sFilePath)) {
346  $this->_aAdminTplLanguageArray[$iLangKey] = $oLang;
347  }
348  }
349  }
350 
351  // moving pointer to beginning
352  reset($this->_aAdminTplLanguageArray);
353 
355  }
356 
364  public function getLanguageAbbr($iLanguage = null)
365  {
366  if ($this->_aLangAbbr === null) {
367  $this->_aLangAbbr = $this->getLanguageIds();
368  }
369 
370  $iLanguage = isset($iLanguage) ? (int) $iLanguage : $this->getBaseLanguage();
371  if (isset($this->_aLangAbbr[$iLanguage])) {
372  $iLanguage = $this->_aLangAbbr[$iLanguage];
373  }
374 
375  return $iLanguage;
376  }
377 
384  public function getLanguageNames()
385  {
386  $aConfLanguages = $this->getConfig()->getConfigParam('aLanguages');
387  $aLangIds = $this->getLanguageIds();
388  $aLanguages = array();
389  foreach ($aLangIds as $iId => $sValue) {
390  $aLanguages[$iId] = $aConfLanguages[$sValue];
391  }
392 
393  return $aLanguages;
394  }
395 
408  public function translateString($sStringToTranslate, $iLang = null, $blAdminMode = null)
409  {
410  $this->setIsTranslated();
411  // checking if in cache exist
412  $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode);
413  if (isset($aLang[$sStringToTranslate])) {
414  return $aLang[$sStringToTranslate];
415  }
416 
417  // checking if in map exist
418  $aMap = $this->_getLanguageMap($iLang, $blAdminMode);
419  if (isset($aLang[$aMap[$sStringToTranslate]])) {
420  return $aLang[$aMap[$sStringToTranslate]];
421  }
422 
423  // checking if in theme options exist
424  if (count($this->_aAdditionalLangFiles)) {
425  $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode, $this->_aAdditionalLangFiles);
426  if (isset($aLang[$sStringToTranslate])) {
427  return $aLang[$sStringToTranslate];
428  }
429  }
430 
431  $this->setIsTranslated(false);
432 
433  return $sStringToTranslate;
434  }
435 
446  protected function _collectSimilar($aData, $sKey, $aCollection = array())
447  {
448  foreach ($aData as $sValKey => $sValue) {
449  if (strpos($sValKey, $sKey) === 0) {
450  $aCollection[$sValKey] = $sValue;
451  }
452  }
453 
454  return $aCollection;
455  }
456 
467  public function getSimilarByKey($sKey, $iLang = null, $blAdmin = null)
468  {
469  startProfile("getSimilarByKey");
470 
471  $iLang = isset($iLang) ? $iLang : $this->getTplLanguage();
472  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
473 
474  // checking if exists in cache
475  $aLang = $this->_getLangTranslationArray($iLang, $blAdmin);
476  $aSimilarConst = $this->_collectSimilar($aLang, $sKey);
477 
478  // checking if in map exist
479  $aMap = $this->_getLanguageMap($iLang, $blAdmin);
480  $aSimilarConst = $this->_collectSimilar($aMap, $sKey, $aSimilarConst);
481 
482  // checking if in theme options exist
483  if (count($this->_aAdditionalLangFiles)) {
484  $aLang = $this->_getLangTranslationArray($iLang, $blAdmin, $this->_aAdditionalLangFiles);
485  $aSimilarConst = $this->_collectSimilar($aLang, $sKey, $aSimilarConst);
486  }
487 
488  stopProfile("getSimilarByKey");
489 
490  return $aSimilarConst;
491  }
492 
501  public function formatCurrency($dValue, $oActCur = null)
502  {
503  if (!$oActCur) {
504  $oActCur = $this->getConfig()->getActShopCurrencyObject();
505  }
506  $sValue = oxRegistry::getUtils()->fRound($dValue, $oActCur);
507 
508  return number_format((double) $sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand);
509  }
510 
519  public function formatVat($dValue, $oActCur = null)
520  {
521  $iDecPos = 0;
522  $sValue = ( string ) $dValue;
524  $oStr = getStr();
525  if (($iDotPos = $oStr->strpos($sValue, '.')) !== false) {
526  $iDecPos = $oStr->strlen($oStr->substr($sValue, $iDotPos + 1));
527  }
528 
529  $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
530  $iDecPos = ($iDecPos < $oActCur->decimal) ? $iDecPos : $oActCur->decimal;
531 
532  return number_format((double) $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand);
533  }
534 
542  public function getLanguageTag($iLanguage = null)
543  {
544  if (!isset($iLanguage)) {
545  $iLanguage = $this->getBaseLanguage();
546  }
547 
548  $iLanguage = (int) $iLanguage;
549 
550  return (($iLanguage) ? "_$iLanguage" : "");
551  }
552 
560  public function validateLanguage($iLang = null)
561  {
562  $iLang = (int) $iLang;
563 
564  // checking if this language is valid
565  $aLanguages = $this->getLanguageArray(null, !$this->isAdmin());
566  if (!isset($aLanguages[$iLang]) && is_array($aLanguages)) {
567  $oLang = current($aLanguages);
568  if (isset($oLang->id)) {
569  $iLang = $oLang->id;
570  }
571  }
572 
573  return $iLang;
574  }
575 
581  public function setBaseLanguage($iLang = null)
582  {
583  if (is_null($iLang)) {
584  $iLang = $this->getBaseLanguage();
585  } else {
586  $this->_iBaseLanguageId = (int) $iLang;
587  }
588 
589  if (defined('OXID_PHP_UNIT')) {
590  modSession::getInstance();
591  }
592 
593  oxRegistry::getSession()->setVariable('language', $iLang);
594  }
595 
603  public function setTplLanguage($iLang = null)
604  {
605  $this->_iTplLanguageId = isset($iLang) ? (int) $iLang : $this->getBaseLanguage();
606  if ($this->isAdmin()) {
608  if (!isset($aLanguages[$this->_iTplLanguageId])) {
609  $this->_iTplLanguageId = key($aLanguages);
610  }
611  }
612 
613  if (defined('OXID_PHP_UNIT')) {
614  modSession::getInstance();
615  }
616 
617  oxRegistry::getSession()->setVariable('tpllanguage', $this->_iTplLanguageId);
618 
619  return $this->_iTplLanguageId;
620  }
621 
631  protected function _recodeLangArray($aLangArray, $sCharset, $blRecodeKeys = false)
632  {
633  $aLangs = array();
634  foreach ($aLangArray as $sKey => $sValue) {
635  $sItemKey = $sKey;
636  if ($blRecodeKeys === true) {
637  $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
638  }
639 
640  $aLangs[$sItemKey] = iconv($sCharset, 'UTF-8', $sValue);
641  unset($aLangArray[$sKey]);
642  }
643 
644  return $aLangs;
645  }
646 
654  protected function _getLangFilesPathArray($iLang)
655  {
656  $oConfig = $this->getConfig();
657  $aLangFiles = array();
658 
659  $sAppDir = $oConfig->getAppDir();
660  $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
661  $sTheme = $oConfig->getConfigParam("sTheme");
662  $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
663  $sShopId = $oConfig->getShopId();
664  $aModulePaths = $this->_getActiveModuleInfo();
665 
666  //get generic lang files
667  $sGenericPath = $sAppDir . 'translations/' . $sLang;
668  if ($sGenericPath) {
669  $aLangFiles[] = $sGenericPath . "/lang.php";
670  $aLangFiles = $this->_appendLangFile($aLangFiles, $sGenericPath);
671  }
672 
673  //get theme lang files
674  if ($sTheme) {
675  $sThemePath = $sAppDir . 'views/' . $sTheme . '/' . $sLang;
676  $aLangFiles[] = $sThemePath . "/lang.php";
677  $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath);
678  }
679 
680  //get custom theme lang files
681  if ($sCustomTheme) {
682  $sCustPath = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang;
683  $aLangFiles[] = $sCustPath . "/lang.php";
684  $aLangFiles = $this->_appendLangFile($aLangFiles, $sCustPath);
685  }
686 
687 
688  // custom theme shop languages
689 
690  // modules language files
691  $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang);
692 
693  // custom language files
694  $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang);
695 
696  return count($aLangFiles) ? $aLangFiles : false;
697  }
698 
706  protected function _getAdminLangFilesPathArray($iLang)
707  {
708  $oConfig = $this->getConfig();
709  $aLangFiles = array();
710 
711  $sAppDir = $oConfig->getAppDir();
712  $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
713 
714  $aModulePaths = array();
715  $aModulePaths = array_merge($aModulePaths, $this->_getActiveModuleInfo());
716  $aModulePaths = array_merge($aModulePaths, $this->_getDisabledModuleInfo());
717 
718  // admin lang files
719  $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
720  $aLangFiles[] = $sAdminPath . "/lang.php";
721  $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
722  $aLangFiles = $this->_appendLangFile($aLangFiles, $sAdminPath);
723 
724  // themes options lang files
725  $sThemePath = $sAppDir . 'views/*/' . $sLang;
726  $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath, "options");
727 
728  // module language files
729  $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, true);
730 
731  // custom language files
732  $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang, true);
733 
734  return count($aLangFiles) ? $aLangFiles : false;
735  }
736 
746  protected function _appendLangFile($aLangFiles, $sFullPath, $sFilePattern = "lang")
747  {
748  $aFiles = glob($sFullPath . "/*_{$sFilePattern}.php");
749  if (is_array($aFiles) && count($aFiles)) {
750  foreach ($aFiles as $sFile) {
751  if (!strpos($sFile, 'cust_lang.php')) {
752  $aLangFiles[] = $sFile;
753  }
754  }
755  }
756 
757  return $aLangFiles;
758  }
759 
769  protected function _appendCustomLangFiles($aLangFiles, $sLang, $blForAdmin = false)
770  {
771  $oConfig = $this->getConfig();
772  $sAppDir = $oConfig->getAppDir();
773  $sTheme = $oConfig->getConfigParam("sTheme");
774  $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
775 
776  if ($blForAdmin) {
777  $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
778  } else {
779  if ($sTheme) {
780  $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
781  }
782  if ($sCustomTheme) {
783  $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
784  }
785  }
786 
787  return $aLangFiles;
788  }
789 
800  protected function _appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, $blForAdmin = false)
801  {
802  if (is_array($aModulePaths)) {
803  $oConfig = $this->getConfig();
804  foreach ($aModulePaths as $sPath) {
805  $sFullPath = $oConfig->getModulesDir() . $sPath;
806  if (file_exists($sFullPath . '/application/')) {
807  $sFullPath .= '/application';
808  }
809  $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
810  $sFullPath .= $sLang;
811  $aLangFiles = $this->_appendLangFile($aLangFiles, $sFullPath);
812  //load admin modules options lang files
813  if ($blForAdmin) {
814  $aLangFiles[] = $sFullPath . '/module_options.php';
815  }
816  }
817  }
818 
819  return $aLangFiles;
820  }
821 
831  protected function _getLangFileCacheName($blAdmin, $iLang, $aLangFiles = null)
832  {
833  $myConfig = $this->getConfig();
834  $sLangFilesIdent = '_default';
835  if (is_array($aLangFiles) && $aLangFiles) {
836  $sLangFilesIdent = '_' . md5(implode('+', $aLangFiles));
837  }
838 
839  return "langcache_" . ((int) $blAdmin) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam('sTheme') . $sLangFilesIdent;
840  }
841 
851  protected function _getLanguageFileData($blAdmin = false, $iLang = 0, $aLangFiles = null)
852  {
853  $myConfig = $this->getConfig();
854  $myUtils = oxRegistry::getUtils();
855 
856  $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
857  $aLangCache = $myUtils->getLangCache($sCacheName);
858  if (!$aLangCache && $aLangFiles === null) {
859  if ($blAdmin) {
860  $aLangFiles = $this->_getAdminLangFilesPathArray($iLang);
861  } else {
862  $aLangFiles = $this->_getLangFilesPathArray($iLang);
863  }
864  }
865  if (!$aLangCache && $aLangFiles) {
866  $aLangCache = array();
867  $sBaseCharset = false;
868  $aLang = array();
869  $aLangSeoReplaceChars = array();
870  foreach ($aLangFiles as $sLangFile) {
871 
872  if (file_exists($sLangFile) && is_readable($sLangFile)) {
873  $aSeoReplaceChars = array();
874  include $sLangFile;
875 
876  // including only (!) those, which has charset defined
877  if (isset($aLang['charset'])) {
878 
879  // recoding only in utf
880  if ($myConfig->isUtf()) {
881  $aLang = $this->_recodeLangArray($aLang, $aLang['charset']);
882 
883  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
884  $aSeoReplaceChars = $this->_recodeLangArray($aSeoReplaceChars, $aLang['charset'], true);
885  }
886 
887  // overriding charset
888  $aLang['charset'] = 'UTF-8';
889  }
890 
891  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
892  $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
893  }
894 
895  if (!$sBaseCharset) {
896  $sBaseCharset = $aLang['charset'];
897  }
898 
899  $aLangCache = array_merge($aLangCache, $aLang);
900  }
901  }
902  }
903 
904  // setting base charset
905  if ($sBaseCharset) {
906  $aLangCache['charset'] = $sBaseCharset;
907  }
908 
909  // special character replacement list
910  $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
911 
912  //save to cache
913  $myUtils->setLangCache($sCacheName, $aLangCache);
914  }
915 
916  return $aLangCache;
917  }
918 
927  protected function _getLanguageMap($iLang, $blAdmin = null)
928  {
929  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
930  $sKey = $iLang . ((int) $blAdmin);
931  if (!isset($this->_aLangMap[$sKey])) {
932  $this->_aLangMap[$sKey] = array();
933  $myConfig = $this->getConfig();
934 
935  $sMapFile = '';
936  $sParentMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
937  $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sCustomTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
938 
939  if (file_exists($sCustomThemeMapFile) && is_readable($sCustomThemeMapFile)) {
940  $sMapFile = $sCustomThemeMapFile;
941  } elseif (file_exists($sParentMapFile) && is_readable($sParentMapFile)) {
942  $sMapFile = $sParentMapFile;
943  }
944 
945  if ($sMapFile) {
946  include $sMapFile;
947  $this->_aLangMap[$sKey] = $aMap;
948  }
949 
950  }
951 
952  return $this->_aLangMap[$sKey];
953  }
954 
963  protected function _getCacheLanguageId($blAdmin, $iLang = null)
964  {
965  $iLang = ($iLang === null && $blAdmin) ? $this->getTplLanguage() : $iLang;
966  if (!isset($iLang)) {
967  $iLang = $this->getBaseLanguage();
968  if (!isset($iLang)) {
969  $iLang = 0;
970  }
971  }
972 
973  return (int) $iLang;
974  }
975 
985  protected function _getLangTranslationArray($iLang = null, $blAdmin = null, $aLangFiles = null)
986  {
987  startProfile("_getLangTranslationArray");
988 
989  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
990  $iLang = $this->_getCacheLanguageId($blAdmin, $iLang);
991  $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
992 
993  if (!isset($this->_aLangCache[$sCacheName])) {
994  $this->_aLangCache[$sCacheName] = array();
995  }
996  if (!isset($this->_aLangCache[$sCacheName][$iLang])) {
997  // loading main lang files data
998  $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData($blAdmin, $iLang, $aLangFiles);
999  }
1000 
1001  stopProfile("_getLangTranslationArray");
1002 
1003  // if language array exists ..
1004  return (isset($this->_aLangCache[$sCacheName][$iLang]) ? $this->_aLangCache[$sCacheName][$iLang] : array());
1005  }
1006 
1015  protected function _sortLanguagesCallback($a1, $a2)
1016  {
1017  return ($a1->sort > $a2->sort);
1018  }
1019 
1025  public function getName()
1026  {
1027  return $this->_sName;
1028  }
1029 
1035  public function getFormLang()
1036  {
1037  $sLang = null;
1038  if (!$this->isAdmin()) {
1039  $sLang = "<input type=\"hidden\" name=\"" . $this->getName() . "\" value=\"" . $this->getBaseLanguage() . "\" />";
1040  }
1041 
1042  return $sLang;
1043  }
1044 
1052  public function getUrlLang($iLang = null)
1053  {
1054  $sLang = null;
1055  if (!$this->isAdmin()) {
1056  $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
1057  $sLang = $this->getName() . "=" . $iLang;
1058  }
1059 
1060  return $sLang;
1061  }
1062 
1075  public function processUrl($sUrl, $iLang = null)
1076  {
1077  $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
1078  $iDefaultLang = intval(oxRegistry::getConfig()->getConfigParam('sDefaultLang'));
1079  $iBrowserLanguage = intval($this->detectLanguageByBrowser());
1081  $oStr = getStr();
1082 
1083  if (!$this->isAdmin()) {
1084  $sParam = $this->getUrlLang($iLang);
1085  if (!$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) &&
1086  ($iLang != $iDefaultLang || $iDefaultLang != $iBrowserLanguage)
1087  ) {
1088  if ($sUrl) {
1089  if ($oStr->strpos($sUrl, '?') === false) {
1090  $sUrl .= "?";
1091  } elseif (!$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl)) {
1092  $sUrl .= "&amp;";
1093  }
1094  }
1095  $sUrl .= $sParam . "&amp;";
1096  } else {
1097  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1' . $sParam, $sUrl);
1098  }
1099  }
1100 
1101  return $sUrl;
1102  }
1103 
1110  public function detectLanguageByBrowser()
1111  {
1112  $sBrowserLanguage = $this->_getBrowserLanguage();
1113 
1114  if (!is_null($sBrowserLanguage)) {
1115 
1116  $aLanguages = $this->getLanguageArray(null, true);
1117  foreach ($aLanguages as $oLang) {
1118  if ($oLang->abbr == $sBrowserLanguage) {
1119  return $oLang->id;
1120  }
1121  }
1122  }
1123  }
1124 
1130  public function getMultiLangTables()
1131  {
1132  $aTables = array("oxarticles", "oxartextends", "oxattribute",
1133  "oxcategories", "oxcontents", "oxcountry",
1134  "oxdelivery", "oxdiscount", "oxgroups",
1135  "oxlinks", "oxnews", "oxobject2attribute",
1136  "oxpayments", "oxselectlist", "oxshops",
1137  "oxactions", "oxwrapping", "oxdeliveryset",
1138  "oxvendor", "oxmanufacturers", "oxmediaurls",
1139  "oxstates");
1140 
1141 
1142  $aMultiLangTables = $this->getConfig()->getConfigParam('aMultiLangTables');
1143 
1144  if (is_array($aMultiLangTables)) {
1145  $aTables = array_merge($aTables, $aMultiLangTables);
1146  }
1147 
1148  return $aTables;
1149  }
1150 
1158  public function getSeoReplaceChars($iLang)
1159  {
1160  // get language replace chars
1161  $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
1162  if (!is_array($aSeoReplaceChars)) {
1163  $aSeoReplaceChars = array();
1164  }
1165 
1166  return $aSeoReplaceChars;
1167  }
1168 
1174  protected function _getActiveModuleInfo()
1175  {
1176  if ($this->_aActiveModuleInfo === null) {
1177  $oModuleList = oxNew('oxModuleList');
1178  $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
1179  }
1180 
1182  }
1183 
1189  protected function _getDisabledModuleInfo()
1190  {
1191  if ($this->_aDisabledModuleInfo === null) {
1192  $oModuleList = oxNew('oxModuleList');
1193  $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
1194  }
1195 
1197  }
1198 
1204  protected function _getBrowserLanguage()
1205  {
1206  $sBrowserLang = null;
1207  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
1208  $sBrowserLang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
1209  }
1210 
1211  return $sBrowserLang;
1212  }
1213 
1219  public function getAllShopLanguageIds()
1220  {
1222 
1223  return $aLanguages;
1224  }
1225 
1233  public function getLanguageIds($iShopId = null)
1234  {
1235  if (empty($iShopId) || $iShopId == $this->getConfig()->getShopId()) {
1237  } else {
1238  $aLanguages = $this->_getLanguageIdsFromDatabase($iShopId);
1239  }
1240 
1241  return $aLanguages;
1242  }
1243 
1249  public function getActiveShopLanguageIds()
1250  {
1251  $oConfig = $this->getConfig();
1252 
1253  //if exists language parameters array, extract lang id's from there
1254  $aLangParams = $oConfig->getConfigParam('aLanguageParams');
1255  if (is_array($aLangParams)) {
1256  $aIds = $this->_getLanguageIdsFromLanguageParamsArray($aLangParams);
1257  } else {
1258  $aIds = $this->_getLanguageIdsFromLanguagesArray($oConfig->getConfigParam('aLanguages'));
1259  }
1260 
1261  return $aIds;
1262  }
1263 
1271  protected function _getLanguageIdsFromDatabase($iShopId = null)
1272  {
1273  $aLanguages = $this->getLanguageIds();
1274 
1275 
1276  return $aLanguages;
1277  }
1278 
1287  protected function _getConfigLanguageValues($sLanguageParameterName, $iShopId = null)
1288  {
1289  $aConfigDecodedValues = array();
1290  $aConfigValues = $this->_selectLanguageParamValues($sLanguageParameterName, $iShopId);
1291 
1292  foreach ($aConfigValues as $sConfigValue) {
1293  $aConfigLanguages = unserialize($sConfigValue['oxvarvalue']);
1294 
1295  $aLanguages = array();
1296  if ($sLanguageParameterName == 'aLanguageParams') {
1297  $aLanguages = $this->_getLanguageIdsFromLanguageParamsArray($aConfigLanguages);
1298  } elseif ($sLanguageParameterName == 'aLanguages') {
1299  $aLanguages = $this->_getLanguageIdsFromLanguagesArray($aConfigLanguages);
1300  }
1301 
1302  $aConfigDecodedValues = array_unique(array_merge($aConfigDecodedValues, $aLanguages));
1303  }
1304 
1305  return $aConfigDecodedValues;
1306  }
1307 
1316  protected function _selectLanguageParamValues($sParamName, $sShopId = null)
1317  {
1319  $oConfig = oxRegistry::getConfig();
1320 
1321  $sQuery = "
1322  select " . $oConfig->getDecodeValueQuery() . " as oxvarvalue
1323  from oxconfig
1324  where oxvarname = '{$sParamName}' ";
1325 
1326  if (!empty($sShopId)) {
1327  $sQuery .= " and oxshopid = '{$sShopId}' limit 1";
1328  }
1329 
1330  return $oDb->getArray($sQuery);
1331  }
1332 
1333 
1341  protected function _getLanguageIdsFromLanguageParamsArray($aLanguageParams)
1342  {
1343  $aLanguages = array();
1344  foreach ($aLanguageParams as $sAbbr => $aValue) {
1345  $iBaseId = (int) $aValue['baseId'];
1346  $aLanguages[$iBaseId] = $sAbbr;
1347  }
1348 
1349  return $aLanguages;
1350  }
1351 
1360  {
1361  return array_keys($aLanguages);
1362  }
1363 }