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     public static function getInstance()
00091     {
00092         if ( defined('OXID_PHP_UNIT')) {
00093             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00094                 return $oClassMod;
00095             } else {
00096                 $inst = oxNew( 'oxLang' );
00097                  modInstances::addMod( __CLASS__, $inst );
00098                  return $inst;
00099             }
00100         }
00101 
00102         if ( !self::$_instance instanceof oxLang ) {
00103 
00104             self::$_instance = oxNew( 'oxLang');
00105         }
00106         return self::$_instance;
00107     }
00108 
00115     public function resetBaseLanguage()
00116     {
00117         $this->_iBaseLanguageId = null;
00118     }
00119 
00125     public function getBaseLanguage()
00126     {
00127         if ( $this->_iBaseLanguageId === null ) {
00128 
00129             $myConfig = $this->getConfig();
00130             $blAdmin = $this->isAdmin();
00131 
00132             // languages and search engines
00133             if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00134                 $this->_iBaseLanguageId = $iSeLang;
00135             }
00136 
00137             if ( is_null( $this->_iBaseLanguageId ) ) {
00138                 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00139             }
00140 
00141             //or determining by domain
00142             $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00143 
00144             if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00145                 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00146                     if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00147                         $this->_iBaseLanguageId = $iId;
00148                         break;
00149                     }
00150                 }
00151             }
00152 
00153             if ( is_null( $this->_iBaseLanguageId ) ) {
00154                 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00155                 if (!isset($this->_iBaseLanguageId)) {
00156                     $this->_iBaseLanguageId = oxSession::getVar('language');
00157                 }
00158             }
00159 
00160             // if language still not setted and not search engine browsing,
00161             // getting language from browser
00162             if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxUtils::getInstance()->isSearchEngine() ) {
00163 
00164                 // getting from cookie
00165                 $this->_iBaseLanguageId = oxUtilsServer::getInstance()->getOxCookie( 'language' );
00166 
00167                 // getting from browser
00168                 if ( is_null( $this->_iBaseLanguageId ) ) {
00169                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00170                 }
00171             }
00172 
00173             if ( is_null( $this->_iBaseLanguageId ) ) {
00174                 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00175             }
00176 
00177             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00178 
00179             // validating language
00180             $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00181 
00182             // setting language to cookie
00183             oxUtilsServer::getInstance()->setOxCookie( 'language', $this->_iBaseLanguageId );
00184         }
00185 
00186         return $this->_iBaseLanguageId;
00187     }
00188 
00194     public function getObjectTplLanguage()
00195     {
00196         if ( $this->_iObjectTplLanguageId === null ) {
00197             $this->_iObjectTplLanguageId = $this->getTplLanguage();
00198             $aLanguages = $this->getAdminTplLanguageArray();
00199             if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00200                  $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00201                 $this->_iObjectTplLanguageId = key( $aLanguages );
00202             }
00203         }
00204         return $this->_iObjectTplLanguageId;
00205     }
00206 
00214     public function getTplLanguage()
00215     {
00216         if ( $this->_iTplLanguageId === null ) {
00217             $iSessLang = oxSession::getVar( 'tpllanguage' );
00218             $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00219         }
00220         return $this->_iTplLanguageId;
00221     }
00222 
00228     public function getEditLanguage()
00229     {
00230         if ( $this->_iEditLanguageId === null ) {
00231 
00232             if ( !$this->isAdmin() ) {
00233                 $this->_iEditLanguageId = $this->getBaseLanguage();
00234             } else {
00235 
00236                 $iLang = null;
00237                 // choosing language ident
00238                 // check if we really need to set the new language
00239                 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00240                     $iLang = oxConfig::getParameter( "new_lang");
00241                 }
00242                 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00243                 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00244                 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00245 
00246                 // validating language
00247                 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00248 
00249                 // writing to session
00250                 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00251             }
00252         }
00253         return $this->_iEditLanguageId;
00254     }
00255 
00265     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00266     {
00267         $myConfig = $this->getConfig();
00268 
00269         if ( is_null($iLanguage) ) {
00270             $iLanguage = $this->_iBaseLanguageId;
00271         }
00272 
00273         $aLanguages = array();
00274         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00275         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00276 
00277         if ( is_array( $aConfLanguages ) ) {
00278             $i = 0;
00279             reset( $aConfLanguages );
00280             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00281 
00282                 if ( $blOnlyActive && is_array($aLangParams) ) {
00283                     //skipping non active languages
00284                     if ( !$aLangParams[$key]['active'] ) {
00285                         $i++;
00286                         continue;
00287                     }
00288                 }
00289 
00290                 if ( $val ) {
00291                     $oLang = new oxStdClass();
00292                     $oLang->id   = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00293                     $oLang->oxid = $key;
00294                     $oLang->abbr = $key;
00295                     $oLang->name = $val;
00296 
00297                     if ( is_array( $aLangParams ) ) {
00298                         $oLang->active = $aLangParams[$key]['active'];
00299                         $oLang->sort   = $aLangParams[$key]['sort'];
00300                     }
00301 
00302                     $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00303                     $aLanguages[$oLang->id] = $oLang;
00304                 }
00305                 ++$i;
00306             }
00307         }
00308 
00309         if ( $blSort && is_array($aLangParams) ) {
00310             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00311         }
00312 
00313 
00314         return $aLanguages;
00315     }
00316 
00322     public function getAdminTplLanguageArray()
00323     {
00324         if ( $this->_aAdminTplLanguageArray === null ) {
00325 
00326             // #656 add admin languages
00327             $aLangData = array();
00328             $aLangIds  = $this->getLanguageIds();
00329 
00330             $sSourceDir = $this->getConfig()->getStdLanguagePath( "", true, false );
00331             foreach ( glob( $sSourceDir."*", GLOB_ONLYDIR ) as $sDir ) {
00332                 $sFilePath = "{$sDir}/lang.php";
00333                 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00334                     $sLangName = "";
00335                     $sAbbr = strtolower( basename( $sDir ) );
00336                     if ( !in_array( $sAbbr, $aLangIds ) ) {
00337                         include $sFilePath;
00338                         $aLangData[$sAbbr] = new oxStdClass();
00339                         $aLangData[$sAbbr]->name = $sLangName;
00340                         $aLangData[$sAbbr]->abbr = $sAbbr;
00341                     }
00342                 }
00343             }
00344 
00345             $this->_aAdminTplLanguageArray = $this->getLanguageArray();
00346             if ( count( $aLangData ) ) {
00347 
00348                 // sorting languages for selection list view
00349                 ksort( $aLangData );
00350                 $iSort = max( array_keys( $this->_aAdminTplLanguageArray ) );
00351 
00352                 // appending other languages
00353                 foreach ( $aLangData as $oLang ) {
00354                     $oLang->id = $oLang->sort = ++$iSort;
00355                     $oLang->selected = 0;
00356                     $oLang->active   = 0;
00357                     $this->_aAdminTplLanguageArray[$iSort] = $oLang;
00358                 }
00359             }
00360         }
00361 
00362         // moving pointer to beginning
00363         reset( $this->_aAdminTplLanguageArray );
00364         return $this->_aAdminTplLanguageArray;
00365     }
00366 
00374     public function getLanguageAbbr( $iLanguage = null )
00375     {
00376         if ( $this->_aLangAbbr === null ) {
00377             $this->_aLangAbbr = array();
00378             if ( $this->isAdmin() ) {
00379                 foreach ( $this->getAdminTplLanguageArray() as $oLang ) {
00380                     $this->_aLangAbbr[$oLang->id] = $oLang->abbr;
00381                 }
00382             } else {
00383                 $this->_aLangAbbr = $this->getLanguageIds();
00384             }
00385         }
00386 
00387         $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00388         if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00389             $iLanguage = $this->_aLangAbbr[$iLanguage];
00390         }
00391 
00392         return $iLanguage;
00393     }
00394 
00401     public function getLanguageNames()
00402     {
00403         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00404         $aLangIds = $this->getLanguageIds();
00405         $aLanguages = array();
00406         foreach ( $aLangIds as $iId => $sValue ) {
00407             $aLanguages[$iId] = $aConfLanguages[$sValue];
00408         }
00409         return $aLanguages;
00410     }
00411 
00417     public function getLanguageIds()
00418     {
00419         $myConfig = $this->getConfig();
00420         $aIds = array();
00421 
00422         //if exists language parameters array, extract lang id's from there
00423         $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00424         if ( is_array( $aLangParams ) ) {
00425             foreach ( $aLangParams as $sAbbr => $aValue ) {
00426                 $iBaseId = (int) $aValue['baseId'];
00427                 $aIds[$iBaseId] = $sAbbr;
00428             }
00429         } else {
00430             $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00431         }
00432 
00433         return $aIds;
00434     }
00435 
00444     public function registerAdditionalLangFile($sFile)
00445     {
00446         if (!$sFile || !is_readable($sFile)) {
00447             $oErr = oxNew( "oxFileException", 'EXCEPTION_FILENOTFOUND');
00448             $oErr->setFileName($sFile);
00449             throw $oErr;
00450         }
00451         $this->_aAdditionalLangFiles[] = $sFile;
00452     }
00453 
00466     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00467     {
00468         // checking if in cash exist
00469         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00470         if ( isset( $aLang[$sStringToTranslate] ) ) {
00471             return $aLang[$sStringToTranslate];
00472         }
00473 
00474         // checking if in map exist
00475         $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
00476         if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00477             return $aLang[$aMap[$sStringToTranslate]];
00478         }
00479 
00480         // checking if in theme options exist
00481         if (count($this->_aAdditionalLangFiles)) {
00482             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00483             if (isset( $aLang[$sStringToTranslate] )) {
00484                 return $aLang[$sStringToTranslate];
00485             }
00486         }
00487 
00488         // @deprecated, text files should not be used any more (2011.07.06)
00489             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00490             if ( !$blIsAdmin ) {
00491                 return $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00492             }
00493 
00494         return $sStringToTranslate;
00495     }
00496 
00507     protected function _collectSimilar( $aData, $sKey, $aCollection = array() )
00508     {
00509         foreach ( $aData as $sValKey => $sValue ) {
00510             if ( strpos( $sValKey, $sKey ) === 0 ) {
00511                 $aCollection[$sValKey] = $sValue;
00512             }
00513         }
00514 
00515         return $aCollection;
00516     }
00517 
00528     public function getSimilarByKey( $sKey, $iLang = null, $blAdmin = null )
00529     {
00530         startProfile("getSimilarByKey");
00531 
00532         $iLang   = isset( $iLang ) ? $iLang : $this->getTplLanguage();
00533         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00534 
00535         // checking if in cash exist
00536         $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin );
00537         $aSimilarConst = $this->_collectSimilar( $aLang, $sKey );
00538 
00539         // checking if in map exist
00540         $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
00541         $aSimilarConst = $this->_collectSimilar( $aMap, $sKey, $aSimilarConst );
00542 
00543         // checking if in theme options exist
00544         if ( count( $this->_aAdditionalLangFiles ) ) {
00545             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00546             $aSimilarConst = $this->_collectSimilar( $aLang, $sKey, $aSimilarConst );
00547         }
00548 
00549         stopProfile("getSimilarByKey");
00550 
00551         return $aSimilarConst;
00552     }
00553 
00562     public function formatCurrency( $dValue, $oActCur = null )
00563     {
00564         if ( !$oActCur ) {
00565             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00566         }
00567         return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00568     }
00569 
00578     public function formatVat( $dValue, $oActCur = null )
00579     {
00580         $iDecPos = 0;
00581         $sValue  = ( string ) $dValue;
00582         $oStr = getStr();
00583         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00584             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00585         }
00586 
00587         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00588         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00589         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00590     }
00591 
00599     public function getLanguageTag( $iLanguage = null)
00600     {
00601         if ( !isset( $iLanguage ) ) {
00602             $iLanguage = $this->getBaseLanguage();
00603         }
00604 
00605         $iLanguage = (int) $iLanguage;
00606 
00607         return ( ( $iLanguage )?"_$iLanguage":"" );
00608     }
00609 
00617     public function validateLanguage( $iLang = null )
00618     {
00619         $iLang = (int) $iLang;
00620 
00621         // checking if this language is valid
00622         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00623         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00624             $oLang = current( $aLanguages );
00625             if (isset($oLang->id)) {
00626                 $iLang = $oLang->id;
00627             }
00628         }
00629 
00630         return $iLang;
00631     }
00632 
00640     public function setBaseLanguage( $iLang = null )
00641     {
00642         if ( is_null($iLang) ) {
00643             $iLang = $this->getBaseLanguage();
00644         } else {
00645             $this->_iBaseLanguageId = (int) $iLang;
00646         }
00647 
00648         if ( defined( 'OXID_PHP_UNIT' ) ) {
00649             modSession::getInstance();
00650         }
00651 
00652         oxSession::setVar( 'language', $iLang );
00653     }
00654 
00662     public function setTplLanguage( $iLang = null )
00663     {
00664         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00665         if ( $this->isAdmin() ) {
00666             $aLanguages = $this->getAdminTplLanguageArray();
00667             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00668                 $this->_iTplLanguageId = key( $aLanguages );
00669             }
00670         }
00671 
00672         if ( defined( 'OXID_PHP_UNIT' ) ) {
00673             modSession::getInstance();
00674         }
00675 
00676         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00677         return $this->_iTplLanguageId;
00678     }
00679 
00689     protected function _recodeLangArray( $aLangArray, $sCharset, $blRecodeKeys = false )
00690     {
00691         $aLangs = array();
00692         foreach ( $aLangArray as $sKey => $sValue ) {
00693             $sItemKey = $sKey;
00694             if ($blRecodeKeys === true) {
00695                 $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
00696             }
00697 
00698             $aLangs[$sItemKey] = iconv( $sCharset, 'UTF-8', $sValue );
00699             unset($aLangArray[$sKey]);
00700         }
00701 
00702         return $aLangs;
00703     }
00704 
00713     protected function _getLangFilesPathArray( $blAdmin, $iLang )
00714     {
00715         $myConfig = $this->getConfig();
00716         $aLangFiles = array();
00717 
00718         $sOutDir        = $myConfig->getOutDir();
00719         $sLang          = oxLang::getInstance()->getLanguageAbbr( $iLang );
00720         $sTheme         = $myConfig->getConfigParam( "sTheme" );
00721         $sCustomTheme   = $myConfig->getConfigParam( "sCustomTheme" );
00722         $sShopId        = $myConfig->getShopId();
00723         $aModuleInfo    = $this->_getActiveModuleInfo();
00724 
00725         if ( $blAdmin ) {
00726             // admin lang files
00727             $sAdminPath = $sOutDir . 'admin/' . $sLang . '/';
00728             $aLangFiles[] = $sAdminPath . "lang.php";
00729             $aTmpFiles = glob( $sAdminPath."*_lang.php" );
00730             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00731                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles);
00732             }
00733 
00734             // themes options lang files
00735             $sThemePath = $sOutDir . '*/' . $sLang . '/theme_options.php';
00736             $aTmpFiles = glob( $sThemePath );
00737             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00738                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles);
00739             }
00740 
00741             //load admin modules lang files
00742             if ( is_array( $aModuleInfo ) ) {
00743                 foreach ( $aModuleInfo as $sPath ) {
00744                     $aModuleFiles = glob( $myConfig->getModulesDir().$sPath.'/out/admin/' . $sLang . '/*_lang.php' );
00745                     if ( is_array( $aModuleFiles ) && count( $aModuleFiles ) ) {
00746                         $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00747                     }
00748 
00749                     //load admin modules options lang files
00750                     $aLangFiles[] = $myConfig->getModulesDir().$sPath.'/out/admin/' . $sLang . '/module_options.php';
00751                 }
00752             }
00753 
00754             return count( $aLangFiles ) ? $aLangFiles : false;
00755         }
00756 
00757         //get generic lang files
00758         $sGenericPath = $sOutDir . $sLang . '/';
00759         if ( $sGenericPath ) {
00760             $aLangFiles[] = $sGenericPath . "lang.php";
00761             $aTmpFiles = glob( $sGenericPath."*_lang.php" );
00762             if ( is_array($aTmpFiles ) && count( $aTmpFiles ) ) {
00763                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00764             }
00765         }
00766 
00767         //get theme lang files
00768         if ( $sTheme ) {
00769             $sThemePath = $sOutDir . $sTheme .'/' . $sLang . '/';
00770             $aLangFiles[] = $sThemePath . "lang.php";
00771             $aTmpFiles = glob( $sThemePath."*_lang.php" );
00772             if ( is_array( $aTmpFiles) && count( $aTmpFiles ) ) {
00773                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00774             }
00775         }
00776 
00777         //get custom theme lang files
00778         if ( $sCustomTheme ) {
00779             $sCustPath = $sOutDir . $sCustomTheme .'/' . $sLang . '/';
00780             $aLangFiles[] = $sCustPath . "lang.php";
00781             $aTmpFiles = glob( $sCustPath."*_lang.php" );
00782             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00783                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00784             }
00785 
00786             // custom theme shop languages
00787             if ( $sCustomTheme ) {
00788                 $sShopPath = $sOutDir . $sCustomTheme .'/' . $sShopId . '/' . $sLang . '/';
00789                 $aLangFiles[] = $sShopPath . "lang.php";
00790                 $aTmpFiles = glob( $sShopPath."*_lang.php" );
00791                 if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00792                     $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00793                 }
00794             }
00795         } else {
00796             // theme shop languages
00797             if ( $sTheme ) {
00798                 $sShopPath = $sOutDir . $sTheme .'/' . $sShopId . '/' . $sLang . '/';
00799                 $aLangFiles[] = $sShopPath . "lang.php";
00800                 $aTmpFiles = glob( $sShopPath."*_lang.php" );
00801                 if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00802                     $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00803                 }
00804             }
00805         }
00806 
00807         //modules language files
00808         if ( is_array( $aModuleInfo ) ) {
00809             foreach ( $aModuleInfo as $sPath ) {
00810                 $aModuleFiles = glob( $myConfig->getModulesDir() . $sPath . '/out/lang/' . $sLang . '/*_lang.php');
00811                 if (is_array($aModuleFiles) && count($aModuleFiles)) {
00812                     $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00813                 }
00814             }
00815         }
00816 
00817         return count( $aLangFiles ) ? $aLangFiles : false;
00818     }
00819 
00829     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00830     {
00831         $myConfig = $this->getConfig();
00832         $sLangFilesIdent = '_default';
00833         if (is_array($aLangFiles) && $aLangFiles) {
00834             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00835         }
00836         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00837     }
00838 
00848     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00849     {
00850         $myConfig = $this->getConfig();
00851         $myUtils  = oxUtils::getInstance();
00852 
00853         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00854         $aLangCache = $myUtils->getLangCache( $sCacheName );
00855         if ( !$aLangCache && $aLangFiles === null ) {
00856             $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang );
00857         }
00858         if ( !$aLangCache && $aLangFiles ) {
00859             $aLangCache = array();
00860             $sBaseCharset = false;
00861             $aLang = array();
00862             $aLangSeoReplaceChars = array();
00863             foreach ( $aLangFiles as $sLangFile ) {
00864 
00865                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00866                     $aSeoReplaceChars = array();
00867                     include $sLangFile;
00868 
00869                     // including only (!) thoose, which has charset defined
00870                     if ( isset( $aLang['charset'] ) ) {
00871 
00872                         // recoding only in utf
00873                         if ( $myConfig->isUtf() ) {
00874                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00875 
00876                             if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00877                                 $aSeoReplaceChars = $this->_recodeLangArray( $aSeoReplaceChars, $aLang['charset'], true );
00878                             }
00879 
00880                             // overriding charset
00881                             $aLang['charset'] = 'UTF-8';
00882                         }
00883 
00884                         if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00885                             $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
00886                         }
00887 
00888                         if ( !$sBaseCharset ) {
00889                             $sBaseCharset = $aLang['charset'];
00890                         }
00891 
00892                         $aLangCache = array_merge( $aLangCache, $aLang );
00893                     }
00894                 }
00895             }
00896 
00897             // setting base charset
00898             if ( $sBaseCharset ) {
00899                 $aLangCache['charset'] = $sBaseCharset;
00900             }
00901 
00902             // special character replacement list
00903             $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
00904 
00905             //save to cache
00906             $myUtils->setLangCache( $sCacheName, $aLangCache );
00907         }
00908 
00909         return $aLangCache;
00910     }
00911 
00920     protected function _getLanguageMap( $iLang, $blAdmin = null )
00921     {
00922         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00923         $sKey = $iLang . ( (int) $blAdmin );
00924         if ( !isset( $this->_aLangMap[$sKey] ) ) {
00925             $this->_aLangMap[$sKey] = array();
00926             $myConfig = $this->getConfig();
00927             $sMapFile = $myConfig->getOutDir() . '/' .  ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sTheme" ) ) .'/' . oxLang::getInstance()->getLanguageAbbr( $iLang ) . '/map.php';
00928             if ( $sMapFile ) {
00929                 if ( file_exists( $sMapFile ) && is_readable( $sMapFile ) ) {
00930                     include $sMapFile;
00931                     $this->_aLangMap[$sKey] = $aMap;
00932                 }
00933             }
00934         }
00935 
00936         return $this->_aLangMap[$sKey];
00937     }
00938 
00947     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00948     {
00949         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00950         if ( !isset( $iLang ) ) {
00951             $iLang = $this->getBaseLanguage();
00952             if ( !isset( $iLang ) ) {
00953                 $iLang = 0;
00954             }
00955         }
00956 
00957         return (int) $iLang;
00958     }
00959 
00969     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00970     {
00971         startProfile("_getLangTranslationArray");
00972 
00973         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00974         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00975         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00976 
00977         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00978             $this->_aLangCache[$sCacheName] = array();
00979         }
00980         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00981             // loading main lang files data
00982             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00983         }
00984 
00985         stopProfile("_getLangTranslationArray");
00986 
00987         // if language array exists ..
00988         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00989     }
00990 
01002     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
01003     {
01004         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
01005         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
01006         if ( !isset( $iLang ) ) {
01007             $iLang = (int) $this->getBaseLanguage();
01008         }
01009 
01010         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
01011         if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
01012 
01013             static $aUserLangCache = array();
01014 
01015             if ( !isset( $aUserLangCache[$sFileName] ) ) {
01016                 $handle = @fopen( $sFileName, "r" );
01017                 if ( $handle === false ) {
01018                     return $sStringToTranslate;
01019                 }
01020 
01021                 $contents = fread( $handle, filesize ( $sFileName ) );
01022                 fclose( $handle );
01023                 $fileArray = explode( "\n", $contents );
01024                 $aUserLangCache[$sFileName] = array();
01025                 $aLang = &$aUserLangCache[$sFileName];
01026                 $oStr = getStr();
01027 
01028                 while ( list( $nr,$line ) = each( $fileArray ) ) {
01029                     $line = ltrim( $line );
01030                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
01031                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
01032                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
01033                         $aLang[trim($index)] = trim($value);
01034                     }
01035                 }
01036             }
01037 
01038             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
01039                 $aLang = &$aUserLangCache[$sFileName];
01040             }
01041 
01042             if ( isset( $aLang[$sStringToTranslate] ) ) {
01043                 $sStringToTranslate = $aLang[$sStringToTranslate];
01044             }
01045         }
01046 
01047         return $sStringToTranslate;
01048     }
01049 
01058     protected function _sortLanguagesCallback( $a1, $a2 )
01059     {
01060         return ($a1->sort > $a2->sort);
01061     }
01062 
01068     public function getName()
01069     {
01070         return $this->_sName;
01071     }
01072 
01078     public function getFormLang()
01079     {
01080         $sLang = null;
01081         if ( !$this->isAdmin()) {
01082             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
01083         }
01084         return $sLang;
01085     }
01086 
01094     public function getUrlLang( $iLang = null )
01095     {
01096         $sLang = null;
01097         if ( !$this->isAdmin()) {
01098             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01099             $sLang = $this->getName()."=". $iLang;
01100         }
01101         return $sLang;
01102     }
01103 
01116     public function processUrl( $sUrl, $iLang = null )
01117     {
01118         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01119         $oStr = getStr();
01120 
01121         if ( !$this->isAdmin() ) {
01122             $sParam = $this->getUrlLang( $iLang );
01123             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
01124                 if ( $sUrl ) {
01125                     if ($oStr->strpos( $sUrl, '?') === false) {
01126                         $sUrl .= "?";
01127                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
01128                         $sUrl .= "&amp;";
01129                     }
01130                 }
01131                 $sUrl .= $sParam."&amp;";
01132             } else {
01133                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01134             }
01135         }
01136 
01137         return $sUrl;
01138     }
01139 
01146     public function detectLanguageByBrowser()
01147     {
01148         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01149 
01150         if ( !$sBrowserLang ) {
01151             return;
01152         }
01153 
01154         $aLangs = $this->getLanguageArray(null, true );
01155 
01156         foreach ( $aLangs as $oLang ) {
01157             if ( $oLang->abbr == $sBrowserLang ) {
01158                 return (int) $oLang->id;
01159             }
01160         }
01161     }
01162 
01168     public function getMultiLangTables()
01169     {
01170         $aTables = array( "oxarticles", "oxartextends", "oxattribute",
01171                           "oxcategories", "oxcontents", "oxcountry",
01172                           "oxdelivery", "oxdiscount", "oxgroups",
01173                           "oxlinks", "oxnews", "oxobject2attribute",
01174                           "oxpayments", "oxselectlist", "oxshops",
01175                           "oxactions", "oxwrapping", "oxdeliveryset",
01176                           "oxvendor", "oxmanufacturers", "oxmediaurls",
01177                           "oxstates" );
01178 
01179 
01180         $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
01181 
01182         if ( is_array( $aMultiLangTables ) ) {
01183             $aTables = array_merge($aTables, $aMultiLangTables);
01184         }
01185 
01186         return $aTables;
01187     }
01188 
01196     public function getSeoReplaceChars($iLang)
01197     {
01198         // compatibility: check for global settings
01199         $aGlobalSeoReplaceChars = $this->getConfig()->getConfigParam('aSeoReplaceChars');
01200         if (!is_array($aGlobalSeoReplaceChars)) {
01201             $aGlobalSeoReplaceChars = array();
01202         }
01203 
01204         // get language replace chars
01205         $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
01206         if (!is_array($aSeoReplaceChars)) {
01207             $aSeoReplaceChars = array();
01208         }
01209 
01210         $aSeoReplaceChars = array_merge($aGlobalSeoReplaceChars, $aSeoReplaceChars);
01211         return $aSeoReplaceChars;
01212     }
01213 
01219     protected function _getActiveModuleInfo()
01220     {
01221         if ($this->_aActiveModuleInfo === null) {
01222             $oModulelist = oxNew('oxmodulelist');
01223             $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
01224         }
01225         return $this->_aActiveModuleInfo;
01226     }
01227 
01228 }