OXID eShop CE  4.10.1
 All Classes Namespaces 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 
406  public function translateString($sStringToTranslate, $iLang = null, $blAdminMode = null)
407  {
408  $this->setIsTranslated();
409  // checking if in cache exist
410  $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode);
411  if (isset($aLang[$sStringToTranslate])) {
412  return $aLang[$sStringToTranslate];
413  }
414 
415  // checking if in map exist
416  $aMap = $this->_getLanguageMap($iLang, $blAdminMode);
417  if (isset($aLang[$aMap[$sStringToTranslate]])) {
418  return $aLang[$aMap[$sStringToTranslate]];
419  }
420 
421  // checking if in theme options exist
422  if (count($this->_aAdditionalLangFiles)) {
423  $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode, $this->_aAdditionalLangFiles);
424  if (isset($aLang[$sStringToTranslate])) {
425  return $aLang[$sStringToTranslate];
426  }
427  }
428 
429  $this->setIsTranslated(false);
430 
431  return $sStringToTranslate;
432  }
433 
444  protected function _collectSimilar($aData, $sKey, $aCollection = array())
445  {
446  foreach ($aData as $sValKey => $sValue) {
447  if (strpos($sValKey, $sKey) === 0) {
448  $aCollection[$sValKey] = $sValue;
449  }
450  }
451 
452  return $aCollection;
453  }
454 
465  public function getSimilarByKey($sKey, $iLang = null, $blAdmin = null)
466  {
467  startProfile("getSimilarByKey");
468 
469  $iLang = isset($iLang) ? $iLang : $this->getTplLanguage();
470  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
471 
472  // checking if exists in cache
473  $aLang = $this->_getLangTranslationArray($iLang, $blAdmin);
474  $aSimilarConst = $this->_collectSimilar($aLang, $sKey);
475 
476  // checking if in map exist
477  $aMap = $this->_getLanguageMap($iLang, $blAdmin);
478  $aSimilarConst = $this->_collectSimilar($aMap, $sKey, $aSimilarConst);
479 
480  // checking if in theme options exist
481  if (count($this->_aAdditionalLangFiles)) {
482  $aLang = $this->_getLangTranslationArray($iLang, $blAdmin, $this->_aAdditionalLangFiles);
483  $aSimilarConst = $this->_collectSimilar($aLang, $sKey, $aSimilarConst);
484  }
485 
486  stopProfile("getSimilarByKey");
487 
488  return $aSimilarConst;
489  }
490 
499  public function formatCurrency($dValue, $oActCur = null)
500  {
501  if (!$oActCur) {
502  $oActCur = $this->getConfig()->getActShopCurrencyObject();
503  }
504  $sValue = oxRegistry::getUtils()->fRound($dValue, $oActCur);
505 
506  return number_format((double) $sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand);
507  }
508 
517  public function formatVat($dValue, $oActCur = null)
518  {
519  $iDecPos = 0;
520  $sValue = ( string ) $dValue;
522  $oStr = getStr();
523  if (($iDotPos = $oStr->strpos($sValue, '.')) !== false) {
524  $iDecPos = $oStr->strlen($oStr->substr($sValue, $iDotPos + 1));
525  }
526 
527  $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
528  $iDecPos = ($iDecPos < $oActCur->decimal) ? $iDecPos : $oActCur->decimal;
529 
530  return number_format((double) $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand);
531  }
532 
540  public function getLanguageTag($iLanguage = null)
541  {
542  if (!isset($iLanguage)) {
543  $iLanguage = $this->getBaseLanguage();
544  }
545 
546  $iLanguage = (int) $iLanguage;
547 
548  return (($iLanguage) ? "_$iLanguage" : "");
549  }
550 
558  public function validateLanguage($iLang = null)
559  {
560  $iLang = (int) $iLang;
561 
562  // checking if this language is valid
563  $aLanguages = $this->getLanguageArray(null, !$this->isAdmin());
564  if (!isset($aLanguages[$iLang]) && is_array($aLanguages)) {
565  $oLang = current($aLanguages);
566  if (isset($oLang->id)) {
567  $iLang = $oLang->id;
568  }
569  }
570 
571  return $iLang;
572  }
573 
579  public function setBaseLanguage($iLang = null)
580  {
581  if (is_null($iLang)) {
582  $iLang = $this->getBaseLanguage();
583  } else {
584  $this->_iBaseLanguageId = (int) $iLang;
585  }
586 
587  if (defined('OXID_PHP_UNIT')) {
588  modSession::getInstance();
589  }
590 
591  oxRegistry::getSession()->setVariable('language', $iLang);
592  }
593 
601  public function setTplLanguage($iLang = null)
602  {
603  $this->_iTplLanguageId = isset($iLang) ? (int) $iLang : $this->getBaseLanguage();
604  if ($this->isAdmin()) {
606  if (!isset($aLanguages[$this->_iTplLanguageId])) {
607  $this->_iTplLanguageId = key($aLanguages);
608  }
609  }
610 
611  if (defined('OXID_PHP_UNIT')) {
612  modSession::getInstance();
613  }
614 
615  oxRegistry::getSession()->setVariable('tpllanguage', $this->_iTplLanguageId);
616 
617  return $this->_iTplLanguageId;
618  }
619 
629  protected function _recodeLangArray($aLangArray, $sCharset, $blRecodeKeys = false)
630  {
631  $newEncoding = $this->getTranslationsExpectedEncoding();
632 
633  if ($sCharset == $newEncoding) {
634  return $aLangArray;
635  }
636 
637  $aLangs = array();
638  foreach ($aLangArray as $sKey => $sValue) {
639  $sItemKey = $sKey;
640  if ($blRecodeKeys === true) {
641  $sItemKey = iconv($sCharset, $newEncoding, $sItemKey);
642  }
643 
644  $aLangs[$sItemKey] = iconv($sCharset, $newEncoding, $sValue);
645  unset($aLangArray[$sKey]);
646  }
647 
648  return $aLangs;
649  }
650 
656  protected function getTranslationsExpectedEncoding()
657  {
658  $shopConfig = $this->getConfig();
659 
660  $newEncoding = 'ISO-8859-15';
661  if ($shopConfig->isUtf()) {
662  $newEncoding = 'UTF-8';
663  }
664 
665  return $newEncoding;
666  }
667 
675  protected function _getLangFilesPathArray($iLang)
676  {
677  $oConfig = $this->getConfig();
678  $aLangFiles = array();
679 
680  $sAppDir = $oConfig->getAppDir();
681  $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
682  $sTheme = $oConfig->getConfigParam("sTheme");
683  $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
684  $sShopId = $oConfig->getShopId();
685  $aModulePaths = $this->_getActiveModuleInfo();
686 
687  //get generic lang files
688  $sGenericPath = $sAppDir . 'translations/' . $sLang;
689  if ($sGenericPath) {
690  $aLangFiles[] = $sGenericPath . "/lang.php";
691  $aLangFiles = $this->_appendLangFile($aLangFiles, $sGenericPath);
692  }
693 
694  //get theme lang files
695  if ($sTheme) {
696  $sThemePath = $sAppDir . 'views/' . $sTheme . '/' . $sLang;
697  $aLangFiles[] = $sThemePath . "/lang.php";
698  $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath);
699  }
700 
701  //get custom theme lang files
702  if ($sCustomTheme) {
703  $sCustPath = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang;
704  $aLangFiles[] = $sCustPath . "/lang.php";
705  $aLangFiles = $this->_appendLangFile($aLangFiles, $sCustPath);
706  }
707 
708 
709  // custom theme shop languages
710 
711  // modules language files
712  $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang);
713 
714  // custom language files
715  $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang);
716 
717  return count($aLangFiles) ? $aLangFiles : false;
718  }
719 
727  protected function _getAdminLangFilesPathArray($iLang)
728  {
729  $oConfig = $this->getConfig();
730  $aLangFiles = array();
731 
732  $sAppDir = $oConfig->getAppDir();
733  $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
734 
735  $aModulePaths = array();
736  $aModulePaths = array_merge($aModulePaths, $this->_getActiveModuleInfo());
737  $aModulePaths = array_merge($aModulePaths, $this->_getDisabledModuleInfo());
738 
739  // admin lang files
740  $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
741  $aLangFiles[] = $sAdminPath . "/lang.php";
742  $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
743  $aLangFiles = $this->_appendLangFile($aLangFiles, $sAdminPath);
744 
745  // themes options lang files
746  $sThemePath = $sAppDir . 'views/*/' . $sLang;
747  $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath, "options");
748 
749  // module language files
750  $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, true);
751 
752  // custom language files
753  $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang, true);
754 
755  return count($aLangFiles) ? $aLangFiles : false;
756  }
757 
767  protected function _appendLangFile($aLangFiles, $sFullPath, $sFilePattern = "lang")
768  {
769  $aFiles = glob($sFullPath . "/*_{$sFilePattern}.php");
770  if (is_array($aFiles) && count($aFiles)) {
771  foreach ($aFiles as $sFile) {
772  if (!strpos($sFile, 'cust_lang.php')) {
773  $aLangFiles[] = $sFile;
774  }
775  }
776  }
777 
778  return $aLangFiles;
779  }
780 
790  protected function _appendCustomLangFiles($aLangFiles, $sLang, $blForAdmin = false)
791  {
792  $oConfig = $this->getConfig();
793  $sAppDir = $oConfig->getAppDir();
794  $sTheme = $oConfig->getConfigParam("sTheme");
795  $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
796 
797  if ($blForAdmin) {
798  $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
799  } else {
800  if ($sTheme) {
801  $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
802  }
803  if ($sCustomTheme) {
804  $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
805  }
806  }
807 
808  return $aLangFiles;
809  }
810 
821  protected function _appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, $blForAdmin = false)
822  {
823  if (is_array($aModulePaths)) {
824  $oConfig = $this->getConfig();
825  foreach ($aModulePaths as $sPath) {
826  $sFullPath = $oConfig->getModulesDir() . $sPath;
827  if (file_exists($sFullPath . '/application/')) {
828  $sFullPath .= '/application';
829  }
830  $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
831  $sFullPath .= $sLang;
832  $aLangFiles = $this->_appendLangFile($aLangFiles, $sFullPath);
833  //load admin modules options lang files
834  if ($blForAdmin) {
835  $aLangFiles[] = $sFullPath . '/module_options.php';
836  }
837  }
838  }
839 
840  return $aLangFiles;
841  }
842 
852  protected function _getLangFileCacheName($blAdmin, $iLang, $aLangFiles = null)
853  {
854  $myConfig = $this->getConfig();
855  $sLangFilesIdent = '_default';
856  if (is_array($aLangFiles) && $aLangFiles) {
857  $sLangFilesIdent = '_' . md5(implode('+', $aLangFiles));
858  }
859 
860  return "langcache_" . ((int) $blAdmin) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam('sTheme') . $sLangFilesIdent;
861  }
862 
872  protected function _getLanguageFileData($blAdmin = false, $iLang = 0, $aLangFiles = null)
873  {
874  $myConfig = $this->getConfig();
875  $myUtils = oxRegistry::getUtils();
876 
877  $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
878  $aLangCache = $myUtils->getLangCache($sCacheName);
879  if (!$aLangCache && $aLangFiles === null) {
880  if ($blAdmin) {
881  $aLangFiles = $this->_getAdminLangFilesPathArray($iLang);
882  } else {
883  $aLangFiles = $this->_getLangFilesPathArray($iLang);
884  }
885  }
886  if (!$aLangCache && $aLangFiles) {
887  $aLangCache = array();
888  $aLang = array();
889  $aLangSeoReplaceChars = array();
890  foreach ($aLangFiles as $sLangFile) {
891 
892  if (file_exists($sLangFile) && is_readable($sLangFile)) {
893  $aSeoReplaceChars = array();
894  include $sLangFile;
895 
896  // including only (!) those, which has charset defined
897  if (isset($aLang['charset'])) {
898 
899  $aLang = $this->_recodeLangArray($aLang, $aLang['charset']);
900 
901  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
902  $aSeoReplaceChars = $this->_recodeLangArray($aSeoReplaceChars, $aLang['charset'], true);
903  }
904 
905  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
906  $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
907  }
908 
909  $aLangCache = array_merge($aLangCache, $aLang);
910  }
911  }
912  }
913 
914  $aLangCache['charset'] = $this->getTranslationsExpectedEncoding();
915 
916  // special character replacement list
917  $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
918 
919  //save to cache
920  $myUtils->setLangCache($sCacheName, $aLangCache);
921  }
922 
923  return $aLangCache;
924  }
925 
934  protected function _getLanguageMap($iLang, $blAdmin = null)
935  {
936  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
937  $sKey = $iLang . ((int) $blAdmin);
938  if (!isset($this->_aLangMap[$sKey])) {
939  $this->_aLangMap[$sKey] = array();
940  $myConfig = $this->getConfig();
941 
942  $sMapFile = '';
943  $sParentMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
944  $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sCustomTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
945 
946  if (file_exists($sCustomThemeMapFile) && is_readable($sCustomThemeMapFile)) {
947  $sMapFile = $sCustomThemeMapFile;
948  } elseif (file_exists($sParentMapFile) && is_readable($sParentMapFile)) {
949  $sMapFile = $sParentMapFile;
950  }
951 
952  if ($sMapFile) {
953  include $sMapFile;
954  $this->_aLangMap[$sKey] = $aMap;
955  }
956 
957  }
958 
959  return $this->_aLangMap[$sKey];
960  }
961 
970  protected function _getCacheLanguageId($blAdmin, $iLang = null)
971  {
972  $iLang = ($iLang === null && $blAdmin) ? $this->getTplLanguage() : $iLang;
973  if (!isset($iLang)) {
974  $iLang = $this->getBaseLanguage();
975  if (!isset($iLang)) {
976  $iLang = 0;
977  }
978  }
979 
980  return (int) $iLang;
981  }
982 
992  protected function _getLangTranslationArray($iLang = null, $blAdmin = null, $aLangFiles = null)
993  {
994  startProfile("_getLangTranslationArray");
995 
996  $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
997  $iLang = $this->_getCacheLanguageId($blAdmin, $iLang);
998  $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
999 
1000  if (!isset($this->_aLangCache[$sCacheName])) {
1001  $this->_aLangCache[$sCacheName] = array();
1002  }
1003  if (!isset($this->_aLangCache[$sCacheName][$iLang])) {
1004  // loading main lang files data
1005  $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData($blAdmin, $iLang, $aLangFiles);
1006  }
1007 
1008  stopProfile("_getLangTranslationArray");
1009 
1010  // if language array exists ..
1011  return (isset($this->_aLangCache[$sCacheName][$iLang]) ? $this->_aLangCache[$sCacheName][$iLang] : array());
1012  }
1013 
1022  protected function _sortLanguagesCallback($a1, $a2)
1023  {
1024  return ($a1->sort > $a2->sort);
1025  }
1026 
1032  public function getName()
1033  {
1034  return $this->_sName;
1035  }
1036 
1042  public function getFormLang()
1043  {
1044  $sLang = null;
1045  if (!$this->isAdmin()) {
1046  $sLang = "<input type=\"hidden\" name=\"" . $this->getName() . "\" value=\"" . $this->getBaseLanguage() . "\" />";
1047  }
1048 
1049  return $sLang;
1050  }
1051 
1059  public function getUrlLang($iLang = null)
1060  {
1061  $sLang = null;
1062  if (!$this->isAdmin()) {
1063  $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
1064  $sLang = $this->getName() . "=" . $iLang;
1065  }
1066 
1067  return $sLang;
1068  }
1069 
1082  public function processUrl($sUrl, $iLang = null)
1083  {
1084  $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
1085  $iDefaultLang = intval(oxRegistry::getConfig()->getConfigParam('sDefaultLang'));
1086  $iBrowserLanguage = intval($this->detectLanguageByBrowser());
1088  $oStr = getStr();
1089 
1090  if (!$this->isAdmin()) {
1091  $sParam = $this->getUrlLang($iLang);
1092  if (!$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) &&
1093  ($iLang != $iDefaultLang || $iDefaultLang != $iBrowserLanguage)
1094  ) {
1095  if ($sUrl) {
1096  if ($oStr->strpos($sUrl, '?') === false) {
1097  $sUrl .= "?";
1098  } elseif (!$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl)) {
1099  $sUrl .= "&amp;";
1100  }
1101  }
1102  $sUrl .= $sParam . "&amp;";
1103  } else {
1104  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1' . $sParam, $sUrl);
1105  }
1106  }
1107 
1108  return $sUrl;
1109  }
1110 
1117  public function detectLanguageByBrowser()
1118  {
1119  $sBrowserLanguage = $this->_getBrowserLanguage();
1120 
1121  if (!is_null($sBrowserLanguage)) {
1122 
1123  $aLanguages = $this->getLanguageArray(null, true);
1124  foreach ($aLanguages as $oLang) {
1125  if ($oLang->abbr == $sBrowserLanguage) {
1126  return $oLang->id;
1127  }
1128  }
1129  }
1130  }
1131 
1137  public function getMultiLangTables()
1138  {
1139  $aTables = array("oxarticles", "oxartextends", "oxattribute",
1140  "oxcategories", "oxcontents", "oxcountry",
1141  "oxdelivery", "oxdiscount", "oxgroups", "oxlinks",
1142  // @deprecated since v.5.3.0 (2016-06-17); The Admin Menu: Customer Info -> News feature will be moved to a module in v6.0.0
1143  "oxnews",
1144  // END deprecated
1145  "oxobject2attribute",
1146  "oxpayments", "oxselectlist", "oxshops",
1147  "oxactions", "oxwrapping", "oxdeliveryset",
1148  "oxvendor", "oxmanufacturers", "oxmediaurls",
1149  "oxstates");
1150 
1151 
1152  $aMultiLangTables = $this->getConfig()->getConfigParam('aMultiLangTables');
1153 
1154  if (is_array($aMultiLangTables)) {
1155  $aTables = array_merge($aTables, $aMultiLangTables);
1156  }
1157 
1158  return $aTables;
1159  }
1160 
1168  public function getSeoReplaceChars($iLang)
1169  {
1170  // get language replace chars
1171  $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
1172  if (!is_array($aSeoReplaceChars)) {
1173  $aSeoReplaceChars = array();
1174  }
1175 
1176  return $aSeoReplaceChars;
1177  }
1178 
1184  protected function _getActiveModuleInfo()
1185  {
1186  if ($this->_aActiveModuleInfo === null) {
1187  $oModuleList = oxNew('oxModuleList');
1188  $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
1189  }
1190 
1192  }
1193 
1199  protected function _getDisabledModuleInfo()
1200  {
1201  if ($this->_aDisabledModuleInfo === null) {
1202  $oModuleList = oxNew('oxModuleList');
1203  $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
1204  }
1205 
1207  }
1208 
1214  protected function _getBrowserLanguage()
1215  {
1216  $sBrowserLang = null;
1217  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
1218  $sBrowserLang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
1219  }
1220 
1221  return $sBrowserLang;
1222  }
1223 
1229  public function getAllShopLanguageIds()
1230  {
1232 
1233  return $aLanguages;
1234  }
1235 
1243  public function getLanguageIds($iShopId = null)
1244  {
1245  if (empty($iShopId) || $iShopId == $this->getConfig()->getShopId()) {
1247  } else {
1248  $aLanguages = $this->_getLanguageIdsFromDatabase($iShopId);
1249  }
1250 
1251  return $aLanguages;
1252  }
1253 
1259  public function getActiveShopLanguageIds()
1260  {
1261  $oConfig = $this->getConfig();
1262 
1263  //if exists language parameters array, extract lang id's from there
1264  $aLangParams = $oConfig->getConfigParam('aLanguageParams');
1265  if (is_array($aLangParams)) {
1266  $aIds = $this->_getLanguageIdsFromLanguageParamsArray($aLangParams);
1267  } else {
1268  $aIds = $this->_getLanguageIdsFromLanguagesArray($oConfig->getConfigParam('aLanguages'));
1269  }
1270 
1271  return $aIds;
1272  }
1273 
1281  protected function _getLanguageIdsFromDatabase($iShopId = null)
1282  {
1283  $aLanguages = $this->getLanguageIds();
1284 
1285 
1286  return $aLanguages;
1287  }
1288 
1297  protected function _getConfigLanguageValues($sLanguageParameterName, $iShopId = null)
1298  {
1299  $aConfigDecodedValues = array();
1300  $aConfigValues = $this->_selectLanguageParamValues($sLanguageParameterName, $iShopId);
1301 
1302  foreach ($aConfigValues as $sConfigValue) {
1303  $aConfigLanguages = unserialize($sConfigValue['oxvarvalue']);
1304 
1305  $aLanguages = array();
1306  if ($sLanguageParameterName == 'aLanguageParams') {
1307  $aLanguages = $this->_getLanguageIdsFromLanguageParamsArray($aConfigLanguages);
1308  } elseif ($sLanguageParameterName == 'aLanguages') {
1309  $aLanguages = $this->_getLanguageIdsFromLanguagesArray($aConfigLanguages);
1310  }
1311 
1312  $aConfigDecodedValues = array_unique(array_merge($aConfigDecodedValues, $aLanguages));
1313  }
1314 
1315  return $aConfigDecodedValues;
1316  }
1317 
1326  protected function _selectLanguageParamValues($sParamName, $sShopId = null)
1327  {
1329  $oConfig = oxRegistry::getConfig();
1330 
1331  $sQuery = "
1332  select " . $oConfig->getDecodeValueQuery() . " as oxvarvalue
1333  from oxconfig
1334  where oxvarname = '{$sParamName}' ";
1335 
1336  if (!empty($sShopId)) {
1337  $sQuery .= " and oxshopid = '{$sShopId}' limit 1";
1338  }
1339 
1340  return $oDb->getArray($sQuery);
1341  }
1342 
1343 
1351  protected function _getLanguageIdsFromLanguageParamsArray($aLanguageParams)
1352  {
1353  $aLanguages = array();
1354  foreach ($aLanguageParams as $sAbbr => $aValue) {
1355  $iBaseId = (int) $aValue['baseId'];
1356  $aLanguages[$iBaseId] = $sAbbr;
1357  }
1358 
1359  return $aLanguages;
1360  }
1361 
1370  {
1371  return array_keys($aLanguages);
1372  }
1373 }