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     public static function getInstance()
00077     {
00078         if ( defined('OXID_PHP_UNIT')) {
00079             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00080                 return $oClassMod;
00081             } else {
00082                 $inst = oxNew( 'oxLang' );
00083                  modInstances::addMod( __CLASS__, $inst );
00084                  return $inst;
00085             }
00086         }
00087 
00088         if ( !self::$_instance instanceof oxLang ) {
00089 
00090             self::$_instance = oxNew( 'oxLang');
00091         }
00092         return self::$_instance;
00093     }
00094 
00101     public function resetBaseLanguage()
00102     {
00103         $this->_iBaseLanguageId = null;
00104     }
00105 
00111     public function getBaseLanguage()
00112     {
00113         if ( $this->_iBaseLanguageId === null ) {
00114 
00115             $myConfig = $this->getConfig();
00116             $blAdmin = $this->isAdmin();
00117 
00118             // languages and search engines
00119             if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00120                 $this->_iBaseLanguageId = $iSeLang;
00121             }
00122 
00123             if ( is_null( $this->_iBaseLanguageId ) ) {
00124                 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00125             }
00126 
00127             //or determining by domain
00128             $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00129 
00130             if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00131                 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00132                     if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00133                         $this->_iBaseLanguageId = $iId;
00134                         break;
00135                     }
00136                 }
00137             }
00138 
00139             if ( is_null( $this->_iBaseLanguageId ) ) {
00140                 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00141                 if (!isset($this->_iBaseLanguageId)) {
00142                     $this->_iBaseLanguageId = oxSession::getVar('language');
00143                 }
00144             }
00145 
00146             // if language still not setted and not search engine browsing,
00147             // getting language from browser
00148             if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxUtils::getInstance()->isSearchEngine() ) {
00149 
00150                 // getting from cookie
00151                 $this->_iBaseLanguageId = oxUtilsServer::getInstance()->getOxCookie( 'language' );
00152 
00153                 // getting from browser
00154                 if ( is_null( $this->_iBaseLanguageId ) ) {
00155                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00156                 }
00157             }
00158 
00159             if ( is_null( $this->_iBaseLanguageId ) ) {
00160                 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00161             }
00162 
00163             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00164 
00165             // validating language
00166             $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00167 
00168             // setting language to cookie
00169             oxUtilsServer::getInstance()->setOxCookie( 'language', $this->_iBaseLanguageId );
00170         }
00171 
00172         return $this->_iBaseLanguageId;
00173     }
00174 
00180     public function getObjectTplLanguage()
00181     {
00182         if ( $this->_iObjectTplLanguageId === null ) {
00183             $this->_iObjectTplLanguageId = $this->getTplLanguage();
00184             $aLanguages = $this->getAdminTplLanguageArray();
00185             if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00186                  $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00187                 $this->_iObjectTplLanguageId = key( $aLanguages );
00188             }
00189         }
00190         return $this->_iObjectTplLanguageId;
00191     }
00192 
00200     public function getTplLanguage()
00201     {
00202         if ( $this->_iTplLanguageId === null ) {
00203             $iSessLang = oxSession::getVar( 'tpllanguage' );
00204             $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00205         }
00206         return $this->_iTplLanguageId;
00207     }
00208 
00214     public function getEditLanguage()
00215     {
00216         if ( $this->_iEditLanguageId === null ) {
00217 
00218             if ( !$this->isAdmin() ) {
00219                 $this->_iEditLanguageId = $this->getBaseLanguage();
00220             } else {
00221 
00222                 // choosing language ident
00223                 // check if we really need to set the new language
00224                 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00225                     $iLang = oxConfig::getParameter( "new_lang");
00226                 }
00227                 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00228                 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00229                 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00230 
00231                 // validating language
00232                 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00233 
00234                 // writing to session
00235                 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00236             }
00237         }
00238         return $this->_iEditLanguageId;
00239     }
00240 
00250     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00251     {
00252         $myConfig = $this->getConfig();
00253 
00254         if ( is_null($iLanguage) ) {
00255             $iLanguage = $this->_iBaseLanguageId;
00256         }
00257 
00258         $aLanguages = array();
00259         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00260         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00261 
00262         if ( is_array( $aConfLanguages ) ) {
00263             $i = 0;
00264             reset( $aConfLanguages );
00265             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00266 
00267                 if ( $blOnlyActive && is_array($aLangParams) ) {
00268                     //skipping non active languages
00269                     if ( !$aLangParams[$key]['active'] ) {
00270                         $i++;
00271                         continue;
00272                     }
00273                 }
00274 
00275                 if ( $val ) {
00276                     $oLang = new oxStdClass();
00277                     $oLang->id   = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00278                     $oLang->oxid = $key;
00279                     $oLang->abbr = $key;
00280                     $oLang->name = $val;
00281 
00282                     if ( is_array( $aLangParams ) ) {
00283                         $oLang->active = $aLangParams[$key]['active'];
00284                         $oLang->sort   = $aLangParams[$key]['sort'];
00285                     }
00286 
00287                     $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00288                     $aLanguages[$oLang->id] = $oLang;
00289                 }
00290                 ++$i;
00291             }
00292         }
00293 
00294         if ( $blSort && is_array($aLangParams) ) {
00295             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00296         }
00297 
00298 
00299         return $aLanguages;
00300     }
00301 
00307     public function getAdminTplLanguageArray()
00308     {
00309         if ( $this->_aAdminTplLanguageArray === null ) {
00310 
00311             // #656 add admin languages
00312             $aLangData = array();
00313             $aLangIds  = $this->getLanguageIds();
00314 
00315             $sSourceDir = $this->getConfig()->getStdLanguagePath( "", true, false );
00316             foreach ( glob( $sSourceDir."*", GLOB_ONLYDIR ) as $sDir ) {
00317                 $sFilePath = "{$sDir}/lang.php";
00318                 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00319                     $sLangName = "";
00320                     $sAbbr = strtolower( basename( $sDir ) );
00321                     if ( !in_array( $sAbbr, $aLangIds ) ) {
00322                         include $sFilePath;
00323                         $aLangData[$sAbbr] = new oxStdClass();
00324                         $aLangData[$sAbbr]->name = $sLangName;
00325                         $aLangData[$sAbbr]->abbr = $sAbbr;
00326                     }
00327                 }
00328             }
00329 
00330             $this->_aAdminTplLanguageArray = $this->getLanguageArray();
00331             if ( count( $aLangData ) ) {
00332 
00333                 // sorting languages for selection list view
00334                 ksort( $aLangData );
00335                 $iSort = max( array_keys( $this->_aAdminTplLanguageArray ) );
00336 
00337                 // appending other languages
00338                 foreach ( $aLangData as $oLang ) {
00339                     $oLang->id = $oLang->sort = ++$iSort;
00340                     $oLang->selected = 0;
00341                     $oLang->active   = 0;
00342                     $this->_aAdminTplLanguageArray[$iSort] = $oLang;
00343                 }
00344             }
00345         }
00346 
00347         // moving pointer to beginning
00348         reset( $this->_aAdminTplLanguageArray );
00349         return $this->_aAdminTplLanguageArray;
00350     }
00351 
00359     public function getLanguageAbbr( $iLanguage = null )
00360     {
00361         if ( $this->_aLangAbbr === null ) {
00362             $this->_aLangAbbr = array();
00363             if ( $this->isAdmin() ) {
00364                 foreach ( $this->getAdminTplLanguageArray() as $oLang ) {
00365                     $this->_aLangAbbr[$oLang->id] = $oLang->abbr;
00366                 }
00367             } else {
00368                 $this->_aLangAbbr = $this->getLanguageIds();
00369             }
00370         }
00371 
00372         $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00373         if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00374             $iLanguage = $this->_aLangAbbr[$iLanguage];
00375         }
00376 
00377         return $iLanguage;
00378     }
00379 
00386     public function getLanguageNames()
00387     {
00388         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00389         $aLangIds = $this->getLanguageIds();
00390         $aLanguages = array();
00391         foreach ( $aLangIds as $iId => $sValue ) {
00392             $aLanguages[$iId] = $aConfLanguages[$sValue];
00393         }
00394         return $aLanguages;
00395     }
00396 
00402     public function getLanguageIds()
00403     {
00404         $myConfig = $this->getConfig();
00405         $aIds = array();
00406 
00407         //if exists language parameters array, extract lang id's from there
00408         $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00409         if ( is_array( $aLangParams ) ) {
00410             foreach ( $aLangParams as $sAbbr => $aValue ) {
00411                 $iBaseId = (int) $aValue['baseId'];
00412                 $aIds[$iBaseId] = $sAbbr;
00413             }
00414         } else {
00415             $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00416         }
00417 
00418         return $aIds;
00419     }
00420 
00428     public function registerAdditionalLangFile($sFile)
00429     {
00430         if (!$sFile || !is_readable($sFile)) {
00431             $oErr = new oxFileException('EXCEPTION_FILENOTFOUND');
00432             $oErr->setFileName($sFile);
00433             throw $oErr;
00434         }
00435         $this->_aAdditionalLangFiles[] = $sFile;
00436     }
00437 
00450     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00451     {
00452         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00453         if (isset( $aLang[$sStringToTranslate] )) {
00454             return $aLang[$sStringToTranslate];
00455         }
00456 
00457         if (count($this->_aAdditionalLangFiles)) {
00458             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00459             if (isset( $aLang[$sStringToTranslate] )) {
00460                 return $aLang[$sStringToTranslate];
00461             }
00462         }
00463 
00464             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00465             if ( !$blIsAdmin ) {
00466                 return $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00467             }
00468 
00469         return $sStringToTranslate;
00470     }
00471 
00480     public function formatCurrency( $dValue, $oActCur = null )
00481     {
00482         if ( !$oActCur ) {
00483             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00484         }
00485         return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00486     }
00487 
00496     public function formatVat( $dValue, $oActCur = null )
00497     {
00498         $iDecPos = 0;
00499         $sValue  = ( string ) $dValue;
00500         $oStr = getStr();
00501         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00502             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00503         }
00504 
00505         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00506         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00507         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00508     }
00509 
00517     public function getLanguageTag( $iLanguage = null)
00518     {
00519         if ( !isset( $iLanguage ) ) {
00520             $iLanguage = $this->getBaseLanguage();
00521         }
00522 
00523         $iLanguage = (int) $iLanguage;
00524 
00525         return ( ( $iLanguage )?"_$iLanguage":"" );
00526     }
00527 
00535     public function validateLanguage( $iLang = null )
00536     {
00537         $iLang = (int) $iLang;
00538 
00539         // checking if this language is valid
00540         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00541         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00542             $oLang = current( $aLanguages );
00543             if (isset($oLang->id)) {
00544                 $iLang = $oLang->id;
00545             }
00546         }
00547 
00548         return $iLang;
00549     }
00550 
00558     public function setBaseLanguage( $iLang = null )
00559     {
00560         if ( is_null($iLang) ) {
00561             $iLang = $this->getBaseLanguage();
00562         } else {
00563             $this->_iBaseLanguageId = (int) $iLang;
00564         }
00565 
00566         if ( defined( 'OXID_PHP_UNIT' ) ) {
00567             modSession::getInstance();
00568         }
00569 
00570         oxSession::setVar( 'language', $iLang );
00571     }
00572 
00580     public function setTplLanguage( $iLang = null )
00581     {
00582         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00583         if ( $this->isAdmin() ) {
00584             $aLanguages = $this->getAdminTplLanguageArray();
00585             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00586                 $this->_iTplLanguageId = key( $aLanguages );
00587             }
00588         }
00589 
00590         if ( defined( 'OXID_PHP_UNIT' ) ) {
00591             modSession::getInstance();
00592         }
00593 
00594         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00595         return $this->_iTplLanguageId;
00596     }
00597 
00606     protected function _recodeLangArray( $aLangArray, $sCharset )
00607     {
00608         foreach ( $aLangArray as $sKey => $sValue ) {
00609             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00610         }
00611 
00612         return $aLangArray;
00613     }
00614 
00623     protected function _getLangFilesPathArray( $blAdmin, $iLang )
00624     {
00625         $myConfig = $this->getConfig();
00626         $aLangFiles = array();
00627 
00628         //get all lang files
00629         $sStdPath = $myConfig->getStdLanguagePath( "", $blAdmin, $iLang );
00630         if ( $sStdPath ) {
00631             $aLangFiles[] = $sStdPath . "lang.php";
00632             $aLangFiles = array_merge( $aLangFiles, glob( $sStdPath."*_lang.php" ) );
00633         }
00634 
00635         $sCustPath = $myConfig->getLanguagePath( "", $blAdmin, $iLang );
00636         if ( $sCustPath && $sCustPath != $sStdPath ) {
00637             if ( is_readable( $sCustPath . "lang.php" ) ) {
00638                 $aLangFiles[] = $sCustPath . "lang.php";
00639             }
00640             $aLangFiles = array_merge( $aLangFiles, glob( $sCustPath."*_lang.php" ) );
00641         }
00642 
00643         $aModuleFiles = glob(getShopBasePath().'/modules/*/out/lang/'.oxLang::getInstance()->getLanguageAbbr( $iLang ).'/*_lang.php');
00644         if (is_array($aModuleFiles) && count($aModuleFiles)) {
00645             $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00646         }
00647 
00648         return count( $aLangFiles ) ? $aLangFiles : false;
00649     }
00650 
00660     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00661     {
00662         $myConfig = $this->getConfig();
00663         $sLangFilesIdent = '_default';
00664         if (is_array($aLangFiles) && $aLangFiles) {
00665             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00666         }
00667         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00668     }
00669 
00679     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00680     {
00681         $myConfig = $this->getConfig();
00682         $myUtils  = oxUtils::getInstance();
00683 
00684         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00685         $aLangCache = $myUtils->getLangCache( $sCacheName );
00686         if ( !$aLangCache && $aLangFiles === null ) {
00687             $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang );
00688         }
00689         if ( !$aLangCache && $aLangFiles ) {
00690             $aLangCache = array();
00691             $sBaseCharset = false;
00692             foreach ( $aLangFiles as $sLangFile ) {
00693 
00694                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00695                     include $sLangFile;
00696 
00697                     // including only (!) thoose, which has charset defined
00698                     if ( isset( $aLang['charset'] ) ) {
00699 
00700                         // recoding only in utf
00701                         if ( $myConfig->isUtf() ) {
00702                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00703 
00704                             // overriding charset
00705                             $aLang['charset'] = 'UTF-8';
00706                         }
00707 
00708                         if ( !$sBaseCharset ) {
00709                             $sBaseCharset = $aLang['charset'];
00710                         }
00711 
00712                         $aLangCache = array_merge( $aLangCache, $aLang );
00713                     }
00714                 }
00715             }
00716 
00717             // setting base charset
00718             if ( $sBaseCharset ) {
00719                 $aLangCache['charset'] = $sBaseCharset;
00720             }
00721 
00722             //save to cache
00723             $myUtils->setLangCache( $sCacheName, $aLangCache );
00724         }
00725 
00726         return $aLangCache;
00727     }
00728 
00737     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00738     {
00739         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00740         if ( !isset( $iLang ) ) {
00741             $iLang = $this->getBaseLanguage();
00742             if ( !isset( $iLang ) ) {
00743                 $iLang = 0;
00744             }
00745         }
00746 
00747         return (int) $iLang;
00748     }
00749 
00759     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00760     {
00761         startProfile("_getLangTranslationArray");
00762 
00763         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00764         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00765         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00766 
00767         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00768             $this->_aLangCache[$sCacheName] = array();
00769         }
00770         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00771             // loading main lang files data
00772             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00773         }
00774 
00775         stopProfile("_getLangTranslationArray");
00776 
00777         // if language array exists ..
00778         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00779     }
00780 
00790     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00791     {
00792         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00793         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00794         if ( !isset( $iLang ) ) {
00795             $iLang = (int) $this->getBaseLanguage();
00796         }
00797 
00798         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00799         if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
00800 
00801             static $aUserLangCache = array();
00802 
00803             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00804                 $handle = @fopen( $sFileName, "r" );
00805                 if ( $handle === false ) {
00806                     return $sStringToTranslate;
00807                 }
00808 
00809                 $contents = fread( $handle, filesize ( $sFileName ) );
00810                 fclose( $handle );
00811                 $fileArray = explode( "\n", $contents );
00812                 $aUserLangCache[$sFileName] = array();
00813                 $aLang = &$aUserLangCache[$sFileName];
00814                 $oStr = getStr();
00815 
00816                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00817                     $line = ltrim( $line );
00818                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00819                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00820                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00821                         $aLang[trim($index)] = trim($value);
00822                     }
00823                 }
00824             }
00825 
00826             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00827                 $aLang = &$aUserLangCache[$sFileName];
00828             }
00829 
00830             if ( isset( $aLang[$sStringToTranslate] ) ) {
00831                 $sStringToTranslate = $aLang[$sStringToTranslate];
00832             }
00833         }
00834 
00835         return $sStringToTranslate;
00836     }
00837 
00846     protected function _sortLanguagesCallback( $a1, $a2 )
00847     {
00848         return ($a1->sort > $a2->sort);
00849     }
00850 
00856     public function getName()
00857     {
00858         return $this->_sName;
00859     }
00860 
00866     public function getFormLang()
00867     {
00868         $sLang = null;
00869         if ( !$this->isAdmin()) {
00870             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
00871         }
00872         return $sLang;
00873     }
00874 
00882     public function getUrlLang( $iLang = null )
00883     {
00884         $sLang = null;
00885         if ( !$this->isAdmin()) {
00886             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00887             $sLang = $this->getName()."=". $iLang;
00888         }
00889         return $sLang;
00890     }
00891 
00904     public function processUrl( $sUrl, $iLang = null )
00905     {
00906         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00907         $oStr = getStr();
00908 
00909         if ( !$this->isAdmin() ) {
00910             $sParam = $this->getUrlLang( $iLang );
00911             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
00912                 if ( $sUrl ) {
00913                     if ($oStr->strpos( $sUrl, '?') === false) {
00914                         $sUrl .= "?";
00915                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
00916                         $sUrl .= "&amp;";
00917                     }
00918                 }
00919                 $sUrl .= $sParam."&amp;";
00920             } else {
00921                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
00922             }
00923         }
00924 
00925         return $sUrl;
00926     }
00927 
00934     public function detectLanguageByBrowser()
00935     {
00936         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
00937 
00938         if ( !$sBrowserLang ) {
00939             return;
00940         }
00941 
00942         $aLangs = $this->getLanguageArray(null, true );
00943 
00944         foreach ( $aLangs as $oLang ) {
00945             if ( $oLang->abbr == $sBrowserLang ) {
00946                 return (int) $oLang->id;
00947             }
00948         }
00949     }
00950 }