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
00094
00095 if ( $this->_iBaseLanguageId !== null ) {
00096 return $this->_iBaseLanguageId;
00097 }
00098
00099 $blAdmin = $this->isAdmin();
00100
00101
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
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
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
00155
00156 if ( is_null( $this->_iTplLanguageId ) ) {
00157
00158 $this->_iTplLanguageId = oxSession::getVar( 'tpllanguage' );
00159 }
00160
00161 if ( is_null( $this->_iTplLanguageId ) ) {
00162 $this->_iTplLanguageId = $this->getBaseLanguage();
00163 }
00164 }
00165
00166
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
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
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
00239 if ( !$aLangParams[$key]['active'] ) {
00240 continue;
00241 }
00242 }
00243
00244 if ( $val) {
00245 $oLang = new oxStdClass();
00246 if ( isset($aLangParams[$key]['baseId']) ) {
00247 $oLang->id = $aLangParams[$key]['baseId'];
00248 } else {
00249 $oLang->id = $i;
00250 }
00251 $oLang->oxid = $key;
00252 $oLang->abbr = $key;
00253 $oLang->name = $val;
00254
00255 if ( is_array($aLangParams) ) {
00256 $oLang->active = $aLangParams[$key]['active'];
00257 $oLang->sort = $aLangParams[$key]['sort'];
00258 }
00259
00260 if ( isset( $iLanguage ) && $i == $iLanguage ) {
00261 $oLang->selected = 1;
00262 } else {
00263 $oLang->selected = 0;
00264 }
00265 $aLanguages[$oLang->id] = $oLang;
00266 }
00267 ++$i;
00268 }
00269 }
00270
00271 if ( $blSort && is_array($aLangParams) ) {
00272 uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00273 }
00274
00275 return $aLanguages;
00276 }
00277
00286 public function getLanguageAbbr( $iLanguage = null)
00287 {
00288 $myConfig = $this->getConfig();
00289
00290 if ( !isset($iLanguage) ) {
00291 $iLanguage = $this->_iBaseLanguageId;
00292 }
00293
00294 $aLangAbbr = $this->getLanguageIds();
00295
00296 if ( isset($iLanguage,$aLangAbbr[$iLanguage]) ) {
00297 return $aLangAbbr[$iLanguage];
00298 }
00299
00300 return $iLanguage;
00301 }
00302
00309 public function getLanguageNames()
00310 {
00311 return array_values( $this->getConfig()->getConfigParam( 'aLanguages' ));
00312 }
00313
00319 public function getLanguageIds()
00320 {
00321 $aLangParams = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00322
00323
00324 if ( is_array($aLangParams) ) {
00325
00326 $aIds = array();
00327
00328 foreach ( $aLangParams as $sAbbr => $aValue ) {
00329 $iBaseId = (int) $aValue['baseId'];
00330 $aIds[$iBaseId] = $sAbbr;
00331 }
00332 return $aIds;
00333 }
00334
00335 return array_keys( $this->getConfig()->getConfigParam( 'aLanguages' ));
00336 }
00337
00350 public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00351 {
00352 $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00353 $sText = isset( $aLangCache[$sStringToTranslate] ) ? $aLangCache[$sStringToTranslate] : $sStringToTranslate;
00354
00355 $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00356 if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00357 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00358 }
00359
00360 return $sText;
00361 }
00362
00371 public function formatCurrency( $dValue, $oActCur = null )
00372 {
00373 if ( !$oActCur ) {
00374 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00375 }
00376 return number_format( $dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00377 }
00378
00387 public function formatVat( $dValue, $oActCur = null )
00388 {
00389 $iDecPos = 0;
00390 $sValue = ( string ) $dValue;
00391 $oStr = getStr();
00392 if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00393 $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00394 }
00395
00396 $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00397 $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00398 return number_format( $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00399 }
00400
00408 public function getLanguageTag( $iLanguage = null)
00409 {
00410 if ( !isset( $iLanguage ) ) {
00411 $iLanguage = $this->getBaseLanguage();
00412 }
00413
00414 $iLanguage = (int) $iLanguage;
00415
00416 return ( ( $iLanguage )?"_$iLanguage":"" );
00417 }
00418
00426 public function validateLanguage( $iLang = null )
00427 {
00428 $iLang = (int) $iLang;
00429
00430
00431 $aLanguages = $this->getLanguageArray();
00432
00433 if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00434 $oLang = current( $aLanguages );
00435 $iLang = $oLang->id;
00436 }
00437
00438 return $iLang;
00439 }
00440
00448 public function setBaseLanguage( $iLang = null )
00449 {
00450 if ( is_null($iLang) ) {
00451 $iLang = $this->getBaseLanguage();
00452 } else {
00453 $this->_iBaseLanguageId = (int) $iLang;
00454 }
00455
00456 if ( defined( 'OXID_PHP_UNIT' ) ) {
00457 modSession::getInstance();
00458 }
00459
00460 oxSession::setVar( 'language', $iLang );
00461 }
00462
00470 public function setTplLanguage( $iLang = null )
00471 {
00472 if ( is_null($iLang) ) {
00473 $iLang = $this->getTplLanguage();
00474 } else {
00475 $this->_iTplLanguageId = (int) $iLang;
00476 }
00477
00478 if ( defined( 'OXID_PHP_UNIT' ) ) {
00479 modSession::getInstance();
00480 }
00481
00482 oxSession::setVar( 'tpllanguage', $iLang );
00483 }
00484
00493 protected function _recodeLangArray( $aLangArray, $sCharset )
00494 {
00495 foreach ( $aLangArray as $sKey => $sValue ) {
00496 $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00497 }
00498
00499 return $aLangArray;
00500 }
00501
00510 protected function _getLanguageFileData( $blAdmin = false, $iLang = 0 )
00511 {
00512 $myConfig = $this->getConfig();
00513 $myUtils = oxUtils::getInstance();
00514
00515 $sCacheName = "langcache_".( (int) $blAdmin )."_{$iLang}_".$myConfig->getShopId();
00516 $aLangCache = $myUtils->getLangCache( $sCacheName );
00517 if ( !$aLangCache ) {
00518 $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', $blAdmin, $iLang ) );
00519
00520
00521
00522 $aLangFiles = glob( $sDir."/*_lang.php" );
00523 array_unshift($aLangFiles, $sDir."/lang.php");
00524
00525 $aLangCache[$iLang] = array();
00526 foreach ( $aLangFiles as $sLangFile ) {
00527 require $sLangFile;
00528
00529
00530 if ( isset( $aLang['charset'] ) ) {
00531
00532
00533 if ( $myConfig->isUtf() ) {
00534 $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00535
00536
00537 $aLang['charset'] = 'UTF-8';
00538 }
00539
00540 $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang );
00541 }
00542 }
00543
00544
00545 $myUtils->setLangCache( $sCacheName, $aLangCache );
00546 }
00547
00548 return $aLangCache;
00549 }
00550
00559 protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00560 {
00561 $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00562 if ( !isset( $iLang ) ) {
00563 $iLang = $this->getBaseLanguage();
00564 if ( !isset( $iLang ) ) {
00565 $iLang = 0;
00566 }
00567 }
00568
00569 return (int) $iLang;
00570 }
00571
00580 protected function _getLangTranslationArray( $iLang = null, $blAdmin = null )
00581 {
00582 startProfile("<b>_getLangTranslationArray</b>");
00583
00584 $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00585 $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00586
00587 $aLangCache = $blAdmin ? $this->_aAdminLangCache : $this->_aLangCache;
00588
00589 if ( !isset( $aLangCache[$iLang] ) ) {
00590
00591
00592 $aLangCache = $this->_getLanguageFileData( $blAdmin, $iLang );
00593 if ( $blAdmin ) {
00594 $this->_aAdminLangCache = $aLangCache;
00595 } else {
00596 $this->_aLangCache = $aLangCache;
00597 }
00598 }
00599
00600 stopProfile("<b>_getLangTranslationArray</b>");
00601
00602
00603 return ( isset( $aLangCache[$iLang] ) ? $aLangCache[$iLang] : array() );
00604 }
00605
00615 protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00616 {
00617 $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00618 $iLang = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00619 if ( !isset( $iLang ) ) {
00620 $iLang = $this->getBaseLanguage();
00621 if ( !isset( $iLang ) ) {
00622 $iLang = 0;
00623 }
00624 }
00625
00626 $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00627
00628 if ( is_file ( $sFileName ) ) {
00629
00630 static $aUserLangCache = array();
00631
00632 if ( !isset( $aUserLangCache[$sFileName] ) ) {
00633 $handle = @fopen( $sFileName, "r" );
00634 if ( $handle === false ) {
00635 return $sStringToTranslate;
00636 }
00637
00638 $contents = fread( $handle, filesize ( $sFileName ) );
00639 fclose( $handle );
00640 $fileArray = explode( "\n", $contents );
00641 $aUserLangCache[$sFileName] = array();
00642 $aLang = &$aUserLangCache[$sFileName];
00643 $oStr = getStr();
00644
00645 while ( list( $nr,$line ) = each( $fileArray ) ) {
00646 $line = ltrim( $line );
00647 if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00648 $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00649 $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00650 $aLang[trim($index)] = trim($value);
00651 }
00652 }
00653 }
00654
00655 if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00656 $aLang = &$aUserLangCache[$sFileName];
00657 }
00658
00659 if ( isset( $aLang[$sStringToTranslate] ) ) {
00660 return $aLang[$sStringToTranslate];
00661 }
00662 }
00663
00664 return $sStringToTranslate;
00665 }
00666
00675 protected function _sortLanguagesCallback( $a1, $a2 )
00676 {
00677 return ($a1->sort > $a2->sort);
00678 }
00679
00680 }