00001 <?php
00002
00006 class oxLang extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 protected $_sName = 'lang';
00021
00027 protected $_iBaseLanguageId = null;
00028
00034 protected $_iTplLanguageId = null;
00035
00041 protected $_iEditLanguageId = null;
00042
00048 protected $_aLangCache = array();
00049
00055 protected $_aAdminLangCache = array();
00056
00062 protected $_aAdminTplLanguageArray = null;
00063
00069 protected $_aLangAbbr = null;
00070
00076 public static function getInstance()
00077 {
00078 if ( defined('OXID_PHP_UNIT')) {
00079 if ( ($oClassMod = modInstances::getMod(__CLASS__)) && is_object($oClassMod) ) {
00080 return $oClassMod;
00081 } else {
00082 $inst = oxNew( 'oxLang' );
00083 modInstances::addMod( __CLASS__, $inst );
00084 return $inst;
00085 }
00086 }
00087
00088 if ( !self::$_instance instanceof oxLang ) {
00089
00090 self::$_instance = oxNew( 'oxLang');
00091 }
00092 return self::$_instance;
00093 }
00094
00101 public function resetBaseLanguage()
00102 {
00103 $this->_iBaseLanguageId = null;
00104 }
00105
00111 public function getBaseLanguage()
00112 {
00113 if ( $this->_iBaseLanguageId === null ) {
00114 $myConfig = $this->getConfig();
00115 $blAdmin = $this->isAdmin();
00116
00117
00118 if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00119 $this->_iBaseLanguageId = $iSeLang;
00120 }
00121
00122 if ( is_null( $this->_iBaseLanguageId ) ) {
00123 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00124 }
00125
00126
00127 $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00128
00129 if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00130 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00131 if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00132 $this->_iBaseLanguageId = $iId;
00133 break;
00134 }
00135 }
00136 }
00137
00138 if ( is_null( $this->_iBaseLanguageId ) ) {
00139 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00140 }
00141
00142
00143
00144 if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxUtils::getInstance()->isSearchEngine() ) {
00145
00146
00147 $this->_iBaseLanguageId = oxUtilsServer::getInstance()->getOxCookie( 'language' );
00148
00149
00150 if ( is_null( $this->_iBaseLanguageId ) ) {
00151 $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00152 }
00153 }
00154
00155 if ( is_null( $this->_iBaseLanguageId ) ) {
00156 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00157 }
00158
00159 $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00160
00161
00162 $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00163
00164
00165 oxUtilsServer::getInstance()->setOxCookie( 'language', $this->_iBaseLanguageId );
00166 }
00167 return $this->_iBaseLanguageId;
00168 }
00169
00175 public function getObjectTplLanguage()
00176 {
00177 if ( $this->_iObjectTplLanguageId === null ) {
00178 $this->_iObjectTplLanguageId = $this->getTplLanguage();
00179 $aLanguages = $this->getAdminTplLanguageArray();
00180 if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00181 $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00182 $this->_iObjectTplLanguageId = key( $aLanguages );
00183 }
00184 }
00185 return $this->_iObjectTplLanguageId;
00186 }
00187
00195 public function getTplLanguage()
00196 {
00197 if ( $this->_iTplLanguageId === null ) {
00198 $iSessLang = oxSession::getVar( 'tpllanguage' );
00199 $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00200 }
00201 return $this->_iTplLanguageId;
00202 }
00203
00209 public function getEditLanguage()
00210 {
00211 if ( $this->_iEditLanguageId !== null ) {
00212 return $this->_iEditLanguageId;
00213 }
00214
00215 if ( !$this->isAdmin() ) {
00216 $this->_iEditLanguageId = $this->getBaseLanguage();
00217 } else {
00218
00219 $this->_iEditLanguageId = oxConfig::getParameter( 'editlanguage' );
00220
00221
00222 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00223 $iNewLanguage = oxConfig::getParameter( "new_lang");
00224 }
00225
00226 if ( isset( $iNewLanguage ) ) {
00227 $this->_iEditLanguageId = $iNewLanguage;
00228 oxSession::deleteVar( "new_lang" );
00229 }
00230
00231 if ( is_null( $this->_iEditLanguageId ) ) {
00232
00233 $this->_iEditLanguageId = $this->getBaseLanguage();
00234 }
00235 }
00236
00237
00238 $this->_iEditLanguageId = $this->validateLanguage( $this->_iEditLanguageId );
00239
00240 return $this->_iEditLanguageId;
00241 }
00242
00252 public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00253 {
00254 $myConfig = $this->getConfig();
00255
00256 if ( is_null($iLanguage) ) {
00257 $iLanguage = $this->_iBaseLanguageId;
00258 }
00259
00260 $aLanguages = array();
00261 $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00262 $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00263
00264 if ( is_array( $aConfLanguages ) ) {
00265 $i = 0;
00266 reset( $aConfLanguages );
00267 while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00268
00269 if ( $blOnlyActive && is_array($aLangParams) ) {
00270
00271 if ( !$aLangParams[$key]['active'] ) {
00272 $i++;
00273 continue;
00274 }
00275 }
00276
00277 if ( $val ) {
00278 $oLang = new oxStdClass();
00279 $oLang->id = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00280 $oLang->oxid = $key;
00281 $oLang->abbr = $key;
00282 $oLang->name = $val;
00283
00284 if ( is_array( $aLangParams ) ) {
00285 $oLang->active = $aLangParams[$key]['active'];
00286 $oLang->sort = $aLangParams[$key]['sort'];
00287 }
00288
00289 $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00290 $aLanguages[$oLang->id] = $oLang;
00291 }
00292 ++$i;
00293 }
00294 }
00295
00296 if ( $blSort && is_array($aLangParams) ) {
00297 uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00298 }
00299
00300 return $aLanguages;
00301 }
00302
00308 public function getAdminTplLanguageArray()
00309 {
00310 if ( $this->_aAdminTplLanguageArray === null ) {
00311
00312
00313 $aLangData = array();
00314 $aLangIds = $this->getLanguageIds();
00315
00316 $sSourceDir = $this->getConfig()->getStdLanguagePath( "", true, false );
00317 foreach ( glob( $sSourceDir."*", GLOB_ONLYDIR ) as $sDir ) {
00318 $sFilePath = "{$sDir}/lang.php";
00319 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00320 $sLangName = "";
00321 $sAbbr = strtolower( basename( $sDir ) );
00322 if ( !in_array( $sAbbr, $aLangIds ) ) {
00323 include $sFilePath;
00324 $aLangData[$sAbbr] = new oxStdClass();
00325 $aLangData[$sAbbr]->name = $sLangName;
00326 $aLangData[$sAbbr]->abbr = $sAbbr;
00327 }
00328 }
00329 }
00330
00331 $this->_aAdminTplLanguageArray = $this->getLanguageArray();
00332 if ( count( $aLangData ) ) {
00333
00334
00335 ksort( $aLangData );
00336 $iSort = max( array_keys( $this->_aAdminTplLanguageArray ) );
00337
00338
00339 foreach ( $aLangData as $oLang ) {
00340 $oLang->id = $oLang->sort = ++$iSort;
00341 $oLang->selected = 0;
00342 $oLang->active = 0;
00343 $this->_aAdminTplLanguageArray[$iSort] = $oLang;
00344 }
00345 }
00346 }
00347
00348
00349 reset( $this->_aAdminTplLanguageArray );
00350 return $this->_aAdminTplLanguageArray;
00351 }
00352
00360 public function getLanguageAbbr( $iLanguage = null )
00361 {
00362 if ( $this->_aLangAbbr === null ) {
00363 $this->_aLangAbbr = array();
00364 if ( $this->isAdmin() ) {
00365 foreach ( $this->getAdminTplLanguageArray() as $oLang ) {
00366 $this->_aLangAbbr[$oLang->id] = $oLang->abbr;
00367 }
00368 } else {
00369 $this->_aLangAbbr = $this->getLanguageIds();
00370 }
00371 }
00372
00373 $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00374 if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00375 $iLanguage = $this->_aLangAbbr[$iLanguage];
00376 }
00377
00378 return $iLanguage;
00379 }
00380
00387 public function getLanguageNames()
00388 {
00389 $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00390 $aLangIds = $this->getLanguageIds();
00391 $aLanguages = array();
00392 foreach ( $aLangIds as $iId => $sValue ) {
00393 $aLanguages[$iId] = $aConfLanguages[$sValue];
00394 }
00395 return $aLanguages;
00396 }
00397
00403 public function getLanguageIds()
00404 {
00405 $myConfig = $this->getConfig();
00406 $aIds = array();
00407
00408
00409 $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00410 if ( is_array( $aLangParams ) ) {
00411 foreach ( $aLangParams as $sAbbr => $aValue ) {
00412 $iBaseId = (int) $aValue['baseId'];
00413 $aIds[$iBaseId] = $sAbbr;
00414 }
00415 } else {
00416 $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00417 }
00418
00419 return $aIds;
00420 }
00421
00434 public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00435 {
00436 $aLangCache = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00437 $sText = isset( $aLangCache[$sStringToTranslate] ) ? $aLangCache[$sStringToTranslate] : $sStringToTranslate;
00438
00439 $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00440 if ( !$blIsAdmin && $sText === $sStringToTranslate ) {
00441 $sText = $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00442 }
00443
00444 return $sText;
00445 }
00446
00455 public function formatCurrency( $dValue, $oActCur = null )
00456 {
00457 if ( !$oActCur ) {
00458 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00459 }
00460 return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00461 }
00462
00471 public function formatVat( $dValue, $oActCur = null )
00472 {
00473 $iDecPos = 0;
00474 $sValue = ( string ) $dValue;
00475 $oStr = getStr();
00476 if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00477 $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00478 }
00479
00480 $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00481 $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00482 return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00483 }
00484
00492 public function getLanguageTag( $iLanguage = null)
00493 {
00494 if ( !isset( $iLanguage ) ) {
00495 $iLanguage = $this->getBaseLanguage();
00496 }
00497
00498 $iLanguage = (int) $iLanguage;
00499
00500 return ( ( $iLanguage )?"_$iLanguage":"" );
00501 }
00502
00510 public function validateLanguage( $iLang = null )
00511 {
00512 $iLang = (int) $iLang;
00513
00514
00515 $aLanguages = $this->getLanguageArray();
00516
00517 if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00518 $oLang = current( $aLanguages );
00519 $iLang = $oLang->id;
00520 }
00521
00522 return $iLang;
00523 }
00524
00532 public function setBaseLanguage( $iLang = null )
00533 {
00534 if ( is_null($iLang) ) {
00535 $iLang = $this->getBaseLanguage();
00536 } else {
00537 $this->_iBaseLanguageId = (int) $iLang;
00538 }
00539
00540 if ( defined( 'OXID_PHP_UNIT' ) ) {
00541 modSession::getInstance();
00542 }
00543
00544 oxSession::setVar( 'language', $iLang );
00545 }
00546
00554 public function setTplLanguage( $iLang = null )
00555 {
00556 $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00557 if ( $this->isAdmin() ) {
00558 $aLanguages = $this->getAdminTplLanguageArray();
00559 if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00560 $this->_iTplLanguageId = key( $aLanguages );
00561 }
00562 }
00563
00564 if ( defined( 'OXID_PHP_UNIT' ) ) {
00565 modSession::getInstance();
00566 }
00567
00568 oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00569 return $this->_iTplLanguageId;
00570 }
00571
00580 protected function _recodeLangArray( $aLangArray, $sCharset )
00581 {
00582 foreach ( $aLangArray as $sKey => $sValue ) {
00583 $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00584 }
00585
00586 return $aLangArray;
00587 }
00588
00597 protected function _getLangFilesPathArray( $blAdmin, $iLang )
00598 {
00599 $myConfig = $this->getConfig();
00600 $aLangFiles = array();
00601
00602
00603 $sStdPath = $myConfig->getStdLanguagePath( "", $blAdmin, $iLang );
00604 if ( $sStdPath ) {
00605 $aLangFiles[] = $sStdPath . "lang.php";
00606 $aLangFiles = array_merge( $aLangFiles, glob( $sStdPath."*_lang.php" ) );
00607 }
00608
00609 $sCustPath = $myConfig->getLanguagePath( "", $blAdmin, $iLang );
00610 if ( $sCustPath && $sCustPath != $sStdPath ) {
00611 if ( is_readable( $sCustPath . "lang.php" ) ) {
00612 $aLangFiles[] = $sCustPath . "lang.php";
00613 }
00614 $aLangFiles = array_merge( $aLangFiles, glob( $sCustPath."*_lang.php" ) );
00615 }
00616
00617 return count( $aLangFiles ) ? $aLangFiles : false;
00618 }
00619
00628 protected function _getLangFileCacheName( $blAdmin, $iLang )
00629 {
00630 $myConfig = $this->getConfig();
00631 return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' );
00632 }
00633
00642 protected function _getLanguageFileData( $blAdmin = false, $iLang = 0 )
00643 {
00644 $myConfig = $this->getConfig();
00645 $myUtils = oxUtils::getInstance();
00646
00647 $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang );
00648 $aLangCache = $myUtils->getLangCache( $sCacheName );
00649 if ( !$aLangCache && ( $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang ) ) ) {
00650 $aLangCache[$iLang] = array();
00651 $sBaseCharset = false;
00652 foreach ( $aLangFiles as $sLangFile ) {
00653
00654 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00655 include $sLangFile;
00656
00657
00658 if ( isset( $aLang['charset'] ) ) {
00659
00660
00661 if ( $myConfig->isUtf() ) {
00662 $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00663
00664
00665 $aLang['charset'] = 'UTF-8';
00666 }
00667
00668 if ( !$sBaseCharset ) {
00669 $sBaseCharset = $aLang['charset'];
00670 }
00671
00672 $aLangCache[$iLang] = array_merge( $aLangCache[$iLang], $aLang );
00673 }
00674 }
00675 }
00676
00677
00678 if ( $sBaseCharset ) {
00679 $aLangCache[$iLang]['charset'] = $sBaseCharset;
00680 }
00681
00682
00683 $myUtils->setLangCache( $sCacheName, $aLangCache );
00684 }
00685
00686 return $aLangCache;
00687 }
00688
00697 protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00698 {
00699 $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00700 if ( !isset( $iLang ) ) {
00701 $iLang = $this->getBaseLanguage();
00702 if ( !isset( $iLang ) ) {
00703 $iLang = 0;
00704 }
00705 }
00706
00707 return (int) $iLang;
00708 }
00709
00718 protected function _getLangTranslationArray( $iLang = null, $blAdmin = null )
00719 {
00720 startProfile("_getLangTranslationArray");
00721
00722 $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00723 $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00724 $aLangCache = $blAdmin ? $this->_aAdminLangCache : $this->_aLangCache;
00725 if ( !isset( $aLangCache[$iLang] ) ) {
00726
00727 $aLangCache = $this->_getLanguageFileData( $blAdmin, $iLang );
00728 if ( $blAdmin ) {
00729 $this->_aAdminLangCache = $aLangCache;
00730 } else {
00731 $this->_aLangCache = $aLangCache;
00732 }
00733 }
00734
00735 stopProfile("_getLangTranslationArray");
00736
00737
00738 return ( isset( $aLangCache[$iLang] ) ? $aLangCache[$iLang] : array() );
00739 }
00740
00750 protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00751 {
00752 $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00753 $iLang = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00754 if ( !isset( $iLang ) ) {
00755 $iLang = (int) $this->getBaseLanguage();
00756 }
00757
00758 $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00759 if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
00760
00761 static $aUserLangCache = array();
00762
00763 if ( !isset( $aUserLangCache[$sFileName] ) ) {
00764 $handle = @fopen( $sFileName, "r" );
00765 if ( $handle === false ) {
00766 return $sStringToTranslate;
00767 }
00768
00769 $contents = fread( $handle, filesize ( $sFileName ) );
00770 fclose( $handle );
00771 $fileArray = explode( "\n", $contents );
00772 $aUserLangCache[$sFileName] = array();
00773 $aLang = &$aUserLangCache[$sFileName];
00774 $oStr = getStr();
00775
00776 while ( list( $nr,$line ) = each( $fileArray ) ) {
00777 $line = ltrim( $line );
00778 if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00779 $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00780 $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00781 $aLang[trim($index)] = trim($value);
00782 }
00783 }
00784 }
00785
00786 if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00787 $aLang = &$aUserLangCache[$sFileName];
00788 }
00789
00790 if ( isset( $aLang[$sStringToTranslate] ) ) {
00791 $sStringToTranslate = $aLang[$sStringToTranslate];
00792 }
00793 }
00794
00795 return $sStringToTranslate;
00796 }
00797
00806 protected function _sortLanguagesCallback( $a1, $a2 )
00807 {
00808 return ($a1->sort > $a2->sort);
00809 }
00810
00816 public function getName()
00817 {
00818 return $this->_sName;
00819 }
00820
00826 public function getFormLang()
00827 {
00828 $sLang = null;
00829 if ( !$this->isAdmin()) {
00830 $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
00831 }
00832 return $sLang;
00833 }
00834
00842 public function getUrlLang( $iLang = null )
00843 {
00844 $sLang = null;
00845 if ( !$this->isAdmin()) {
00846 $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00847 $sLang = $this->getName()."=". $iLang;
00848 }
00849 return $sLang;
00850 }
00851
00864 public function processUrl( $sUrl, $iLang = null )
00865 {
00866 $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00867 $oStr = getStr();
00868
00869 if ( !$this->isAdmin() ) {
00870 $sParam = $this->getUrlLang( $iLang );
00871 if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
00872 if ( $sUrl ) {
00873 if ($oStr->strpos( $sUrl, '?') === false) {
00874 $sUrl .= "?";
00875 } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
00876 $sUrl .= "&";
00877 }
00878 }
00879 $sUrl .= $sParam."&";
00880 } else {
00881 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
00882 }
00883 }
00884
00885 return $sUrl;
00886 }
00887
00894 public function detectLanguageByBrowser()
00895 {
00896 $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
00897
00898 if ( !$sBrowserLang ) {
00899 return;
00900 }
00901
00902 $aLangs = $this->getLanguageArray(null, true );
00903
00904 foreach ( $aLangs as $oLang ) {
00905 if ( $oLang->abbr == $sBrowserLang ) {
00906 return (int) $oLang->id;
00907 }
00908 }
00909 }
00910 }