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 
00218     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00219     {
00220         $myConfig = $this->getConfig();
00221 
00222         if ( is_null($iLanguage) ) {
00223             $iLanguage = $this->_iBaseLanguageId;
00224         }
00225 
00226         $aLanguages = array();
00227         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00228         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00229 
00230         if ( is_array( $aConfLanguages ) ) {
00231             $i = 0;
00232             reset( $aConfLanguages );
00233             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00234 
00235                 if ( $blOnlyActive && is_array($aLangParams) ) {
00236                     //skipping non active languages
00237                     if ( !$aLangParams[$key]['active'] ) {
00238                         continue;
00239                     }
00240                 }
00241 
00242                 if ( $val) {
00243                     $oLang = new oxStdClass();
00244                     if ( isset($aLangParams[$key]['baseId']) ) {
00245                         $oLang->id  = $aLangParams[$key]['baseId'];
00246                     } else {
00247                         $oLang->id  = $i;
00248                     }
00249                     $oLang->oxid    = $key;
00250                     $oLang->abbr    = $key;
00251                     $oLang->name    = $val;
00252 
00253                     if ( is_array($aLangParams) ) {
00254                         $oLang->active  = $aLangParams[$key]['active'];
00255                         $oLang->sort   = $aLangParams[$key]['sort'];
00256                     }
00257 
00258                     if ( isset( $iLanguage ) && $i == $iLanguage ) {
00259                         $oLang->selected = 1;
00260                     } else {
00261                         $oLang->selected = 0;
00262                     }
00263                     $aLanguages[$oLang->id] = $oLang;
00264                 }
00265                 ++$i;
00266             }
00267         }
00268 
00269         if ( $blSort && is_array($aLangParams) ) {
00270             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00271         }
00272 
00273         return $aLanguages;
00274     }
00275 
00284     public function getLanguageAbbr( $iLanguage = null)
00285     {
00286         $myConfig = $this->getConfig();
00287 
00288         if ( !isset($iLanguage) ) {
00289             $iLanguage = $this->_iBaseLanguageId;
00290         }
00291 
00292         $aLangAbbr = $this->getLanguageIds();
00293 
00294         if ( isset($iLanguage,$aLangAbbr[$iLanguage]) ) {
00295             return $aLangAbbr[$iLanguage];
00296         }
00297 
00298         return $iLanguage;
00299     }
00300 
00307     public function getLanguageNames()
00308     {
00309         return array_values( $this->getConfig()->getConfigParam( 'aLanguages' ));
00310     }
00311 
00317     public function getLanguageIds()
00318     {
00319         $aLangParams = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00320 
00321         //if exists language parameters array, extract lang id's from there
00322         if ( is_array($aLangParams) ) {
00323 
00324             $aIds = array();
00325 
00326             foreach ( $aLangParams as $sAbbr => $aValue ) {
00327                 $iBaseId = (int)$aValue['baseId'];
00328                 $aIds[$iBaseId] = $sAbbr;
00329             }
00330             return $aIds;
00331         }
00332 
00333         return array_keys( $this->getConfig()->getConfigParam( 'aLanguages' ));
00334     }
00335 
00348     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00349     {
00350         $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00351         $sText = isset( $aLangCache[$sStringToTranslate] ) ? $aLangCache[$sStringToTranslate] : $sStringToTranslate;
00352 
00353             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00354             if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00355                 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00356             }
00357 
00358         return $sText;
00359     }
00360 
00369     public function formatCurrency( $dValue, $oActCur = null )
00370     {
00371         if ( !$oActCur ) {
00372             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00373         }
00374         return number_format( $dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00375     }
00376 
00385     public function formatVat( $dValue, $oActCur = null )
00386     {
00387         $iDecPos = 0;
00388         $sValue  = ( string ) $dValue;
00389         $oStr = getStr();
00390         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00391             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00392         }
00393 
00394         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00395         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00396         return number_format( $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00397     }
00398 
00406     public function getLanguageTag( $iLanguage = null)
00407     {
00408         if ( !isset( $iLanguage ) ) {
00409             $iLanguage = $this->getBaseLanguage();
00410         }
00411 
00412         $iLanguage = (int) $iLanguage;
00413 
00414         return ( ( $iLanguage )?"_$iLanguage":"" );
00415     }
00416 
00425     protected function _recodeLangArray( $aLangArray, $sCharset )
00426     {
00427         foreach ( $aLangArray as $sKey => $sValue ) {
00428             $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00429         }
00430 
00431         return $aLangArray;
00432     }
00433 
00442     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0 )
00443     {
00444         $myConfig = $this->getConfig();
00445         $myUtils  = oxUtils::getInstance();
00446 
00447         $sCacheName = "langcache_".( (int) $blAdmin )."_{$iLang}_".$myConfig->getShopId();
00448         $aLangCache = $myUtils->getLangCache( $sCacheName );
00449         if ( !$aLangCache ) {
00450             $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', $blAdmin, $iLang ) );
00451 
00452             //get all lang files
00453             //#M681: content of cust_lang.php should be prefered to lang.php
00454             $aLangFiles = glob( $sDir."/*_lang.php" );
00455             array_unshift($aLangFiles, $sDir."/lang.php");
00456 
00457             $aLangCache[$iLang] = array();
00458             foreach ( $aLangFiles as $sLangFile ) {
00459                 require $sLangFile;
00460 
00461                 // inclyding only (!) thoose, which has charset defined
00462                 if ( isset( $aLang['charset'] ) ) {
00463 
00464                     // recoding only in utf
00465                     if ( $myConfig->isUtf() ) {
00466                         $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00467 
00468                         // overriding charset
00469                         $aLang['charset'] = 'UTF-8';
00470                     }
00471 
00472                     $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang );
00473                 }
00474             }
00475 
00476             //save to cache
00477             $myUtils->setLangCache( $sCacheName, $aLangCache );
00478         }
00479 
00480         return $aLangCache;
00481     }
00482 
00491     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00492     {
00493         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00494         if ( !isset( $iLang ) ) {
00495             $iLang = $this->getBaseLanguage();
00496             if ( !isset( $iLang ) ) {
00497                 $iLang = 0;
00498             }
00499         }
00500 
00501         return (int) $iLang;
00502     }
00503 
00512     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null )
00513     {
00514         startProfile("<b>_getLangTranslationArray</b>");
00515 
00516         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00517         $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00518 
00519         $aLangCache = $blAdmin ? $this->_aAdminLangCache : $this->_aLangCache;
00520 
00521         if ( !isset( $aLangCache[$iLang] ) ) {
00522 
00523             // loading lang file data
00524             $aLangCache = $this->_getLanguageFileData( $blAdmin, $iLang );
00525             if ( $blAdmin ) {
00526                 $this->_aAdminLangCache = $aLangCache;
00527             } else {
00528                 $this->_aLangCache = $aLangCache;
00529             }
00530         }
00531 
00532         stopProfile("<b>_getLangTranslationArray</b>");
00533 
00534         // if language array exists ..
00535         return ( isset( $aLangCache[$iLang] ) ? $aLangCache[$iLang] : array() );
00536     }
00537 
00547     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00548     {
00549         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00550         $iLang  = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00551         if ( !isset( $iLang ) ) {
00552             $iLang = $this->getBaseLanguage();
00553             if ( !isset( $iLang ) ) {
00554                 $iLang = 0;
00555             }
00556         }
00557 
00558         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00559 
00560         if ( is_file ( $sFileName ) ) {
00561 
00562             static $aUserLangCache = array();
00563 
00564             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00565                 $handle = @fopen( $sFileName, "r" );
00566                 if ( $handle === false ) {
00567                     return $sStringToTranslate;
00568                 }
00569 
00570                 $contents = fread( $handle, filesize ( $sFileName ) );
00571                 fclose( $handle );
00572                 $fileArray = explode( "\n", $contents );
00573                 $aUserLangCache[$sFileName] = array();
00574                 $aLang = &$aUserLangCache[$sFileName];
00575                 $oStr = getStr();
00576 
00577                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00578                     $line = ltrim( $line );
00579                     if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00580                         $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00581                         $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00582                         $aLang[trim($index)] = trim($value);
00583                     }
00584                 }
00585             }
00586 
00587             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00588                 $aLang = &$aUserLangCache[$sFileName];
00589             }
00590 
00591             if ( isset( $aLang[$sStringToTranslate] ) ) {
00592                 return $aLang[$sStringToTranslate];
00593             }
00594         }
00595 
00596         return $sStringToTranslate;
00597     }
00598 
00606     public function validateLanguage( $iLang = null )
00607     {
00608         $iLang = (int) $iLang;
00609 
00610         // checking if this language is valid
00611         $aLanguages = $this->getLanguageArray();
00612 
00613         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00614             $oLang = current( $aLanguages );
00615             $iLang = $oLang->id;
00616         }
00617 
00618         return $iLang;
00619     }
00620 
00628     public function setBaseLanguage( $iLang = null )
00629     {
00630         if ( is_null($iLang) ) {
00631             $iLang = $this->getBaseLanguage();
00632         } else {
00633             $this->_iBaseLanguageId = (int) $iLang;
00634         }
00635 
00636         if ( defined( 'OXID_PHP_UNIT' ) ) {
00637             modSession::getInstance();
00638         }
00639 
00640         oxSession::setVar( 'language', $iLang );
00641     }
00642 
00650     public function setTplLanguage( $iLang = null )
00651     {
00652         if ( is_null($iLang) ) {
00653             $iLang = $this->getTplLanguage();
00654         } else {
00655             $this->_iTplLanguageId = (int) $iLang;
00656         }
00657 
00658         if ( defined( 'OXID_PHP_UNIT' ) ) {
00659             modSession::getInstance();
00660         }
00661 
00662         oxSession::setVar( 'tpllanguage', $iLang );
00663     }
00664 
00665     protected function _sortLanguagesCallback( $a1, $a2 )
00666     {
00667         return ($a1->sort > $a2->sort);
00668     }
00669 
00670 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5