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 ) && $i == $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         return array_values( $this->getConfig()->getConfigParam( 'aLanguages' ));
00313     }
00314 
00320     public function getLanguageIds()
00321     {
00322         $aLangParams = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00323 
00324         //if exists language parameters array, extract lang id's from there
00325         if ( is_array($aLangParams) ) {
00326 
00327             $aIds = array();
00328 
00329             foreach ( $aLangParams as $sAbbr => $aValue ) {
00330                 $iBaseId = (int) $aValue['baseId'];
00331                 $aIds[$iBaseId] = $sAbbr;
00332             }
00333             return $aIds;
00334         }
00335 
00336         return array_keys( $this->getConfig()->getConfigParam( 'aLanguages' ));
00337     }
00338 
00351     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00352     {
00353         $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00354         $sText = isset( $aLangCache[$sStringToTranslate] ) ? $aLangCache[$sStringToTranslate] : $sStringToTranslate;
00355 
00356             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00357             if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00358                 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00359             }
00360 
00361         return $sText;
00362     }
00363 
00372     public function formatCurrency( $dValue, $oActCur = null )
00373     {
00374         if ( !$oActCur ) {
00375             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00376         }
00377         return number_format( $dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00378     }
00379 
00388     public function formatVat( $dValue, $oActCur = null )
00389     {
00390         $iDecPos = 0;
00391         $sValue  = ( string ) $dValue;
00392         $oStr = getStr();
00393         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00394             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00395         }
00396 
00397         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00398         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00399         return number_format( $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00400     }
00401 
00409     public function getLanguageTag( $iLanguage = null)
00410     {
00411         if ( !isset( $iLanguage ) ) {
00412             $iLanguage = $this->getBaseLanguage();
00413         }
00414 
00415         $iLanguage = (int) $iLanguage;
00416 
00417         return ( ( $iLanguage )?"_$iLanguage":"" );
00418     }
00419 
00427     public function validateLanguage( $iLang = null )
00428     {
00429         $iLang = (int) $iLang;
00430 
00431         // checking if this language is valid
00432         $aLanguages = $this->getLanguageArray();
00433 
00434         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00435             $oLang = current( $aLanguages );
00436             $iLang = $oLang->id;
00437         }
00438 
00439         return $iLang;
00440     }
00441 
00449     public function setBaseLanguage( $iLang = null )
00450     {
00451         if ( is_null($iLang) ) {
00452             $iLang = $this->getBaseLanguage();
00453         } else {
00454             $this->_iBaseLanguageId = (int) $iLang;
00455         }
00456 
00457         if ( defined( 'OXID_PHP_UNIT' ) ) {
00458             modSession::getInstance();
00459         }
00460 
00461         oxSession::setVar( 'language', $iLang );
00462     }
00463 
00471     public function setTplLanguage( $iLang = null )
00472     {
00473         if ( is_null($iLang) ) {
00474             $iLang = $this->getTplLanguage();
00475         } else {
00476             $this->_iTplLanguageId = (int) $iLang;
00477         }
00478 
00479         if ( defined( 'OXID_PHP_UNIT' ) ) {
00480             modSession::getInstance();
00481         }
00482 
00483         oxSession::setVar( 'tpllanguage', $iLang );
00484     }
00485 
00494     protected function _recodeLangArray( $aLangArray, $sCharset )
00495     {
00496         foreach ( $aLangArray as $sKey => $sValue ) {
00497             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00498         }
00499 
00500         return $aLangArray;
00501     }
00502 
00511     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0 )
00512     {
00513         $myConfig = $this->getConfig();
00514         $myUtils  = oxUtils::getInstance();
00515 
00516         $sCacheName = "langcache_".( (int) $blAdmin )."_{$iLang}_".$myConfig->getShopId();
00517         $aLangCache = $myUtils->getLangCache( $sCacheName );
00518         if ( !$aLangCache ) {
00519             $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', $blAdmin, $iLang ) );
00520 
00521             //get all lang files
00522             //#M681: content of cust_lang.php should be prefered to lang.php
00523             $aLangFiles = glob( $sDir."/*_lang.php" );
00524             array_unshift($aLangFiles, $sDir."/lang.php");
00525 
00526             $aLangCache[$iLang] = array();
00527             if (!$sDir) {
00528                 return array();
00529             }
00530             foreach ( $aLangFiles as $sLangFile ) {
00531                 require $sLangFile;
00532 
00533                 // inclyding only (!) thoose, which has charset defined
00534                 if ( isset( $aLang['charset'] ) ) {
00535 
00536                     // recoding only in utf
00537                     if ( $myConfig->isUtf() ) {
00538                         $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00539 
00540                         // overriding charset
00541                         $aLang['charset'] = 'UTF-8';
00542                     }
00543 
00544                     $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang );
00545                 }
00546             }
00547 
00548             //save to cache
00549             $myUtils->setLangCache( $sCacheName, $aLangCache );
00550         }
00551 
00552         return $aLangCache;
00553     }
00554 
00563     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00564     {
00565         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00566         if ( !isset( $iLang ) ) {
00567             $iLang = $this->getBaseLanguage();
00568             if ( !isset( $iLang ) ) {
00569                 $iLang = 0;
00570             }
00571         }
00572 
00573         return (int) $iLang;
00574     }
00575 
00584     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null )
00585     {
00586         startProfile("<b>_getLangTranslationArray</b>");
00587 
00588         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00589         $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00590 
00591         $aLangCache = $blAdmin ? $this->_aAdminLangCache : $this->_aLangCache;
00592 
00593         if ( !isset( $aLangCache[$iLang] ) ) {
00594 
00595             // loading lang file data
00596             $aLangCache = $this->_getLanguageFileData( $blAdmin, $iLang );
00597             if ( $blAdmin ) {
00598                 $this->_aAdminLangCache = $aLangCache;
00599             } else {
00600                 $this->_aLangCache = $aLangCache;
00601             }
00602         }
00603 
00604         stopProfile("<b>_getLangTranslationArray</b>");
00605 
00606         // if language array exists ..
00607         return ( isset( $aLangCache[$iLang] ) ? $aLangCache[$iLang] : array() );
00608     }
00609 
00619     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00620     {
00621         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00622         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00623         if ( !isset( $iLang ) ) {
00624             $iLang = $this->getBaseLanguage();
00625             if ( !isset( $iLang ) ) {
00626                 $iLang = 0;
00627             }
00628         }
00629 
00630         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00631 
00632         if ( is_file ( $sFileName ) ) {
00633 
00634             static $aUserLangCache = array();
00635 
00636             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00637                 $handle = @fopen( $sFileName, "r" );
00638                 if ( $handle === false ) {
00639                     return $sStringToTranslate;
00640                 }
00641 
00642                 $contents = fread( $handle, filesize ( $sFileName ) );
00643                 fclose( $handle );
00644                 $fileArray = explode( "\n", $contents );
00645                 $aUserLangCache[$sFileName] = array();
00646                 $aLang = &$aUserLangCache[$sFileName];
00647                 $oStr = getStr();
00648 
00649                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00650                     $line = ltrim( $line );
00651                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00652                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00653                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00654                         $aLang[trim($index)] = trim($value);
00655                     }
00656                 }
00657             }
00658 
00659             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00660                 $aLang = &$aUserLangCache[$sFileName];
00661             }
00662 
00663             if ( isset( $aLang[$sStringToTranslate] ) ) {
00664                 return $aLang[$sStringToTranslate];
00665             }
00666         }
00667 
00668         return $sStringToTranslate;
00669     }
00670 
00679     protected function _sortLanguagesCallback( $a1, $a2 )
00680     {
00681         return ($a1->sort > $a2->sort);
00682     }
00683 
00684 }

Generated on Tue Aug 18 09:21:05 2009 for OXID eShop CE by  doxygen 1.5.5