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 
00437     public function registerAdditionalLangFile($sFile)
00438     {
00439         if (!$sFile || !is_readable($sFile)) {
00440             $oErr = oxNew( "oxFileException", 'EXCEPTION_FILENOTFOUND');
00441             $oErr->setFileName($sFile);
00442             throw $oErr;
00443         }
00444         $this->_aAdditionalLangFiles[] = $sFile;
00445     }
00446 
00459     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00460     {
00461         $myConfig = $this->getConfig();
00462 
00463         // checking if in cash exist
00464         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00465         if ( isset( $aLang[$sStringToTranslate] ) ) {
00466             return $aLang[$sStringToTranslate];
00467         }
00468 
00469         // checking if in map exist
00470         $aMap = $this->_getLanguageMap( $iLang );
00471         if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00472             return $aLang[$aMap[$sStringToTranslate]];
00473         }
00474 
00475         // checking if in theme options exist
00476         if (count($this->_aAdditionalLangFiles)) {
00477             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00478             if (isset( $aLang[$sStringToTranslate] )) {
00479                 return $aLang[$sStringToTranslate];
00480             }
00481         }
00482 
00483         // @deprecated, text files should not be used any more (2011.07.06)
00484             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00485             if ( !$blIsAdmin ) {
00486                 return $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00487             }
00488 
00489         return $sStringToTranslate;
00490     }
00491 
00500     public function formatCurrency( $dValue, $oActCur = null )
00501     {
00502         if ( !$oActCur ) {
00503             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00504         }
00505         return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00506     }
00507 
00516     public function formatVat( $dValue, $oActCur = null )
00517     {
00518         $iDecPos = 0;
00519         $sValue  = ( string ) $dValue;
00520         $oStr = getStr();
00521         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00522             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00523         }
00524 
00525         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00526         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00527         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00528     }
00529 
00537     public function getLanguageTag( $iLanguage = null)
00538     {
00539         if ( !isset( $iLanguage ) ) {
00540             $iLanguage = $this->getBaseLanguage();
00541         }
00542 
00543         $iLanguage = (int) $iLanguage;
00544 
00545         return ( ( $iLanguage )?"_$iLanguage":"" );
00546     }
00547 
00555     public function validateLanguage( $iLang = null )
00556     {
00557         $iLang = (int) $iLang;
00558 
00559         // checking if this language is valid
00560         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00561         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00562             $oLang = current( $aLanguages );
00563             if (isset($oLang->id)) {
00564                 $iLang = $oLang->id;
00565             }
00566         }
00567 
00568         return $iLang;
00569     }
00570 
00578     public function setBaseLanguage( $iLang = null )
00579     {
00580         if ( is_null($iLang) ) {
00581             $iLang = $this->getBaseLanguage();
00582         } else {
00583             $this->_iBaseLanguageId = (int) $iLang;
00584         }
00585 
00586         if ( defined( 'OXID_PHP_UNIT' ) ) {
00587             modSession::getInstance();
00588         }
00589 
00590         oxSession::setVar( 'language', $iLang );
00591     }
00592 
00600     public function setTplLanguage( $iLang = null )
00601     {
00602         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00603         if ( $this->isAdmin() ) {
00604             $aLanguages = $this->getAdminTplLanguageArray();
00605             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00606                 $this->_iTplLanguageId = key( $aLanguages );
00607             }
00608         }
00609 
00610         if ( defined( 'OXID_PHP_UNIT' ) ) {
00611             modSession::getInstance();
00612         }
00613 
00614         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00615         return $this->_iTplLanguageId;
00616     }
00617 
00626     protected function _recodeLangArray( $aLangArray, $sCharset )
00627     {
00628         foreach ( $aLangArray as $sKey => $sValue ) {
00629             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00630         }
00631 
00632         return $aLangArray;
00633     }
00634 
00643     protected function _getLangFilesPathArray( $blAdmin, $iLang )
00644     {
00645         $myConfig = $this->getConfig();
00646         $aLangFiles = array();
00647 
00648         $sOutDir        = $myConfig->getOutDir();
00649         $sLang          = oxLang::getInstance()->getLanguageAbbr( $iLang );
00650         $sTheme         = $myConfig->getConfigParam( "sTheme" );
00651         $sCustomTheme   = $myConfig->getConfigParam( "sCustomTheme" );
00652         $sShopId        = $myConfig->getShopId();
00653 
00654         if ( $blAdmin ) {
00655 
00656             // admin lang files
00657             $sAdminPath = $sOutDir . 'admin/' . $sLang . '/';
00658             $aLangFiles[] = $sAdminPath . "lang.php";
00659             $aTmpFiles = glob( $sAdminPath."*_lang.php" );
00660             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00661                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles);
00662             }
00663 
00664             // themes options lang files
00665             $sThemePath = $sOutDir . '*/' . $sLang . '/theme_options.php';
00666             $aTmpFiles = glob( $sThemePath );
00667             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00668                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles);
00669             }
00670 
00671             return count( $aLangFiles ) ? $aLangFiles : false;
00672         }
00673 
00674         //get generic lang files
00675         $sGenericPath = $sOutDir . $sLang . '/';
00676         if ( $sGenericPath ) {
00677             $aLangFiles[] = $sGenericPath . "lang.php";
00678             $aTmpFiles = glob( $sGenericPath."*_lang.php" );
00679             if ( is_array($aTmpFiles ) && count( $aTmpFiles ) ) {
00680                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00681             }
00682         }
00683 
00684         //get theme lang files
00685         if ( $sTheme ) {
00686             $sThemePath = $sOutDir . $sTheme .'/' . $sLang . '/';
00687             $aLangFiles[] = $sThemePath . "lang.php";
00688             $aTmpFiles = glob( $sThemePath."*_lang.php" );
00689             if ( is_array( $aTmpFiles) && count( $aTmpFiles ) ) {
00690                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00691             }
00692         }
00693 
00694         //get custom theme lang files
00695         if ( $sCustomTheme ) {
00696             $sCustPath = $sOutDir . $sCustomTheme .'/' . $sLang . '/';
00697             $aLangFiles[] = $sCustPath . "lang.php";
00698             $aTmpFiles = glob( $sCustPath."*_lang.php" );
00699             if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00700                 $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00701             }
00702 
00703             // custom theme shop languages
00704             if ( $sCustomTheme ) {
00705                 $sShopPath = $sOutDir . $sCustomTheme .'/' . $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         } else {
00713             // theme shop languages
00714             if ( $sTheme ) {
00715                 $sShopPath = $sOutDir . $sTheme .'/' . $sShopId . '/' . $sLang . '/';
00716                 $aLangFiles[] = $sShopPath . "lang.php";
00717                 $aTmpFiles = glob( $sShopPath."*_lang.php" );
00718                 if ( is_array( $aTmpFiles ) && count( $aTmpFiles ) ) {
00719                     $aLangFiles = array_merge( $aLangFiles, $aTmpFiles );
00720                 }
00721             }
00722         }
00723 
00724         //modules language files
00725         $aModuleFiles = glob(getShopBasePath().'/modules/*/out/lang/' . $sLang . '/*_lang.php');
00726         if (is_array($aModuleFiles) && count($aModuleFiles)) {
00727             $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00728         }
00729 
00730         return count( $aLangFiles ) ? $aLangFiles : false;
00731     }
00732 
00742     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00743     {
00744         $myConfig = $this->getConfig();
00745         $sLangFilesIdent = '_default';
00746         if (is_array($aLangFiles) && $aLangFiles) {
00747             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00748         }
00749         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00750     }
00751 
00761     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00762     {
00763         $myConfig = $this->getConfig();
00764         $myUtils  = oxUtils::getInstance();
00765 
00766         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00767         $aLangCache = $myUtils->getLangCache( $sCacheName );
00768         if ( !$aLangCache && $aLangFiles === null ) {
00769             $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang );
00770         }
00771         if ( !$aLangCache && $aLangFiles ) {
00772             $aLangCache = array();
00773             $sBaseCharset = false;
00774             foreach ( $aLangFiles as $sLangFile ) {
00775 
00776                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00777                     include $sLangFile;
00778 
00779                     // including only (!) thoose, which has charset defined
00780                     if ( isset( $aLang['charset'] ) ) {
00781 
00782                         // recoding only in utf
00783                         if ( $myConfig->isUtf() ) {
00784                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00785 
00786                             // overriding charset
00787                             $aLang['charset'] = 'UTF-8';
00788                         }
00789 
00790                         if ( !$sBaseCharset ) {
00791                             $sBaseCharset = $aLang['charset'];
00792                         }
00793 
00794                         $aLangCache = array_merge( $aLangCache, $aLang );
00795                     }
00796                 }
00797             }
00798 
00799             // setting base charset
00800             if ( $sBaseCharset ) {
00801                 $aLangCache['charset'] = $sBaseCharset;
00802             }
00803 
00804             //save to cache
00805             $myUtils->setLangCache( $sCacheName, $aLangCache );
00806         }
00807 
00808         return $aLangCache;
00809     }
00810 
00818     protected function _getLanguageMap( $iLang )
00819     {
00820         if ( !isset($this->_aLangMap[$iLang]) ) {
00821             $this->_aLangMap[$iLang] = array();
00822             $myConfig = $this->getConfig();
00823             $sMapFile = $myConfig->getOutDir() . '/' . $myConfig->getConfigParam( "sTheme" ) .'/' . oxLang::getInstance()->getLanguageAbbr( $iLang ) . '/map.php';
00824             if ( $sMapFile ) {
00825                 if ( file_exists( $sMapFile ) && is_readable( $sMapFile ) ) {
00826                     include $sMapFile;
00827                     $this->_aLangMap[$iLang] = $aMap;
00828                 }
00829             }
00830         }
00831 
00832         return $this->_aLangMap[$iLang];
00833     }
00834 
00843     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00844     {
00845         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00846         if ( !isset( $iLang ) ) {
00847             $iLang = $this->getBaseLanguage();
00848             if ( !isset( $iLang ) ) {
00849                 $iLang = 0;
00850             }
00851         }
00852 
00853         return (int) $iLang;
00854     }
00855 
00865     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00866     {
00867         startProfile("_getLangTranslationArray");
00868 
00869         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00870         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00871         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00872 
00873         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00874             $this->_aLangCache[$sCacheName] = array();
00875         }
00876         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00877             // loading main lang files data
00878             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00879         }
00880 
00881         stopProfile("_getLangTranslationArray");
00882 
00883         // if language array exists ..
00884         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00885     }
00886 
00898     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00899     {
00900         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00901         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00902         if ( !isset( $iLang ) ) {
00903             $iLang = (int) $this->getBaseLanguage();
00904         }
00905 
00906         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00907         if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
00908 
00909             static $aUserLangCache = array();
00910 
00911             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00912                 $handle = @fopen( $sFileName, "r" );
00913                 if ( $handle === false ) {
00914                     return $sStringToTranslate;
00915                 }
00916 
00917                 $contents = fread( $handle, filesize ( $sFileName ) );
00918                 fclose( $handle );
00919                 $fileArray = explode( "\n", $contents );
00920                 $aUserLangCache[$sFileName] = array();
00921                 $aLang = &$aUserLangCache[$sFileName];
00922                 $oStr = getStr();
00923 
00924                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00925                     $line = ltrim( $line );
00926                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00927                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00928                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00929                         $aLang[trim($index)] = trim($value);
00930                     }
00931                 }
00932             }
00933 
00934             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00935                 $aLang = &$aUserLangCache[$sFileName];
00936             }
00937 
00938             if ( isset( $aLang[$sStringToTranslate] ) ) {
00939                 $sStringToTranslate = $aLang[$sStringToTranslate];
00940             }
00941         }
00942 
00943         return $sStringToTranslate;
00944     }
00945 
00954     protected function _sortLanguagesCallback( $a1, $a2 )
00955     {
00956         return ($a1->sort > $a2->sort);
00957     }
00958 
00964     public function getName()
00965     {
00966         return $this->_sName;
00967     }
00968 
00974     public function getFormLang()
00975     {
00976         $sLang = null;
00977         if ( !$this->isAdmin()) {
00978             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
00979         }
00980         return $sLang;
00981     }
00982 
00990     public function getUrlLang( $iLang = null )
00991     {
00992         $sLang = null;
00993         if ( !$this->isAdmin()) {
00994             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00995             $sLang = $this->getName()."=". $iLang;
00996         }
00997         return $sLang;
00998     }
00999 
01012     public function processUrl( $sUrl, $iLang = null )
01013     {
01014         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01015         $oStr = getStr();
01016 
01017         if ( !$this->isAdmin() ) {
01018             $sParam = $this->getUrlLang( $iLang );
01019             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
01020                 if ( $sUrl ) {
01021                     if ($oStr->strpos( $sUrl, '?') === false) {
01022                         $sUrl .= "?";
01023                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
01024                         $sUrl .= "&amp;";
01025                     }
01026                 }
01027                 $sUrl .= $sParam."&amp;";
01028             } else {
01029                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01030             }
01031         }
01032 
01033         return $sUrl;
01034     }
01035 
01042     public function detectLanguageByBrowser()
01043     {
01044         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01045 
01046         if ( !$sBrowserLang ) {
01047             return;
01048         }
01049 
01050         $aLangs = $this->getLanguageArray(null, true );
01051 
01052         foreach ( $aLangs as $oLang ) {
01053             if ( $oLang->abbr == $sBrowserLang ) {
01054                 return (int) $oLang->id;
01055             }
01056         }
01057     }
01058 
01064     public function getMultiLangTables()
01065     {
01066         $aTables = array( "oxarticles", "oxartextends", "oxattribute",
01067                           "oxcategories", "oxcontents", "oxcountry",
01068                           "oxdelivery", "oxdiscount", "oxgroups",
01069                           "oxlinks", "oxnews", "oxobject2attribute",
01070                           "oxpayments", "oxselectlist", "oxshops",
01071                           "oxactions", "oxwrapping", "oxdeliveryset",
01072                           "oxvendor", "oxmanufacturers", "oxmediaurls",
01073                           "oxstates" );
01074 
01075 
01076         $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
01077 
01078         if ( is_array( $aMultiLangTables ) ) {
01079             $aTables = array_merge($aTables, $aMultiLangTables);
01080         }
01081 
01082         return $aTables;
01083     }
01084 }