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 = null;
00042 
00048     protected $_aAdminLangCache = null;
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             $this->_iEditLanguageId = oxConfig::getParameter( 'editlanguage' );
00187 
00188             // check if we really need to set the new language
00189             if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00190                 $iNewLanguage = oxConfig::getParameter( "new_lang");
00191             }
00192 
00193             if ( isset( $iNewLanguage ) ) {
00194                 if ( isset( $iNewLanguage ) ) {
00195                     $this->_iEditLanguageId = $iNewLanguage;
00196                     oxSession::deleteVar( "new_lang" );
00197                 }
00198             }
00199 
00200             if ( is_null( $this->_iEditLanguageId ) ) {
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 )
00219     {
00220         $myConfig = oxConfig::getInstance();
00221 
00222         if ( is_null($iLanguage) ) {
00223             $iLanguage = $this->_iBaseLanguageId;
00224         }
00225 
00226         $aLanguages = array();
00227         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00228         if ( is_array( $aConfLanguages ) ) {
00229 
00230             $i = 0;
00231             reset( $aConfLanguages );
00232             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00233                 if ( $val) {
00234                     $oLang = new oxStdClass();
00235                     $oLang->id   = $i;
00236                     $oLang->abbr = $key;
00237                     $oLang->name = $val;
00238                     if ( isset( $iLanguage ) && $i == $iLanguage ) {
00239                         $oLang->selected = 1;
00240                     } else {
00241                         $oLang->selected = 0;
00242                     }
00243                     $aLanguages[$i] = $oLang;
00244                 }
00245                 ++$i;
00246             }
00247         }
00248         return $aLanguages;
00249     }
00250 
00259     public function getLanguageAbbr( $iLanguage = null)
00260     {
00261         $myConfig = oxConfig::getInstance();
00262 
00263         if ( !isset($iLanguage) ) {
00264             $iLanguage = $this->_iBaseLanguageId;
00265         }
00266 
00267         $aLangAbbr = $this->getLanguageIds();
00268 
00269         if ( isset($iLanguage,$aLangAbbr[$iLanguage]) ) {
00270             return $aLangAbbr[$iLanguage];
00271         }
00272 
00273         return $iLanguage;
00274     }
00275 
00282     public function getLanguageNames()
00283     {
00284         return array_values(oxConfig::getInstance()->getConfigParam( 'aLanguages' ));
00285     }
00286 
00292     public function getLanguageIds()
00293     {
00294         return array_keys(oxConfig::getInstance()->getConfigParam( 'aLanguages' ));
00295     }
00296 
00309     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00310     {
00311         $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00312         if ( isset( $aLangCache[$sStringToTranslate] ) ) {
00313             $sText = $aLangCache[$sStringToTranslate];
00314         } else {
00315             $sText = $sStringToTranslate;
00316         }
00317 
00318             $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00319             if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00320                 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00321             }
00322 
00323         return $sText;
00324     }
00325 
00334     public function formatCurrency( $dValue, $oActCur = null )
00335     {
00336         if ( !$oActCur ) {
00337             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00338         }
00339         return number_format( $dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00340     }
00341 
00350     public function formatVat( $dValue, $oActCur = null )
00351     {   
00352         $iDecPos = 0;
00353         $sValue  = ( string ) $dValue;
00354         if ( ( $iDotPos = strpos( $sValue, '.' ) ) !== false ) {
00355             $iDecPos = strlen( substr( $sValue, $iDotPos + 1 ) );
00356         }
00357 
00358         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00359         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;          
00360         return number_format( $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00361     }
00362 
00370     public function getLanguageTag( $iLanguage = null)
00371     {
00372         if ( !isset( $iLanguage ) ) {
00373             $iLanguage = $this->getBaseLanguage();
00374         }
00375 
00376         $iLanguage = (int) $iLanguage;
00377 
00378         return ( ( $iLanguage )?"_$iLanguage":"" );
00379     }
00380 
00389     protected function _getLangTranslationArray( $iLang = null, $blAdminMode = null )
00390     {
00391         startProfile("_getLangTranslationArray");
00392         $myConfig = $this->getConfig();
00393         $sFileName = '';
00394         $sCustFileName = '';
00395 
00396         $blAdminMode = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00397 
00398         $iLang  = ( $iLang === null )?oxSession::getVar( 'blAdminTemplateLanguage' ):$iLang;
00399         if ( !isset( $iLang ) ) {
00400             $iLang = $this->getBaseLanguage();
00401             if ( !isset( $iLang ) ) {
00402                 $iLang = 0;
00403             }
00404         }
00405 
00406         if ( $blAdminMode ) {
00407             $aLangCache = $this->_aAdminLangCache;
00408         } else {
00409             $aLangCache = $this->_aLangCache;
00410         }
00411 
00412         // casting for security reasons
00413         $iLang = (int) $iLang;
00414         if ( !$aLangCache[$iLang] ) {
00415 
00416             /*
00417             $sFileName     = $myConfig->getLanguagePath('lang.php', $blAdminMode,$iLang);
00418             $sCustFileName = $myConfig->getLanguagePath('cust_lang.php', $blAdminMode,$iLang);
00419             */
00420             $sCacheName = "languagefiles_".$blAdminMode."_".$iLang."_".$this->getConfig()->getShopId();
00421             $aLangFiles = oxUtils::getInstance()->fromFileCache($sCacheName);
00422             if (!$aLangFiles) {
00423                 $sDir = dirname($myConfig->getLanguagePath('lang.php', $blAdminMode, $iLang));
00424 
00425                 //get all lang files
00426                 $aLangFiles = glob($sDir."/*lang.php");
00427 
00428                 //save to cache
00429                 oxUtils::getInstance()->toFileCache($sCacheName, $aLangFiles);
00430             }
00431 
00432             $aLangCache[$iLang] = array();
00433             foreach ($aLangFiles as $sLangFile) {
00434                 require $sLangFile;
00435                 $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang);
00436             }
00437 
00438 
00439             //build lang array
00440             /*
00441             if ( is_file( $sFileName ) ) {
00442                 require $sFileName;
00443                 $aLangCache[$iLang] = $aLang;
00444             }
00445 
00446             if ( is_file( $sCustFileName ) ) {
00447                 require $sCustFileName;
00448                 $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang);
00449             }*/
00450 
00451             if ( $blAdminMode ) {
00452                 $this->_aAdminLangCache = $aLangCache;
00453             } else {
00454                 $this->_aLangCache = $aLangCache;
00455             }
00456         }
00457 
00458 
00459         stopProfile("_getLangTranslationArray");
00460 
00461         // if language array exists ..
00462         if ( isset( $aLangCache[$iLang] ) ) {
00463             return $aLangCache[$iLang];
00464         } else {
00465             return array();
00466         }
00467     }
00468 
00478     protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00479     {
00480         $iLang  = ( $iLang === null )?oxSession::getVar( 'blAdminTemplateLanguage' ):$iLang;
00481         if ( !isset( $iLang ) ) {
00482             $iLang = $this->getBaseLanguage();
00483             if ( !isset( $iLang ) ) {
00484                 $iLang = 0;
00485             }
00486         }
00487 
00488         $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00489         $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00490 
00491         if ( is_file ( $sFileName ) ) {
00492 
00493             static $aUserLangCache = array();
00494 
00495             if ( !isset( $aUserLangCache[$sFileName] ) ) {
00496                 $handle = @fopen( $sFileName, "r" );
00497                 if ( $handle === false ) {
00498                     return $sStringToTranslate;
00499                 }
00500 
00501                 $contents = fread( $handle, filesize ( $sFileName ) );
00502                 fclose( $handle );
00503                 $fileArray = explode( "\n", $contents );
00504                 $aUserLangCache[$sFileName] = array();
00505                 $aLang = &$aUserLangCache[$sFileName];
00506 
00507                 while ( list( $nr,$line ) = each( $fileArray ) ) {
00508                     $line = ltrim( $line );
00509                     if ( $line[0]!="#" and strpos( $line, "=" ) > 0 ) {
00510                         $index = trim( substr( $line, 0, strpos($line, "=" ) ) );
00511                         $value = trim( substr( $line, strpos( $line, "=" ) + 1, strlen( $line ) ) );
00512                         $aLang[trim($index)] = trim($value);
00513                     }
00514                 }
00515             }
00516 
00517             if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00518                 $aLang = &$aUserLangCache[$sFileName];
00519             }
00520 
00521             if ( isset( $aLang[$sStringToTranslate] ) ) {
00522                 return $aLang[$sStringToTranslate];
00523             }
00524         }
00525 
00526         return $sStringToTranslate;
00527     }
00528 
00536     public function validateLanguage( $iLang = null )
00537     {
00538         $iLang = (int) $iLang;
00539 
00540         // checking if this language is valid
00541         $aLanguages = $this->getLanguageArray();
00542 
00543         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00544             $oLang = current( $aLanguages );
00545             $iLang = $oLang->id;
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         if ( is_null($iLang) ) {
00583             $iLang = $this->getTplLanguage();
00584         } else {
00585             $this->_iTplLanguageId = (int) $iLang;
00586         }
00587 
00588         if ( defined( 'OXID_PHP_UNIT' ) ) {
00589             modSession::getInstance();
00590         }
00591 
00592         oxSession::setVar( 'tpllanguage', $iLang );
00593     }
00594 
00595 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5