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 
00078 
00084     public static function getInstance()
00085     {
00086         if ( defined('OXID_PHP_UNIT')) {
00087             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00088                 return $oClassMod;
00089             } else {
00090                 $inst = oxNew( 'oxLang' );
00091                  modInstances::addMod( __CLASS__, $inst );
00092                  return $inst;
00093             }
00094         }
00095 
00096         if ( !self::$_instance instanceof oxLang ) {
00097 
00098             self::$_instance = oxNew( 'oxLang');
00099         }
00100         return self::$_instance;
00101     }
00102 
00109     public function resetBaseLanguage()
00110     {
00111         $this->_iBaseLanguageId = null;
00112     }
00113 
00119     public function getBaseLanguage()
00120     {
00121         if ( $this->_iBaseLanguageId === null ) {
00122 
00123             $myConfig = $this->getConfig();
00124             $blAdmin = $this->isAdmin();
00125 
00126             // languages and search engines
00127             if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00128                 $this->_iBaseLanguageId = $iSeLang;
00129             }
00130 
00131             if ( is_null( $this->_iBaseLanguageId ) ) {
00132                 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00133             }
00134 
00135             //or determining by domain
00136             $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00137 
00138             if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00139                 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00140                     if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00141                         $this->_iBaseLanguageId = $iId;
00142                         break;
00143                     }
00144                 }
00145             }
00146 
00147             if ( is_null( $this->_iBaseLanguageId ) ) {
00148                 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00149                 if (!isset($this->_iBaseLanguageId)) {
00150                     $this->_iBaseLanguageId = oxSession::getVar('language');
00151                 }
00152             }
00153 
00154             // if language still not setted and not search engine browsing,
00155             // getting language from browser
00156             if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxUtils::getInstance()->isSearchEngine() ) {
00157 
00158                 // getting from cookie
00159                 $this->_iBaseLanguageId = oxUtilsServer::getInstance()->getOxCookie( 'language' );
00160 
00161                 // getting from browser
00162                 if ( is_null( $this->_iBaseLanguageId ) ) {
00163                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00164                 }
00165             }
00166 
00167             if ( is_null( $this->_iBaseLanguageId ) ) {
00168                 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00169             }
00170 
00171             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00172 
00173             // validating language
00174             $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00175 
00176             // setting language to cookie
00177             oxUtilsServer::getInstance()->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                 // choosing language ident
00231                 // check if we really need to set the new language
00232                 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00233                     $iLang = oxConfig::getParameter( "new_lang");
00234                 }
00235                 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00236                 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00237                 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00238 
00239                 // validating language
00240                 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00241 
00242                 // writing to session
00243                 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00244             }
00245         }
00246         return $this->_iEditLanguageId;
00247     }
00248 
00258     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00259     {
00260         $myConfig = $this->getConfig();
00261 
00262         if ( is_null($iLanguage) ) {
00263             $iLanguage = $this->_iBaseLanguageId;
00264         }
00265 
00266         $aLanguages = array();
00267         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00268         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00269 
00270         if ( is_array( $aConfLanguages ) ) {
00271             $i = 0;
00272             reset( $aConfLanguages );
00273             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00274 
00275                 if ( $blOnlyActive && is_array($aLangParams) ) {
00276                     //skipping non active languages
00277                     if ( !$aLangParams[$key]['active'] ) {
00278                         $i++;
00279                         continue;
00280                     }
00281                 }
00282 
00283                 if ( $val ) {
00284                     $oLang = new oxStdClass();
00285                     $oLang->id   = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00286                     $oLang->oxid = $key;
00287                     $oLang->abbr = $key;
00288                     $oLang->name = $val;
00289 
00290                     if ( is_array( $aLangParams ) ) {
00291                         $oLang->active = $aLangParams[$key]['active'];
00292                         $oLang->sort   = $aLangParams[$key]['sort'];
00293                     }
00294 
00295                     $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00296                     $aLanguages[$oLang->id] = $oLang;
00297                 }
00298                 ++$i;
00299             }
00300         }
00301 
00302         if ( $blSort && is_array($aLangParams) ) {
00303             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00304         }
00305 
00306 
00307         return $aLanguages;
00308     }
00309 
00315     public function getAdminTplLanguageArray()
00316     {
00317         if ( $this->_aAdminTplLanguageArray === null ) {
00318 
00319             // #656 add admin languages
00320             $aLangData = array();
00321             $aLangIds  = $this->getLanguageIds();
00322 
00323             $sSourceDir = $this->getConfig()->getStdLanguagePath( "", true, false );
00324             foreach ( glob( $sSourceDir."*", GLOB_ONLYDIR ) as $sDir ) {
00325                 $sFilePath = "{$sDir}/lang.php";
00326                 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00327                     $sLangName = "";
00328                     $sAbbr = strtolower( basename( $sDir ) );
00329                     if ( !in_array( $sAbbr, $aLangIds ) ) {
00330                         include $sFilePath;
00331                         $aLangData[$sAbbr] = new oxStdClass();
00332                         $aLangData[$sAbbr]->name = $sLangName;
00333                         $aLangData[$sAbbr]->abbr = $sAbbr;
00334                     }
00335                 }
00336             }
00337 
00338             $this->_aAdminTplLanguageArray = $this->getLanguageArray();
00339             if ( count( $aLangData ) ) {
00340 
00341                 // sorting languages for selection list view
00342                 ksort( $aLangData );
00343                 $iSort = max( array_keys( $this->_aAdminTplLanguageArray ) );
00344 
00345                 // appending other languages
00346                 foreach ( $aLangData as $oLang ) {
00347                     $oLang->id = $oLang->sort = ++$iSort;
00348                     $oLang->selected = 0;
00349                     $oLang->active   = 0;
00350                     $this->_aAdminTplLanguageArray[$iSort] = $oLang;
00351                 }
00352             }
00353         }
00354 
00355         // moving pointer to beginning
00356         reset( $this->_aAdminTplLanguageArray );
00357         return $this->_aAdminTplLanguageArray;
00358     }
00359 
00367     public function getLanguageAbbr( $iLanguage = null )
00368     {
00369         if ( $this->_aLangAbbr === null ) {
00370             $this->_aLangAbbr = array();
00371             if ( $this->isAdmin() ) {
00372                 foreach ( $this->getAdminTplLanguageArray() as $oLang ) {
00373                     $this->_aLangAbbr[$oLang->id] = $oLang->abbr;
00374                 }
00375             } else {
00376                 $this->_aLangAbbr = $this->getLanguageIds();
00377             }
00378         }
00379 
00380         $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00381         if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00382             $iLanguage = $this->_aLangAbbr[$iLanguage];
00383         }
00384 
00385         return $iLanguage;
00386     }
00387 
00394     public function getLanguageNames()
00395     {
00396         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00397         $aLangIds = $this->getLanguageIds();
00398         $aLanguages = array();
00399         foreach ( $aLangIds as $iId => $sValue ) {
00400             $aLanguages[$iId] = $aConfLanguages[$sValue];
00401         }
00402         return $aLanguages;
00403     }
00404 
00410     public function getLanguageIds()
00411     {
00412         $myConfig = $this->getConfig();
00413         $aIds = array();
00414 
00415         //if exists language parameters array, extract lang id's from there
00416         $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00417         if ( is_array( $aLangParams ) ) {
00418             foreach ( $aLangParams as $sAbbr => $aValue ) {
00419                 $iBaseId = (int) $aValue['baseId'];
00420                 $aIds[$iBaseId] = $sAbbr;
00421             }
00422         } else {
00423             $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00424         }
00425 
00426         return $aIds;
00427     }
00428 
00436     public function registerAdditionalLangFile($sFile)
00437     {
00438         if (!$sFile || !is_readable($sFile)) {
00439             $oErr = new oxFileException('EXCEPTION_FILENOTFOUND');
00440             $oErr->setFileName($sFile);
00441             throw $oErr;
00442         }
00443         $this->_aAdditionalLangFiles[] = $sFile;
00444     }
00445 
00458     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00459     {
00460         $myConfig = $this->getConfig();
00461 
00462         // checking if in cash exist
00463         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00464         if ( isset( $aLang[$sStringToTranslate] ) ) {
00465             return $aLang[$sStringToTranslate];
00466         }
00467 
00468         // checking if in map exist
00469         $aMap = $this->_getLanguageMap( $iLang );
00470         if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00471             return $aLang[$aMap[$sStringToTranslate]];
00472         }
00473 
00474         // checking if in theme options exist
00475         if (count($this->_aAdditionalLangFiles)) {
00476             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00477             if (isset( $aLang[$sStringToTranslate] )) {
00478                 return $aLang[$sStringToTranslate];
00479             }
00480         }
00481 
00482         // @deprecated, text files should not be used any more (2011.07.06)
00483             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00484             if ( !$blIsAdmin ) {
00485                 return $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00486             }
00487 
00488         return $sStringToTranslate;
00489     }
00490 
00499     public function formatCurrency( $dValue, $oActCur = null )
00500     {
00501         if ( !$oActCur ) {
00502             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00503         }
00504         return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00505     }
00506 
00515     public function formatVat( $dValue, $oActCur = null )
00516     {
00517         $iDecPos = 0;
00518         $sValue  = ( string ) $dValue;
00519         $oStr = getStr();
00520         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00521             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00522         }
00523 
00524         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00525         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00526         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00527     }
00528 
00536     public function getLanguageTag( $iLanguage = null)
00537     {
00538         if ( !isset( $iLanguage ) ) {
00539             $iLanguage = $this->getBaseLanguage();
00540         }
00541 
00542         $iLanguage = (int) $iLanguage;
00543 
00544         return ( ( $iLanguage )?"_$iLanguage":"" );
00545     }
00546 
00554     public function validateLanguage( $iLang = null )
00555     {
00556         $iLang = (int) $iLang;
00557 
00558         // checking if this language is valid
00559         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00560         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00561             $oLang = current( $aLanguages );
00562             if (isset($oLang->id)) {
00563                 $iLang = $oLang->id;
00564             }
00565         }
00566 
00567         return $iLang;
00568     }
00569 
00577     public function setBaseLanguage( $iLang = null )
00578     {
00579         if ( is_null($iLang) ) {
00580             $iLang = $this->getBaseLanguage();
00581         } else {
00582             $this->_iBaseLanguageId = (int) $iLang;
00583         }
00584 
00585         if ( defined( 'OXID_PHP_UNIT' ) ) {
00586             modSession::getInstance();
00587         }
00588 
00589         oxSession::setVar( 'language', $iLang );
00590     }
00591 
00599     public function setTplLanguage( $iLang = null )
00600     {
00601         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00602         if ( $this->isAdmin() ) {
00603             $aLanguages = $this->getAdminTplLanguageArray();
00604             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00605                 $this->_iTplLanguageId = key( $aLanguages );
00606             }
00607         }
00608 
00609         if ( defined( 'OXID_PHP_UNIT' ) ) {
00610             modSession::getInstance();
00611         }
00612 
00613         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00614         return $this->_iTplLanguageId;
00615     }
00616 
00625     protected function _recodeLangArray( $aLangArray, $sCharset )
00626     {
00627         foreach ( $aLangArray as $sKey => $sValue ) {
00628             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00629         }
00630 
00631         return $aLangArray;
00632     }
00633 
00642     protected function _getLangFilesPathArray( $blAdmin, $iLang )
00643     {
00644         $myConfig = $this->getConfig();
00645         $aLangFiles = array();
00646 
00647         $sOutDir        = $myConfig->getOutDir();
00648         $sLang          = oxLang::getInstance()->getLanguageAbbr( $iLang );
00649         $sTheme         = $myConfig->getConfigParam( "sTheme" );
00650         $sCustomTheme   = $myConfig->getConfigParam( "sCustomTheme" );
00651         $sShopId        = $myConfig->getShopId();
00652 
00653         if ( $blAdmin ) {
00654 
00655             $sAdminPath = $sOutDir . 'admin/' . $sLang . '/';
00656             $aLangFiles[] = $sAdminPath . "lang.php";
00657             $aTmpFiles = glob( $sAdminPath."*_lang.php" );
00658             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00659                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles);
00660             }
00661             return count( $aLangFiles ) ? $aLangFiles : false;
00662         }
00663 
00664         //get generic lang files
00665         $sGenericPath = $sOutDir . $sLang . '/';
00666         if ( $sGenericPath ) {
00667             $aLangFiles[] = $sGenericPath . "lang.php";
00668             $aTmpFiles = glob( $sGenericPath."*_lang.php" );
00669             if ( is_array($aTmpFiles ) && count( $aTmpFiles ) ) {
00670                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00671             }
00672         }
00673 
00674         //get theme lang files
00675         if ( $sTheme ) {
00676             $sThemePath = $sOutDir . $sTheme .'/' . $sLang . '/';
00677             $aLangFiles[] = $sThemePath . "lang.php";
00678             $aTmpFiles = glob( $sThemePath."*_lang.php" );
00679             if ( is_array( $aTmpFiles) && count( $aTmpFiles ) ) {
00680                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00681             }
00682         }
00683 
00684         //get custom theme lang files
00685         if ( $sCustomTheme ) {
00686             $sCustPath = $sOutDir . $sCustomTheme .'/' . $sLang . '/';
00687             $aLangFiles[] = $sCustPath . "lang.php";
00688             $aTmpFiles = glob( $sCustPath."*_lang.php" );
00689             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00690                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00691             }
00692 
00693             // custom theme shop languages
00694             if ( $sCustomTheme ) {
00695                 $sShopPath = $sOutDir . $sCustomTheme .'/' . $sShopId . '/' . $sLang . '/';
00696                 $aLangFiles[] = $sShopPath . "lang.php";
00697                 $aTmpFiles = glob( $sShopPath."*_lang.php" );
00698                 if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00699                     $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00700                 }
00701             }
00702         } else {
00703             // theme shop languages
00704             if ( $sTheme ) {
00705                 $sShopPath = $sOutDir . $sTheme .'/' . $sShopId . '/' . $sLang . '/';
00706                 $aLangFiles[] = $sShopPath . "lang.php";
00707                 $aTmpFiles = glob( $sShopPath."*_lang.php" );
00708                 if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00709                     $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00710                 }
00711             }
00712         }
00713 
00714         //modules language files
00715         $aModuleFiles = glob(getShopBasePath().'/modules/*/out/lang/' . $sLang . '/*_lang.php');
00716         if (is_array($aModuleFiles) && count($aModuleFiles)) {
00717             $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00718         }
00719 
00720         return count( $aLangFiles ) ? $aLangFiles : false;
00721     }
00722 
00732     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00733     {
00734         $myConfig = $this->getConfig();
00735         $sLangFilesIdent = '_default';
00736         if (is_array($aLangFiles) && $aLangFiles) {
00737             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00738         }
00739         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00740     }
00741 
00751     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00752     {
00753         $myConfig = $this->getConfig();
00754         $myUtils  = oxUtils::getInstance();
00755 
00756         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00757         $aLangCache = $myUtils->getLangCache( $sCacheName );
00758         if ( !$aLangCache && $aLangFiles === null ) {
00759             $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang );
00760         }
00761         if ( !$aLangCache && $aLangFiles ) {
00762             $aLangCache = array();
00763             $sBaseCharset = false;
00764             foreach ( $aLangFiles as $sLangFile ) {
00765 
00766                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00767                     include $sLangFile;
00768 
00769                     // including only (!) thoose, which has charset defined
00770                     if ( isset( $aLang['charset'] ) ) {
00771 
00772                         // recoding only in utf
00773                         if ( $myConfig->isUtf() ) {
00774                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00775 
00776                             // overriding charset
00777                             $aLang['charset'] = 'UTF-8';
00778                         }
00779 
00780                         if ( !$sBaseCharset ) {
00781                             $sBaseCharset = $aLang['charset'];
00782                         }
00783 
00784                         $aLangCache = array_merge( $aLangCache, $aLang );
00785                     }
00786                 }
00787             }
00788 
00789             // setting base charset
00790             if ( $sBaseCharset ) {
00791                 $aLangCache['charset'] = $sBaseCharset;
00792             }
00793 
00794             //save to cache
00795             $myUtils->setLangCache( $sCacheName, $aLangCache );
00796         }
00797 
00798         return $aLangCache;
00799     }
00800 
00808     protected function _getLanguageMap( $iLang )
00809     {
00810         if ( !isset($this->_aLangMap[$iLang]) ) {
00811             $this->_aLangMap[$iLang] = array();
00812             $myConfig = $this->getConfig();
00813             $sMapFile = $myConfig->getOutDir() . '/' . $myConfig->getConfigParam( "sTheme" ) .'/' . oxLang::getInstance()->getLanguageAbbr( $iLang ) . '/map.php';
00814             if ( $sMapFile ) {
00815                 if ( file_exists( $sMapFile ) && is_readable( $sMapFile ) ) {
00816                     include $sMapFile;
00817                     $this->_aLangMap[$iLang] = $aMap;
00818                 }
00819             }
00820         }
00821 
00822         return $this->_aLangMap[$iLang];
00823     }
00824 
00833     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00834     {
00835         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00836         if ( !isset( $iLang ) ) {
00837             $iLang = $this->getBaseLanguage();
00838             if ( !isset( $iLang ) ) {
00839                 $iLang = 0;
00840             }
00841         }
00842 
00843         return (int) $iLang;
00844     }
00845 
00855     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00856     {
00857         startProfile("_getLangTranslationArray");
00858 
00859         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00860         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00861         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00862 
00863         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00864             $this->_aLangCache[$sCacheName] = array();
00865         }
00866         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00867             // loading main lang files data
00868             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00869         }
00870 
00871         stopProfile("_getLangTranslationArray");
00872 
00873         // if language array exists ..
00874         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00875     }
00876 
00888     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00889     {
00890         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00891         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00892         if ( !isset( $iLang ) ) {
00893             $iLang = (int) $this->getBaseLanguage();
00894         }
00895 
00896         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00897         if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
00898 
00899             static $aUserLangCache = array();
00900 
00901             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00902                 $handle = @fopen( $sFileName, "r" );
00903                 if ( $handle === false ) {
00904                     return $sStringToTranslate;
00905                 }
00906 
00907                 $contents = fread( $handle, filesize ( $sFileName ) );
00908                 fclose( $handle );
00909                 $fileArray = explode( "\n", $contents );
00910                 $aUserLangCache[$sFileName] = array();
00911                 $aLang = &$aUserLangCache[$sFileName];
00912                 $oStr = getStr();
00913 
00914                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00915                     $line = ltrim( $line );
00916                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00917                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00918                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00919                         $aLang[trim($index)] = trim($value);
00920                     }
00921                 }
00922             }
00923 
00924             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00925                 $aLang = &$aUserLangCache[$sFileName];
00926             }
00927 
00928             if ( isset( $aLang[$sStringToTranslate] ) ) {
00929                 $sStringToTranslate = $aLang[$sStringToTranslate];
00930             }
00931         }
00932 
00933         return $sStringToTranslate;
00934     }
00935 
00944     protected function _sortLanguagesCallback( $a1, $a2 )
00945     {
00946         return ($a1->sort > $a2->sort);
00947     }
00948 
00954     public function getName()
00955     {
00956         return $this->_sName;
00957     }
00958 
00964     public function getFormLang()
00965     {
00966         $sLang = null;
00967         if ( !$this->isAdmin()) {
00968             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
00969         }
00970         return $sLang;
00971     }
00972 
00980     public function getUrlLang( $iLang = null )
00981     {
00982         $sLang = null;
00983         if ( !$this->isAdmin()) {
00984             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00985             $sLang = $this->getName()."=". $iLang;
00986         }
00987         return $sLang;
00988     }
00989 
01002     public function processUrl( $sUrl, $iLang = null )
01003     {
01004         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01005         $oStr = getStr();
01006 
01007         if ( !$this->isAdmin() ) {
01008             $sParam = $this->getUrlLang( $iLang );
01009             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
01010                 if ( $sUrl ) {
01011                     if ($oStr->strpos( $sUrl, '?') === false) {
01012                         $sUrl .= "?";
01013                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
01014                         $sUrl .= "&amp;";
01015                     }
01016                 }
01017                 $sUrl .= $sParam."&amp;";
01018             } else {
01019                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01020             }
01021         }
01022 
01023         return $sUrl;
01024     }
01025 
01032     public function detectLanguageByBrowser()
01033     {
01034         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01035 
01036         if ( !$sBrowserLang ) {
01037             return;
01038         }
01039 
01040         $aLangs = $this->getLanguageArray(null, true );
01041 
01042         foreach ( $aLangs as $oLang ) {
01043             if ( $oLang->abbr == $sBrowserLang ) {
01044                 return (int) $oLang->id;
01045             }
01046         }
01047     }
01048 
01054     public function getMultiLangTables()
01055     {
01056         $aTables = array( "oxarticles", "oxartextends", "oxattribute",
01057                           "oxcategories", "oxcontents", "oxcountry",
01058                           "oxdelivery", "oxdiscount", "oxgroups",
01059                           "oxlinks", "oxnews", "oxobject2attribute",
01060                           "oxpayments", "oxselectlist", "oxshops",
01061                           "oxactions", "oxwrapping", "oxdeliveryset",
01062                           "oxvendor", "oxmanufacturers", "oxmediaurls",
01063                           "oxstates" );
01064 
01065 
01066         $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
01067 
01068         if ( is_array( $aMultiLangTables ) ) {
01069             $aTables = array_merge($aTables, $aMultiLangTables);
01070         }
01071 
01072         return $aTables;
01073     }
01074 }