oxlang.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxLang extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     protected $_sName = 'lang';
00021 
00027     protected $_iBaseLanguageId = null;
00028 
00034     protected $_iTplLanguageId = null;
00035 
00041     protected $_iEditLanguageId = null;
00042 
00048     protected $_aLangCache = array();
00049 
00055     protected $_aAdminTplLanguageArray = null;
00056 
00062     protected $_aLangAbbr = null;
00063 
00069     protected $_aAdditionalLangFiles = array();
00070 
00076     protected $_aLangMap = array();
00077 
00083     protected $_aActiveModuleInfo = null;
00084 
00090     protected $_aDisabledModuleInfo = null;
00091 
00099     public static function getInstance()
00100     {
00101         return oxRegistry::getLang();
00102     }
00103 
00110     public function resetBaseLanguage()
00111     {
00112         $this->_iBaseLanguageId = null;
00113     }
00114 
00120     public function getBaseLanguage()
00121     {
00122         if ( $this->_iBaseLanguageId === null ) {
00123 
00124             $myConfig = $this->getConfig();
00125             $blAdmin = $this->isAdmin();
00126 
00127             // languages and search engines
00128             if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00129                 $this->_iBaseLanguageId = $iSeLang;
00130             }
00131 
00132             if ( is_null( $this->_iBaseLanguageId ) ) {
00133                 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00134             }
00135 
00136             //or determining by domain
00137             $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00138 
00139             if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00140                 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00141                     if ( $sUrl && $myConfig->isCurrentUrl( $sUrl ) ) {
00142                         $this->_iBaseLanguageId = $iId;
00143                         break;
00144                     }
00145                 }
00146             }
00147 
00148             if ( is_null( $this->_iBaseLanguageId ) ) {
00149                 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00150                 if (!isset($this->_iBaseLanguageId)) {
00151                     $this->_iBaseLanguageId = oxSession::getVar('language');
00152                 }
00153             }
00154 
00155             // if language still not setted and not search engine browsing,
00156             // getting language from browser
00157             if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxRegistry::getUtils()->isSearchEngine() ) {
00158 
00159                 // getting from cookie
00160                 $this->_iBaseLanguageId = oxRegistry::get("oxUtilsServer")->getOxCookie( 'language' );
00161 
00162                 // getting from browser
00163                 if ( is_null( $this->_iBaseLanguageId ) ) {
00164                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00165                 }
00166             }
00167 
00168             if ( is_null( $this->_iBaseLanguageId ) ) {
00169                 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00170             }
00171 
00172             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00173 
00174             // validating language
00175             $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00176 
00177             oxRegistry::get("oxUtilsServer")->setOxCookie( 'language', $this->_iBaseLanguageId );
00178         }
00179 
00180         return $this->_iBaseLanguageId;
00181     }
00182 
00188     public function getObjectTplLanguage()
00189     {
00190         if ( $this->_iObjectTplLanguageId === null ) {
00191             $this->_iObjectTplLanguageId = $this->getTplLanguage();
00192             $aLanguages = $this->getAdminTplLanguageArray();
00193             if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00194                  $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00195                 $this->_iObjectTplLanguageId = key( $aLanguages );
00196             }
00197         }
00198         return $this->_iObjectTplLanguageId;
00199     }
00200 
00208     public function getTplLanguage()
00209     {
00210         if ( $this->_iTplLanguageId === null ) {
00211             $iSessLang = oxSession::getVar( 'tpllanguage' );
00212             $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00213         }
00214         return $this->_iTplLanguageId;
00215     }
00216 
00222     public function getEditLanguage()
00223     {
00224         if ( $this->_iEditLanguageId === null ) {
00225 
00226             if ( !$this->isAdmin() ) {
00227                 $this->_iEditLanguageId = $this->getBaseLanguage();
00228             } else {
00229 
00230                 $iLang = null;
00231                 // choosing language ident
00232                 // check if we really need to set the new language
00233                 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00234                     $iLang = oxConfig::getParameter( "new_lang");
00235                 }
00236                 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00237                 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00238                 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00239 
00240                 // validating language
00241                 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00242 
00243                 // writing to session
00244                 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00245             }
00246         }
00247         return $this->_iEditLanguageId;
00248     }
00249 
00259     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00260     {
00261         $myConfig = $this->getConfig();
00262 
00263         if ( is_null($iLanguage) ) {
00264             $iLanguage = $this->_iBaseLanguageId;
00265         }
00266 
00267         $aLanguages = array();
00268         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00269         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00270 
00271         if ( is_array( $aConfLanguages ) ) {
00272             $i = 0;
00273             reset( $aConfLanguages );
00274             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00275 
00276                 if ( $blOnlyActive && is_array($aLangParams) ) {
00277                     //skipping non active languages
00278                     if ( !$aLangParams[$key]['active'] ) {
00279                         $i++;
00280                         continue;
00281                     }
00282                 }
00283 
00284                 if ( $val ) {
00285                     $oLang = new stdClass();
00286                     $oLang->id   = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00287                     $oLang->oxid = $key;
00288                     $oLang->abbr = $key;
00289                     $oLang->name = $val;
00290 
00291                     if ( is_array( $aLangParams ) ) {
00292                         $oLang->active = $aLangParams[$key]['active'];
00293                         $oLang->sort   = $aLangParams[$key]['sort'];
00294                     }
00295 
00296                     $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00297                     $aLanguages[$oLang->id] = $oLang;
00298                 }
00299                 ++$i;
00300             }
00301         }
00302 
00303         if ( $blSort && is_array($aLangParams) ) {
00304             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00305         }
00306         return $aLanguages;
00307     }
00308 
00314     public function getAdminTplLanguageArray()
00315     {
00316         if ( $this->_aAdminTplLanguageArray === null ) {
00317             $myConfig = $this->getConfig();
00318 
00319             $aLangArray  = $this->getLanguageArray();
00320             $this->_aAdminTplLanguageArray = array();
00321 
00322             $sSourceDir = $myConfig->getAppDir() . 'views/admin/';
00323             foreach ( $aLangArray as $iLangKey => $oLang ) {
00324                 $sFilePath = "{$sSourceDir}{$oLang->abbr}/lang.php";
00325                 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00326                     $this->_aAdminTplLanguageArray[$iLangKey] = $oLang;
00327                 }
00328             }
00329         }
00330 
00331         // moving pointer to beginning
00332         reset( $this->_aAdminTplLanguageArray );
00333         return $this->_aAdminTplLanguageArray;
00334     }
00335 
00343     public function getLanguageAbbr( $iLanguage = null )
00344     {
00345         if ( $this->_aLangAbbr === null ) {
00346             $this->_aLangAbbr = $this->getLanguageIds();
00347         }
00348 
00349         $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00350         if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00351             $iLanguage = $this->_aLangAbbr[$iLanguage];
00352         }
00353 
00354         return $iLanguage;
00355     }
00356 
00363     public function getLanguageNames()
00364     {
00365         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00366         $aLangIds = $this->getLanguageIds();
00367         $aLanguages = array();
00368         foreach ( $aLangIds as $iId => $sValue ) {
00369             $aLanguages[$iId] = $aConfLanguages[$sValue];
00370         }
00371         return $aLanguages;
00372     }
00373 
00379     public function getLanguageIds()
00380     {
00381         $myConfig = $this->getConfig();
00382         $aIds = array();
00383 
00384         //if exists language parameters array, extract lang id's from there
00385         $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00386         if ( is_array( $aLangParams ) ) {
00387             foreach ( $aLangParams as $sAbbr => $aValue ) {
00388                 $iBaseId = (int) $aValue['baseId'];
00389                 $aIds[$iBaseId] = $sAbbr;
00390             }
00391         } else {
00392             $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00393         }
00394 
00395         return $aIds;
00396     }
00397 
00410     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00411     {
00412         // checking if in cash exist
00413         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00414         if ( isset( $aLang[$sStringToTranslate] ) ) {
00415             return $aLang[$sStringToTranslate];
00416         }
00417 
00418         // checking if in map exist
00419         $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
00420         if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00421             return $aLang[$aMap[$sStringToTranslate]];
00422         }
00423 
00424         // checking if in theme options exist
00425         if (count($this->_aAdditionalLangFiles)) {
00426             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00427             if (isset( $aLang[$sStringToTranslate] )) {
00428                 return $aLang[$sStringToTranslate];
00429             }
00430         }
00431 
00432         return $sStringToTranslate;
00433     }
00434 
00445     protected function _collectSimilar( $aData, $sKey, $aCollection = array() )
00446     {
00447         foreach ( $aData as $sValKey => $sValue ) {
00448             if ( strpos( $sValKey, $sKey ) === 0 ) {
00449                 $aCollection[$sValKey] = $sValue;
00450             }
00451         }
00452 
00453         return $aCollection;
00454     }
00455 
00466     public function getSimilarByKey( $sKey, $iLang = null, $blAdmin = null )
00467     {
00468         startProfile("getSimilarByKey");
00469 
00470         $iLang   = isset( $iLang ) ? $iLang : $this->getTplLanguage();
00471         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00472 
00473         // checking if exists in cache
00474         $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin );
00475         $aSimilarConst = $this->_collectSimilar( $aLang, $sKey );
00476 
00477         // checking if in map exist
00478         $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
00479         $aSimilarConst = $this->_collectSimilar( $aMap, $sKey, $aSimilarConst );
00480 
00481         // checking if in theme options exist
00482         if ( count( $this->_aAdditionalLangFiles ) ) {
00483             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00484             $aSimilarConst = $this->_collectSimilar( $aLang, $sKey, $aSimilarConst );
00485         }
00486 
00487         stopProfile("getSimilarByKey");
00488 
00489         return $aSimilarConst;
00490     }
00491 
00500     public function formatCurrency( $dValue, $oActCur = null )
00501     {
00502         if ( !$oActCur ) {
00503             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00504         }
00505         $sValue = oxRegistry::getUtils()->fRound( $dValue, $oActCur );
00506         return number_format( (double)$sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00507     }
00508 
00517     public function formatVat( $dValue, $oActCur = null )
00518     {
00519         $iDecPos = 0;
00520         $sValue  = ( string ) $dValue;
00521         $oStr = getStr();
00522         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00523             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00524         }
00525 
00526         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00527         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00528         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00529     }
00530 
00538     public function getLanguageTag( $iLanguage = null)
00539     {
00540         if ( !isset( $iLanguage ) ) {
00541             $iLanguage = $this->getBaseLanguage();
00542         }
00543 
00544         $iLanguage = (int) $iLanguage;
00545 
00546         return ( ( $iLanguage )?"_$iLanguage":"" );
00547     }
00548 
00556     public function validateLanguage( $iLang = null )
00557     {
00558         $iLang = (int) $iLang;
00559 
00560         // checking if this language is valid
00561         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00562         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00563             $oLang = current( $aLanguages );
00564             if (isset($oLang->id)) {
00565                 $iLang = $oLang->id;
00566             }
00567         }
00568 
00569         return $iLang;
00570     }
00571 
00579     public function setBaseLanguage( $iLang = null )
00580     {
00581         if ( is_null($iLang) ) {
00582             $iLang = $this->getBaseLanguage();
00583         } else {
00584             $this->_iBaseLanguageId = (int) $iLang;
00585         }
00586 
00587         if ( defined( 'OXID_PHP_UNIT' ) ) {
00588             modSession::getInstance();
00589         }
00590 
00591         oxSession::setVar( 'language', $iLang );
00592     }
00593 
00601     public function setTplLanguage( $iLang = null )
00602     {
00603         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00604         if ( $this->isAdmin() ) {
00605             $aLanguages = $this->getAdminTplLanguageArray();
00606             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00607                 $this->_iTplLanguageId = key( $aLanguages );
00608             }
00609         }
00610 
00611         if ( defined( 'OXID_PHP_UNIT' ) ) {
00612             modSession::getInstance();
00613         }
00614 
00615         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00616         return $this->_iTplLanguageId;
00617     }
00618 
00628     protected function _recodeLangArray( $aLangArray, $sCharset, $blRecodeKeys = false )
00629     {
00630         $aLangs = array();
00631         foreach ( $aLangArray as $sKey => $sValue ) {
00632             $sItemKey = $sKey;
00633             if ($blRecodeKeys === true) {
00634                 $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
00635             }
00636 
00637             $aLangs[$sItemKey] = iconv( $sCharset, 'UTF-8', $sValue );
00638             unset($aLangArray[$sKey]);
00639         }
00640 
00641         return $aLangs;
00642     }
00643 
00651     protected function _getLangFilesPathArray( $iLang )
00652     {
00653         $oConfig = $this->getConfig();
00654         $aLangFiles = array();
00655 
00656         $sAppDir        = $oConfig->getAppDir();
00657         $sLang          = oxRegistry::getLang()->getLanguageAbbr( $iLang );
00658         $sTheme         = $oConfig->getConfigParam( "sTheme" );
00659         $sCustomTheme   = $oConfig->getConfigParam( "sCustomTheme" );
00660         $sShopId        = $oConfig->getShopId();
00661         $aModulePaths   = $this->_getActiveModuleInfo();
00662 
00663         //get generic lang files
00664         $sGenericPath = $sAppDir . 'translations/' . $sLang;
00665         if ( $sGenericPath ) {
00666             $aLangFiles[] = $sGenericPath . "/lang.php";
00667             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sGenericPath );
00668         }
00669 
00670         //get theme lang files
00671         if ( $sTheme ) {
00672             $sThemePath = $sAppDir . 'views/' . $sTheme .'/' . $sLang;
00673             $aLangFiles[] = $sThemePath . "/lang.php";
00674             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath );
00675         }
00676 
00677         //get custom theme lang files
00678         if ( $sCustomTheme ) {
00679             $sCustPath = $sAppDir . 'views/' . $sCustomTheme .'/' . $sLang;
00680             $aLangFiles[] = $sCustPath . "/lang.php";
00681             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sCustPath );
00682         }
00683 
00684 
00685         // custom theme shop languages
00686 
00687         // modules language files
00688         $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang );
00689 
00690         // custom language files
00691         $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang );
00692 
00693         return count( $aLangFiles ) ? $aLangFiles : false;
00694     }
00695 
00703     protected function _getAdminLangFilesPathArray( $iLang )
00704     {
00705         $oConfig = $this->getConfig();
00706         $aLangFiles = array();
00707 
00708         $sAppDir        = $oConfig->getAppDir();
00709         $sLang          = oxRegistry::getLang()->getLanguageAbbr( $iLang );
00710 
00711         $aModulePaths = array();
00712         $aModulePaths = array_merge( $aModulePaths, $this->_getActiveModuleInfo() );
00713         $aModulePaths = array_merge( $aModulePaths, $this->_getDisabledModuleInfo() );
00714 
00715         // admin lang files
00716         $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
00717         $aLangFiles[] = $sAdminPath . "/lang.php";
00718         $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
00719         $aLangFiles = $this->_appendLangFile( $aLangFiles, $sAdminPath );
00720 
00721         // themes options lang files
00722         $sThemePath = $sAppDir . 'views/*/' . $sLang;
00723         $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath, "options" );
00724 
00725         // module language files
00726         $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, true );
00727 
00728         // custom language files
00729         $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang, true );
00730 
00731         return count( $aLangFiles ) ? $aLangFiles : false;
00732     }
00733 
00743     protected function _appendLangFile( $aLangFiles, $sFullPath, $sFilePattern = "lang" )
00744     {
00745         $aFiles = glob( $sFullPath . "/*_{$sFilePattern}.php" );
00746         if ( is_array( $aFiles ) && count( $aFiles ) ) {
00747             foreach ( $aFiles as $sFile ) {
00748                 if ( !strpos( $sFile, 'cust_lang.php' ) ) {
00749                     $aLangFiles[] = $sFile;
00750                 }
00751             }
00752         }
00753         return $aLangFiles;
00754     }
00755 
00765     protected function _appendCustomLangFiles( $aLangFiles, $sLang, $blForAdmin = false  )
00766     {
00767         $oConfig      = $this->getConfig();
00768         $sAppDir      = $oConfig->getAppDir();
00769         $sTheme       = $oConfig->getConfigParam( "sTheme" );
00770         $sCustomTheme = $oConfig->getConfigParam( "sCustomTheme" );
00771 
00772         if ( $blForAdmin ) {
00773             $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
00774         } else {
00775             if ( $sTheme ) {
00776                 $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
00777             }
00778             if ( $sCustomTheme ) {
00779                 $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
00780             }
00781         }
00782 
00783         return $aLangFiles;
00784     }
00785 
00796     protected function _appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, $blForAdmin = false )
00797     {
00798         if ( is_array( $aModulePaths ) ) {
00799 
00800             $oConfig = $this->getConfig();
00801 
00802             foreach ( $aModulePaths as $sPath ) {
00803                 $sFullPath  = $oConfig->getModulesDir() . $sPath;
00804                 $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
00805                 $sFullPath .= $sLang;
00806                 //@deprecated since v4.7.1/5.0.1 (2012-11-20); for < 4.6 modules, since 4.7/5.0 translation files should be in modules/modulepath/views/admin/ dir
00807                 if ( !is_dir( $sFullPath ) ) {
00808                     $sFullPath = $oConfig->getModulesDir() . $sPath;
00809                     $sFullPath .= ($blForAdmin) ? '/out/admin/' : '/out/lang/';
00810                     $sFullPath .= $sLang;
00811                 }
00812                 // END deprecated
00813                 $aLangFiles = $this->_appendLangFile( $aLangFiles, $sFullPath );
00814                 //load admin modules options lang files
00815                 if ( $blForAdmin ) {
00816                     $aLangFiles[] = $sFullPath . '/module_options.php';
00817                 }
00818             }
00819         }
00820 
00821         return $aLangFiles;
00822     }
00823 
00833     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00834     {
00835         $myConfig = $this->getConfig();
00836         $sLangFilesIdent = '_default';
00837         if (is_array($aLangFiles) && $aLangFiles) {
00838             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00839         }
00840         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00841     }
00842 
00852     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00853     {
00854         $myConfig = $this->getConfig();
00855         $myUtils  = oxRegistry::getUtils();
00856 
00857         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00858         $aLangCache = $myUtils->getLangCache( $sCacheName );
00859         if ( !$aLangCache && $aLangFiles === null ) {
00860             if ( $blAdmin ) {
00861                 $aLangFiles = $this->_getAdminLangFilesPathArray( $iLang );
00862             } else {
00863                 $aLangFiles = $this->_getLangFilesPathArray( $iLang );
00864             }
00865         }
00866         if ( !$aLangCache && $aLangFiles ) {
00867             $aLangCache = array();
00868             $sBaseCharset = false;
00869             $aLang = array();
00870             $aLangSeoReplaceChars = array();
00871             foreach ( $aLangFiles as $sLangFile ) {
00872 
00873                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00874                     $aSeoReplaceChars = array();
00875                     include $sLangFile;
00876 
00877                     // including only (!) those, which has charset defined
00878                     if ( isset( $aLang['charset'] ) ) {
00879 
00880                         // recoding only in utf
00881                         if ( $myConfig->isUtf() ) {
00882                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00883 
00884                             if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00885                                 $aSeoReplaceChars = $this->_recodeLangArray( $aSeoReplaceChars, $aLang['charset'], true );
00886                             }
00887 
00888                             // overriding charset
00889                             $aLang['charset'] = 'UTF-8';
00890                         }
00891 
00892                         if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00893                             $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
00894                         }
00895 
00896                         if ( !$sBaseCharset ) {
00897                             $sBaseCharset = $aLang['charset'];
00898                         }
00899 
00900                         $aLangCache = array_merge( $aLangCache, $aLang );
00901                     }
00902                 }
00903             }
00904 
00905             // setting base charset
00906             if ( $sBaseCharset ) {
00907                 $aLangCache['charset'] = $sBaseCharset;
00908             }
00909 
00910             // special character replacement list
00911             $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
00912 
00913             //save to cache
00914             $myUtils->setLangCache( $sCacheName, $aLangCache );
00915         }
00916 
00917         return $aLangCache;
00918     }
00919 
00928     protected function _getLanguageMap( $iLang, $blAdmin = null )
00929     {
00930         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00931         $sKey = $iLang . ( (int) $blAdmin );
00932         if ( !isset( $this->_aLangMap[$sKey] ) ) {
00933             $this->_aLangMap[$sKey] = array();
00934             $myConfig = $this->getConfig();
00935 
00936             $sMapFile = '';
00937             $sParentMapFile = $myConfig->getAppDir() . '/views/' .  ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
00938             $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' .  ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sCustomTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
00939 
00940             if ( file_exists( $sCustomThemeMapFile ) && is_readable( $sCustomThemeMapFile ) ) {
00941                 $sMapFile = $sCustomThemeMapFile;
00942             } elseif ( file_exists( $sParentMapFile ) && is_readable( $sParentMapFile ) ) {
00943                 $sMapFile = $sParentMapFile;
00944             }
00945 
00946             if ( $sMapFile ) {
00947                 include $sMapFile;
00948                 $this->_aLangMap[$sKey] = $aMap;
00949             }
00950 
00951         }
00952 
00953         return $this->_aLangMap[$sKey];
00954     }
00955 
00964     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00965     {
00966         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00967         if ( !isset( $iLang ) ) {
00968             $iLang = $this->getBaseLanguage();
00969             if ( !isset( $iLang ) ) {
00970                 $iLang = 0;
00971             }
00972         }
00973 
00974         return (int) $iLang;
00975     }
00976 
00986     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00987     {
00988         startProfile("_getLangTranslationArray");
00989 
00990         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00991         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00992         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00993 
00994         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00995             $this->_aLangCache[$sCacheName] = array();
00996         }
00997         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00998             // loading main lang files data
00999             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
01000         }
01001 
01002         stopProfile("_getLangTranslationArray");
01003 
01004         // if language array exists ..
01005         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
01006     }
01007 
01016     protected function _sortLanguagesCallback( $a1, $a2 )
01017     {
01018         return ($a1->sort > $a2->sort);
01019     }
01020 
01026     public function getName()
01027     {
01028         return $this->_sName;
01029     }
01030 
01036     public function getFormLang()
01037     {
01038         $sLang = null;
01039         if ( !$this->isAdmin()) {
01040             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\" />";
01041         }
01042         return $sLang;
01043     }
01044 
01052     public function getUrlLang( $iLang = null )
01053     {
01054         $sLang = null;
01055         if ( !$this->isAdmin()) {
01056             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01057             $sLang = $this->getName()."=". $iLang;
01058         }
01059         return $sLang;
01060     }
01061 
01074     public function processUrl( $sUrl, $iLang = null )
01075     {
01076         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01077         $oStr = getStr();
01078 
01079         if ( !$this->isAdmin() ) {
01080             $sParam = $this->getUrlLang( $iLang );
01081             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxRegistry::getConfig()->getConfigParam( 'sDefaultLang' ))) {
01082                 if ( $sUrl ) {
01083                     if ($oStr->strpos( $sUrl, '?') === false) {
01084                         $sUrl .= "?";
01085                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
01086                         $sUrl .= "&amp;";
01087                     }
01088                 }
01089                 $sUrl .= $sParam."&amp;";
01090             } else {
01091                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01092             }
01093         }
01094 
01095         return $sUrl;
01096     }
01097 
01104     public function detectLanguageByBrowser()
01105     {
01106         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01107 
01108         if ( !$sBrowserLang ) {
01109             return;
01110         }
01111 
01112         $aLangs = $this->getLanguageArray(null, true );
01113 
01114         foreach ( $aLangs as $oLang ) {
01115             if ( $oLang->abbr == $sBrowserLang ) {
01116                 return (int) $oLang->id;
01117             }
01118         }
01119     }
01120 
01126     public function getMultiLangTables()
01127     {
01128         $aTables = array( "oxarticles", "oxartextends", "oxattribute",
01129                           "oxcategories", "oxcontents", "oxcountry",
01130                           "oxdelivery", "oxdiscount", "oxgroups",
01131                           "oxlinks", "oxnews", "oxobject2attribute",
01132                           "oxpayments", "oxselectlist", "oxshops",
01133                           "oxactions", "oxwrapping", "oxdeliveryset",
01134                           "oxvendor", "oxmanufacturers", "oxmediaurls",
01135                           "oxstates" );
01136 
01137 
01138         $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
01139 
01140         if ( is_array( $aMultiLangTables ) ) {
01141             $aTables = array_merge($aTables, $aMultiLangTables);
01142         }
01143 
01144         return $aTables;
01145     }
01146 
01154     public function getSeoReplaceChars($iLang)
01155     {
01156         // get language replace chars
01157         $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
01158         if (!is_array($aSeoReplaceChars)) {
01159             $aSeoReplaceChars = array();
01160         }
01161 
01162         return $aSeoReplaceChars;
01163     }
01164 
01170     protected function _getActiveModuleInfo()
01171     {
01172         if ($this->_aActiveModuleInfo === null) {
01173             $oModuleList = oxNew('oxModuleList');
01174             $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
01175         }
01176         return $this->_aActiveModuleInfo;
01177     }
01178 
01184     protected function _getDisabledModuleInfo()
01185     {
01186         if ($this->_aDisabledModuleInfo === null) {
01187             $oModuleList = oxNew('oxModuleList');
01188             $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
01189         }
01190         return $this->_aDisabledModuleInfo;
01191     }
01192 
01193 }