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 $_iBaseLanguageId = null;
00021 
00027     protected $_iTplLanguageId = null;
00028 
00034     protected $_iEditLanguageId = null;
00035 
00041     protected $_aLangCache = array();
00042 
00048     protected $_aAdminLangCache = array();
00049 
00055     public static function getInstance()
00056     {
00057         if ( defined('OXID_PHP_UNIT')) {
00058             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00059                 return $oClassMod;
00060             } else {
00061                 $inst = oxNew( 'oxLang' );
00062                  modInstances::addMod( __CLASS__, $inst );
00063                  return $inst;
00064             }
00065         }
00066 
00067         if ( !self::$_instance instanceof oxLang ) {
00068 
00069             self::$_instance = oxNew( 'oxLang');
00070         }
00071         return self::$_instance;
00072     }
00073 
00080     public function resetBaseLanguage()
00081     {
00082         $this->_iBaseLanguageId = null;
00083     }
00084 
00090     public function getBaseLanguage()
00091     {
00092         $myConfig = $this->getConfig();
00093         //$this->_iBaseLanguageId = null;
00094 
00095         if ( $this->_iBaseLanguageId !== null ) {
00096             return $this->_iBaseLanguageId;
00097         }
00098 
00099         $blAdmin = $this->isAdmin();
00100 
00101         // languages and search engines
00102         if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00103             $this->_iBaseLanguageId = $iSeLang;
00104         }
00105 
00106         if ( is_null( $this->_iBaseLanguageId ) ) {
00107             $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00108         }
00109 
00110         //or determining by domain
00111         $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00112 
00113         if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00114             foreach ( $aLanguageUrls as $iId => $sUrl ) {
00115                 if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00116                     $this->_iBaseLanguageId = $iId;
00117                     break;
00118                 }
00119             }
00120         }
00121 
00122         if ( is_null( $this->_iBaseLanguageId ) ) {
00123             $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00124         }
00125 
00126         if ( is_null( $this->_iBaseLanguageId ) ) {
00127             $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00128         }
00129 
00130         $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00131 
00132         // validating language
00133         $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00134 
00135         return $this->_iBaseLanguageId;
00136     }
00137 
00145     public function getTplLanguage()
00146     {
00147         if ( $this->_iTplLanguageId !== null ) {
00148             return $this->_iTplLanguageId;
00149         }
00150 
00151         if ( !$this->isAdmin() ) {
00152             $this->_iTplLanguageId = $this->getBaseLanguage();
00153         } else {
00154             //admin area
00155 
00156             if ( is_null( $this->_iTplLanguageId ) ) {
00157                 //$this->_iTplLanguageId = oxConfig::getParameter( 'tpllanguage' );
00158                 $this->_iTplLanguageId = oxSession::getVar( 'tpllanguage' );
00159             }
00160 
00161             if ( is_null( $this->_iTplLanguageId ) ) {
00162                 $this->_iTplLanguageId = $this->getBaseLanguage();
00163             }
00164         }
00165 
00166         // validating language
00167         $this->_iTplLanguageId = $this->validateLanguage( $this->_iTplLanguageId );
00168 
00169         return $this->_iTplLanguageId;
00170     }
00171 
00177     public function getEditLanguage()
00178     {
00179         if ( $this->_iEditLanguageId !== null ) {
00180             return $this->_iEditLanguageId;
00181         }
00182 
00183         if ( !$this->isAdmin() ) {
00184             $this->_iEditLanguageId = $this->getBaseLanguage();
00185         } else {
00186 
00187             $this->_iEditLanguageId = oxConfig::getParameter( 'editlanguage' );
00188 
00189             // check if we really need to set the new language
00190             if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00191                 $iNewLanguage = oxConfig::getParameter( "new_lang");
00192             }
00193 
00194             if ( isset( $iNewLanguage ) ) {
00195                 $this->_iEditLanguageId = $iNewLanguage;
00196                 oxSession::deleteVar( "new_lang" );
00197             }
00198 
00199             if ( is_null( $this->_iEditLanguageId ) ) {
00200 
00201                 $this->_iEditLanguageId = $this->getBaseLanguage();
00202             }
00203         }
00204 
00205         // validating language
00206         $this->_iEditLanguageId = $this->validateLanguage( $this->_iEditLanguageId );
00207 
00208         return $this->_iEditLanguageId;
00209     }
00210 
00220     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00221     {
00222         $myConfig = $this->getConfig();
00223 
00224         if ( is_null($iLanguage) ) {
00225             $iLanguage = $this->_iBaseLanguageId;
00226         }
00227 
00228         $aLanguages = array();
00229         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00230         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00231 
00232         if ( is_array( $aConfLanguages ) ) {
00233             $i = 0;
00234             reset( $aConfLanguages );
00235             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00236 
00237                 if ( $blOnlyActive && is_array($aLangParams) ) {
00238                     //skipping non active languages
00239                     if ( !$aLangParams[$key]['active'] ) {
00240                         $i++;
00241                         continue;
00242                     }
00243                 }
00244 
00245                 if ( $val) {
00246                     $oLang = new oxStdClass();
00247                     if ( isset($aLangParams[$key]['baseId']) ) {
00248                         $oLang->id  = $aLangParams[$key]['baseId'];
00249                     } else {
00250                         $oLang->id  = $i;
00251                     }
00252                     $oLang->oxid    = $key;
00253                     $oLang->abbr    = $key;
00254                     $oLang->name    = $val;
00255 
00256                     if ( is_array($aLangParams) ) {
00257                         $oLang->active  = $aLangParams[$key]['active'];
00258                         $oLang->sort   = $aLangParams[$key]['sort'];
00259                     }
00260 
00261                     if ( isset( $iLanguage ) && $oLang->id == $iLanguage ) {
00262                         $oLang->selected = 1;
00263                     } else {
00264                         $oLang->selected = 0;
00265                     }
00266                     $aLanguages[$oLang->id] = $oLang;
00267                 }
00268                 ++$i;
00269             }
00270         }
00271 
00272         if ( $blSort && is_array($aLangParams) ) {
00273             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00274         }
00275 
00276         return $aLanguages;
00277     }
00278 
00287     public function getLanguageAbbr( $iLanguage = null)
00288     {
00289         $myConfig = $this->getConfig();
00290 
00291         if ( !isset($iLanguage) ) {
00292             $iLanguage = $this->_iBaseLanguageId;
00293         }
00294 
00295         $aLangAbbr = $this->getLanguageIds();
00296 
00297         if ( isset($iLanguage,$aLangAbbr[$iLanguage]) ) {
00298             return $aLangAbbr[$iLanguage];
00299         }
00300 
00301         return $iLanguage;
00302     }
00303 
00310     public function getLanguageNames()
00311     {
00312 
00313         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00314         $aLangIds = $this->getLanguageIds();
00315         $aLanguages = array();
00316         foreach ( $aLangIds as $iId => $sValue ) {
00317             $aLanguages[$iId] = $aConfLanguages[$sValue];
00318         }
00319         return $aLanguages;
00320     }
00321 
00327     public function getLanguageIds()
00328     {
00329         $aLangParams = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00330 
00331         //if exists language parameters array, extract lang id's from there
00332         if ( is_array($aLangParams) ) {
00333 
00334             $aIds = array();
00335 
00336             foreach ( $aLangParams as $sAbbr => $aValue ) {
00337                 $iBaseId = (int) $aValue['baseId'];
00338                 $aIds[$iBaseId] = $sAbbr;
00339             }
00340             return $aIds;
00341         }
00342 
00343         return array_keys( $this->getConfig()->getConfigParam( 'aLanguages' ));
00344     }
00345 
00358     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00359     {
00360         $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00361         $sText = isset( $aLangCache[$sStringToTranslate] ) ? $aLangCache[$sStringToTranslate] : $sStringToTranslate;
00362 
00363             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00364             if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00365                 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00366             }
00367 
00368         return $sText;
00369     }
00370 
00379     public function formatCurrency( $dValue, $oActCur = null )
00380     {
00381         if ( !$oActCur ) {
00382             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00383         }
00384         return number_format( $dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00385     }
00386 
00395     public function formatVat( $dValue, $oActCur = null )
00396     {
00397         $iDecPos = 0;
00398         $sValue  = ( string ) $dValue;
00399         $oStr = getStr();
00400         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00401             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00402         }
00403 
00404         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00405         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00406         return number_format( $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00407     }
00408 
00416     public function getLanguageTag( $iLanguage = null)
00417     {
00418         if ( !isset( $iLanguage ) ) {
00419             $iLanguage = $this->getBaseLanguage();
00420         }
00421 
00422         $iLanguage = (int) $iLanguage;
00423 
00424         return ( ( $iLanguage )?"_$iLanguage":"" );
00425     }
00426 
00434     public function validateLanguage( $iLang = null )
00435     {
00436         $iLang = (int) $iLang;
00437 
00438         // checking if this language is valid
00439         $aLanguages = $this->getLanguageArray();
00440 
00441         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00442             $oLang = current( $aLanguages );
00443             $iLang = $oLang->id;
00444         }
00445 
00446         return $iLang;
00447     }
00448 
00456     public function setBaseLanguage( $iLang = null )
00457     {
00458         if ( is_null($iLang) ) {
00459             $iLang = $this->getBaseLanguage();
00460         } else {
00461             $this->_iBaseLanguageId = (int) $iLang;
00462         }
00463 
00464         if ( defined( 'OXID_PHP_UNIT' ) ) {
00465             modSession::getInstance();
00466         }
00467 
00468         oxSession::setVar( 'language', $iLang );
00469     }
00470 
00478     public function setTplLanguage( $iLang = null )
00479     {
00480         if ( is_null($iLang) ) {
00481             $iLang = $this->getTplLanguage();
00482         } else {
00483             $this->_iTplLanguageId = (int) $iLang;
00484         }
00485 
00486         if ( defined( 'OXID_PHP_UNIT' ) ) {
00487             modSession::getInstance();
00488         }
00489 
00490         oxSession::setVar( 'tpllanguage', $iLang );
00491     }
00492 
00501     protected function _recodeLangArray( $aLangArray, $sCharset )
00502     {
00503         foreach ( $aLangArray as $sKey => $sValue ) {
00504             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00505         }
00506 
00507         return $aLangArray;
00508     }
00509 
00518     protected function _getLangFilesPathArray( $blAdmin, $iLang )
00519     {
00520         $aLangFiles = false;
00521         if ( ( $sDir = dirname( $this->getConfig()->getLanguagePath( 'lang.php', $blAdmin, $iLang ) ) ) ) {
00522             //get all lang files
00523             //#M681: content of cust_lang.php should be prefered to lang.php
00524             $aLangFiles = glob( $sDir."/*_lang.php" );
00525             array_unshift( $aLangFiles, $sDir."/lang.php");
00526         }
00527 
00528         return $aLangFiles;
00529     }
00530 
00539     protected function _getLangFileCacheName( $blAdmin, $iLang )
00540     {
00541         $myConfig = $this->getConfig();
00542         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' );
00543     }
00544 
00553     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0 )
00554     {
00555         $myConfig = $this->getConfig();
00556         $myUtils  = oxUtils::getInstance();
00557 
00558         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang );
00559         $aLangCache = $myUtils->getLangCache( $sCacheName );
00560         if ( !$aLangCache && ( $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang ) ) ) {
00561             $aLangCache[$iLang] = array();
00562             $sBaseCharset = false;
00563             foreach ( $aLangFiles as $sLangFile ) {
00564                 require $sLangFile;
00565 
00566                 // including only (!) thoose, which has charset defined
00567                 if ( isset( $aLang['charset'] ) ) {
00568 
00569                     // recoding only in utf
00570                     if ( $myConfig->isUtf() ) {
00571                         $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00572 
00573                         // overriding charset
00574                         $aLang['charset'] = 'UTF-8';
00575                     }
00576 
00577                     if ( !$sBaseCharset ) {
00578                         $sBaseCharset = $aLang['charset'];
00579                     }
00580 
00581                     $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang );
00582                 }
00583             }
00584 
00585             // setting base charset
00586             if ( $sBaseCharset ) {
00587                 $aLangCache[$iLang]['charset'] = $sBaseCharset;
00588             }
00589 
00590             //save to cache
00591             $myUtils->setLangCache( $sCacheName, $aLangCache );
00592         }
00593 
00594         return $aLangCache;
00595     }
00596 
00605     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00606     {
00607         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00608         if ( !isset( $iLang ) ) {
00609             $iLang = $this->getBaseLanguage();
00610             if ( !isset( $iLang ) ) {
00611                 $iLang = 0;
00612             }
00613         }
00614 
00615         return (int) $iLang;
00616     }
00617 
00626     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null )
00627     {
00628         startProfile("<b>_getLangTranslationArray</b>");
00629 
00630         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00631         $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00632 
00633         $aLangCache = $blAdmin ? $this->_aAdminLangCache : $this->_aLangCache;
00634 
00635         if ( !isset( $aLangCache[$iLang] ) ) {
00636 
00637             // loading lang file data
00638             $aLangCache = $this->_getLanguageFileData( $blAdmin, $iLang );
00639             if ( $blAdmin ) {
00640                 $this->_aAdminLangCache = $aLangCache;
00641             } else {
00642                 $this->_aLangCache = $aLangCache;
00643             }
00644         }
00645 
00646         stopProfile("<b>_getLangTranslationArray</b>");
00647 
00648         // if language array exists ..
00649         return ( isset( $aLangCache[$iLang] ) ? $aLangCache[$iLang] : array() );
00650     }
00651 
00661     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00662     {
00663         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00664         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00665         if ( !isset( $iLang ) ) {
00666             $iLang = $this->getBaseLanguage();
00667             if ( !isset( $iLang ) ) {
00668                 $iLang = 0;
00669             }
00670         }
00671 
00672         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00673 
00674         if ( is_file ( $sFileName ) ) {
00675 
00676             static $aUserLangCache = array();
00677 
00678             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00679                 $handle = @fopen( $sFileName, "r" );
00680                 if ( $handle === false ) {
00681                     return $sStringToTranslate;
00682                 }
00683 
00684                 $contents = fread( $handle, filesize ( $sFileName ) );
00685                 fclose( $handle );
00686                 $fileArray = explode( "\n", $contents );
00687                 $aUserLangCache[$sFileName] = array();
00688                 $aLang = &$aUserLangCache[$sFileName];
00689                 $oStr = getStr();
00690 
00691                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00692                     $line = ltrim( $line );
00693                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00694                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00695                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00696                         $aLang[trim($index)] = trim($value);
00697                     }
00698                 }
00699             }
00700 
00701             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00702                 $aLang = &$aUserLangCache[$sFileName];
00703             }
00704 
00705             if ( isset( $aLang[$sStringToTranslate] ) ) {
00706                 return $aLang[$sStringToTranslate];
00707             }
00708         }
00709 
00710         return $sStringToTranslate;
00711     }
00712 
00721     protected function _sortLanguagesCallback( $a1, $a2 )
00722     {
00723         return ($a1->sort > $a2->sort);
00724     }
00725 
00726 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5