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 $_aAdminTplLanguageArray = null;
00056
00062 protected $_aLangAbbr = null;
00063
00069 protected $_aAdditionalLangFiles = array();
00070
00076 protected $_aLangMap = array();
00077
00078
00084 public static function getInstance()
00085 {
00086 if ( defined('OXID_PHP_UNIT')) {
00087 if ( ($oClassMod = modInstances::getMod(__CLASS__)) && is_object($oClassMod) ) {
00088 return $oClassMod;
00089 } else {
00090 $inst = oxNew( 'oxLang' );
00091 modInstances::addMod( __CLASS__, $inst );
00092 return $inst;
00093 }
00094 }
00095
00096 if ( !self::$_instance instanceof oxLang ) {
00097
00098 self::$_instance = oxNew( 'oxLang');
00099 }
00100 return self::$_instance;
00101 }
00102
00109 public function resetBaseLanguage()
00110 {
00111 $this->_iBaseLanguageId = null;
00112 }
00113
00119 public function getBaseLanguage()
00120 {
00121 if ( $this->_iBaseLanguageId === null ) {
00122
00123 $myConfig = $this->getConfig();
00124 $blAdmin = $this->isAdmin();
00125
00126
00127 if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00128 $this->_iBaseLanguageId = $iSeLang;
00129 }
00130
00131 if ( is_null( $this->_iBaseLanguageId ) ) {
00132 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00133 }
00134
00135
00136 $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00137
00138 if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00139 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00140 if ( $myConfig->isCurrentUrl( $sUrl ) ) {
00141 $this->_iBaseLanguageId = $iId;
00142 break;
00143 }
00144 }
00145 }
00146
00147 if ( is_null( $this->_iBaseLanguageId ) ) {
00148 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00149 if (!isset($this->_iBaseLanguageId)) {
00150 $this->_iBaseLanguageId = oxSession::getVar('language');
00151 }
00152 }
00153
00154
00155
00156 if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxUtils::getInstance()->isSearchEngine() ) {
00157
00158
00159 $this->_iBaseLanguageId = oxUtilsServer::getInstance()->getOxCookie( 'language' );
00160
00161
00162 if ( is_null( $this->_iBaseLanguageId ) ) {
00163 $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00164 }
00165 }
00166
00167 if ( is_null( $this->_iBaseLanguageId ) ) {
00168 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00169 }
00170
00171 $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00172
00173
00174 $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00175
00176
00177 oxUtilsServer::getInstance()->setOxCookie( 'language', $this->_iBaseLanguageId );
00178 }
00179
00180 return $this->_iBaseLanguageId;
00181 }
00182
00188 public function getObjectTplLanguage()
00189 {
00190 if ( $this->_iObjectTplLanguageId === null ) {
00191 $this->_iObjectTplLanguageId = $this->getTplLanguage();
00192 $aLanguages = $this->getAdminTplLanguageArray();
00193 if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00194 $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00195 $this->_iObjectTplLanguageId = key( $aLanguages );
00196 }
00197 }
00198 return $this->_iObjectTplLanguageId;
00199 }
00200
00208 public function getTplLanguage()
00209 {
00210 if ( $this->_iTplLanguageId === null ) {
00211 $iSessLang = oxSession::getVar( 'tpllanguage' );
00212 $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00213 }
00214 return $this->_iTplLanguageId;
00215 }
00216
00222 public function getEditLanguage()
00223 {
00224 if ( $this->_iEditLanguageId === null ) {
00225
00226 if ( !$this->isAdmin() ) {
00227 $this->_iEditLanguageId = $this->getBaseLanguage();
00228 } else {
00229
00230
00231
00232 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00233 $iLang = oxConfig::getParameter( "new_lang");
00234 }
00235 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00236 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00237 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00238
00239
00240 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00241
00242
00243 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00244 }
00245 }
00246 return $this->_iEditLanguageId;
00247 }
00248
00258 public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00259 {
00260 $myConfig = $this->getConfig();
00261
00262 if ( is_null($iLanguage) ) {
00263 $iLanguage = $this->_iBaseLanguageId;
00264 }
00265
00266 $aLanguages = array();
00267 $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00268 $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00269
00270 if ( is_array( $aConfLanguages ) ) {
00271 $i = 0;
00272 reset( $aConfLanguages );
00273 while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00274
00275 if ( $blOnlyActive && is_array($aLangParams) ) {
00276
00277 if ( !$aLangParams[$key]['active'] ) {
00278 $i++;
00279 continue;
00280 }
00281 }
00282
00283 if ( $val ) {
00284 $oLang = new oxStdClass();
00285 $oLang->id = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00286 $oLang->oxid = $key;
00287 $oLang->abbr = $key;
00288 $oLang->name = $val;
00289
00290 if ( is_array( $aLangParams ) ) {
00291 $oLang->active = $aLangParams[$key]['active'];
00292 $oLang->sort = $aLangParams[$key]['sort'];
00293 }
00294
00295 $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00296 $aLanguages[$oLang->id] = $oLang;
00297 }
00298 ++$i;
00299 }
00300 }
00301
00302 if ( $blSort && is_array($aLangParams) ) {
00303 uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00304 }
00305
00306
00307 return $aLanguages;
00308 }
00309
00315 public function getAdminTplLanguageArray()
00316 {
00317 if ( $this->_aAdminTplLanguageArray === null ) {
00318
00319
00320 $aLangData = array();
00321 $aLangIds = $this->getLanguageIds();
00322
00323 $sSourceDir = $this->getConfig()->getStdLanguagePath( "", true, false );
00324 foreach ( glob( $sSourceDir."*", GLOB_ONLYDIR ) as $sDir ) {
00325 $sFilePath = "{$sDir}/lang.php";
00326 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00327 $sLangName = "";
00328 $sAbbr = strtolower( basename( $sDir ) );
00329 if ( !in_array( $sAbbr, $aLangIds ) ) {
00330 include $sFilePath;
00331 $aLangData[$sAbbr] = new oxStdClass();
00332 $aLangData[$sAbbr]->name = $sLangName;
00333 $aLangData[$sAbbr]->abbr = $sAbbr;
00334 }
00335 }
00336 }
00337
00338 $this->_aAdminTplLanguageArray = $this->getLanguageArray();
00339 if ( count( $aLangData ) ) {
00340
00341
00342 ksort( $aLangData );
00343 $iSort = max( array_keys( $this->_aAdminTplLanguageArray ) );
00344
00345
00346 foreach ( $aLangData as $oLang ) {
00347 $oLang->id = $oLang->sort = ++$iSort;
00348 $oLang->selected = 0;
00349 $oLang->active = 0;
00350 $this->_aAdminTplLanguageArray[$iSort] = $oLang;
00351 }
00352 }
00353 }
00354
00355
00356 reset( $this->_aAdminTplLanguageArray );
00357 return $this->_aAdminTplLanguageArray;
00358 }
00359
00367 public function getLanguageAbbr( $iLanguage = null )
00368 {
00369 if ( $this->_aLangAbbr === null ) {
00370 $this->_aLangAbbr = array();
00371 if ( $this->isAdmin() ) {
00372 foreach ( $this->getAdminTplLanguageArray() as $oLang ) {
00373 $this->_aLangAbbr[$oLang->id] = $oLang->abbr;
00374 }
00375 } else {
00376 $this->_aLangAbbr = $this->getLanguageIds();
00377 }
00378 }
00379
00380 $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00381 if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00382 $iLanguage = $this->_aLangAbbr[$iLanguage];
00383 }
00384
00385 return $iLanguage;
00386 }
00387
00394 public function getLanguageNames()
00395 {
00396 $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00397 $aLangIds = $this->getLanguageIds();
00398 $aLanguages = array();
00399 foreach ( $aLangIds as $iId => $sValue ) {
00400 $aLanguages[$iId] = $aConfLanguages[$sValue];
00401 }
00402 return $aLanguages;
00403 }
00404
00410 public function getLanguageIds()
00411 {
00412 $myConfig = $this->getConfig();
00413 $aIds = array();
00414
00415
00416 $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
00417 if ( is_array( $aLangParams ) ) {
00418 foreach ( $aLangParams as $sAbbr => $aValue ) {
00419 $iBaseId = (int) $aValue['baseId'];
00420 $aIds[$iBaseId] = $sAbbr;
00421 }
00422 } else {
00423 $aIds = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00424 }
00425
00426 return $aIds;
00427 }
00428
00436 public function registerAdditionalLangFile($sFile)
00437 {
00438 if (!$sFile || !is_readable($sFile)) {
00439 $oErr = new oxFileException('EXCEPTION_FILENOTFOUND');
00440 $oErr->setFileName($sFile);
00441 throw $oErr;
00442 }
00443 $this->_aAdditionalLangFiles[] = $sFile;
00444 }
00445
00458 public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00459 {
00460 $myConfig = $this->getConfig();
00461
00462
00463 $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00464 if ( isset( $aLang[$sStringToTranslate] ) ) {
00465 return $aLang[$sStringToTranslate];
00466 }
00467
00468
00469 $aMap = $this->_getLanguageMap( $iLang );
00470 if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00471 return $aLang[$aMap[$sStringToTranslate]];
00472 }
00473
00474
00475 if (count($this->_aAdditionalLangFiles)) {
00476 $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00477 if (isset( $aLang[$sStringToTranslate] )) {
00478 return $aLang[$sStringToTranslate];
00479 }
00480 }
00481
00482
00483 $blIsAdmin = isset( $blAdminMode ) ? $blAdminMode : $this->isAdmin();
00484 if ( !$blIsAdmin ) {
00485 return $this->_readTranslateStrFromTextFile( $sStringToTranslate, $iLang, $blIsAdmin );
00486 }
00487
00488 return $sStringToTranslate;
00489 }
00490
00499 public function formatCurrency( $dValue, $oActCur = null )
00500 {
00501 if ( !$oActCur ) {
00502 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00503 }
00504 return number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00505 }
00506
00515 public function formatVat( $dValue, $oActCur = null )
00516 {
00517 $iDecPos = 0;
00518 $sValue = ( string ) $dValue;
00519 $oStr = getStr();
00520 if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00521 $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00522 }
00523
00524 $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00525 $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00526 return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00527 }
00528
00536 public function getLanguageTag( $iLanguage = null)
00537 {
00538 if ( !isset( $iLanguage ) ) {
00539 $iLanguage = $this->getBaseLanguage();
00540 }
00541
00542 $iLanguage = (int) $iLanguage;
00543
00544 return ( ( $iLanguage )?"_$iLanguage":"" );
00545 }
00546
00554 public function validateLanguage( $iLang = null )
00555 {
00556 $iLang = (int) $iLang;
00557
00558
00559 $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00560 if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00561 $oLang = current( $aLanguages );
00562 if (isset($oLang->id)) {
00563 $iLang = $oLang->id;
00564 }
00565 }
00566
00567 return $iLang;
00568 }
00569
00577 public function setBaseLanguage( $iLang = null )
00578 {
00579 if ( is_null($iLang) ) {
00580 $iLang = $this->getBaseLanguage();
00581 } else {
00582 $this->_iBaseLanguageId = (int) $iLang;
00583 }
00584
00585 if ( defined( 'OXID_PHP_UNIT' ) ) {
00586 modSession::getInstance();
00587 }
00588
00589 oxSession::setVar( 'language', $iLang );
00590 }
00591
00599 public function setTplLanguage( $iLang = null )
00600 {
00601 $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00602 if ( $this->isAdmin() ) {
00603 $aLanguages = $this->getAdminTplLanguageArray();
00604 if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00605 $this->_iTplLanguageId = key( $aLanguages );
00606 }
00607 }
00608
00609 if ( defined( 'OXID_PHP_UNIT' ) ) {
00610 modSession::getInstance();
00611 }
00612
00613 oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00614 return $this->_iTplLanguageId;
00615 }
00616
00625 protected function _recodeLangArray( $aLangArray, $sCharset )
00626 {
00627 foreach ( $aLangArray as $sKey => $sValue ) {
00628 $aLangArray[$sKey] = iconv( $sCharset, 'UTF-8', $sValue );
00629 }
00630
00631 return $aLangArray;
00632 }
00633
00642 protected function _getLangFilesPathArray( $blAdmin, $iLang )
00643 {
00644 $myConfig = $this->getConfig();
00645 $aLangFiles = array();
00646
00647 $sOutDir = $myConfig->getOutDir();
00648 $sLang = oxLang::getInstance()->getLanguageAbbr( $iLang );
00649 $sTheme = $myConfig->getConfigParam( "sTheme" );
00650 $sCustomTheme = $myConfig->getConfigParam( "sCustomTheme" );
00651 $sShopId = $myConfig->getShopId();
00652
00653 if ( $blAdmin ) {
00654
00655 $sAdminPath = $sOutDir . 'admin/' . $sLang . '/';
00656 $aLangFiles[] = $sAdminPath . "lang.php";
00657 $aLangFiles = array_merge( $aLangFiles, glob( $sAdminPath."*_lang.php" ) );
00658
00659 return count( $aLangFiles ) ? $aLangFiles : false;
00660 }
00661
00662
00663 $sGenericPath = $sOutDir . $sLang . '/';
00664 if ( $sGenericPath ) {
00665 $aLangFiles[] = $sGenericPath . "lang.php";
00666 $aLangFiles = array_merge( $aLangFiles, glob( $sGenericPath."*_lang.php" ) );
00667 }
00668
00669
00670 if ( $sTheme ) {
00671 $sThemePath = $sOutDir . $sTheme .'/' . $sLang . '/';
00672 $aLangFiles[] = $sThemePath . "lang.php";
00673 $aLangFiles = array_merge( $aLangFiles, glob( $sThemePath."*_lang.php" ) );
00674 }
00675
00676
00677
00678 if ( $sCustomTheme) {
00679 $sCustPath = $sOutDir . $sCustomTheme .'/' . $sLang . '/';
00680 $aLangFiles[] = $sCustPath . "lang.php";
00681 $aLangFiles = array_merge( $aLangFiles, glob( $sCustPath."*_lang.php" ) );
00682
00683
00684 if ( $sCustomTheme ) {
00685 $sShopPath = $sOutDir . $sCustomTheme .'/' . $sShopId . '/' . $sLang . '/';
00686 $aLangFiles[] = $sShopPath . "lang.php";
00687 $aLangFiles = array_merge( $aLangFiles, glob( $sShopPath."*_lang.php" ) );
00688 }
00689 } else {
00690
00691 if ( $sTheme ) {
00692 $sShopPath = $sOutDir . $sTheme .'/' . $sShopId . '/' . $sLang . '/';
00693 $aLangFiles[] = $sShopPath . "lang.php";
00694 $aLangFiles = array_merge( $aLangFiles, glob( $sShopPath."*_lang.php" ) );
00695 }
00696 }
00697
00698
00699 $aModuleFiles = glob(getShopBasePath().'/modules/*/out/lang/' . $sLang . '/*_lang.php');
00700 if (is_array($aModuleFiles) && count($aModuleFiles)) {
00701 $aLangFiles = array_merge( $aLangFiles, $aModuleFiles );
00702 }
00703
00704 return count( $aLangFiles ) ? $aLangFiles : false;
00705 }
00706
00716 protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00717 {
00718 $myConfig = $this->getConfig();
00719 $sLangFilesIdent = '_default';
00720 if (is_array($aLangFiles) && $aLangFiles) {
00721 $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00722 }
00723 return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00724 }
00725
00735 protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00736 {
00737 $myConfig = $this->getConfig();
00738 $myUtils = oxUtils::getInstance();
00739
00740 $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00741 $aLangCache = $myUtils->getLangCache( $sCacheName );
00742 if ( !$aLangCache && $aLangFiles === null ) {
00743 $aLangFiles = $this->_getLangFilesPathArray( $blAdmin, $iLang );
00744 }
00745 if ( !$aLangCache && $aLangFiles ) {
00746 $aLangCache = array();
00747 $sBaseCharset = false;
00748 foreach ( $aLangFiles as $sLangFile ) {
00749
00750 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00751 include $sLangFile;
00752
00753
00754 if ( isset( $aLang['charset'] ) ) {
00755
00756
00757 if ( $myConfig->isUtf() ) {
00758 $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00759
00760
00761 $aLang['charset'] = 'UTF-8';
00762 }
00763
00764 if ( !$sBaseCharset ) {
00765 $sBaseCharset = $aLang['charset'];
00766 }
00767
00768 $aLangCache = array_merge( $aLangCache, $aLang );
00769 }
00770 }
00771 }
00772
00773
00774 if ( $sBaseCharset ) {
00775 $aLangCache['charset'] = $sBaseCharset;
00776 }
00777
00778
00779 $myUtils->setLangCache( $sCacheName, $aLangCache );
00780 }
00781
00782 return $aLangCache;
00783 }
00784
00792 protected function _getLanguageMap( $iLang )
00793 {
00794 if ( !isset($this->_aLangMap[$iLang]) ) {
00795 $this->_aLangMap[$iLang] = array();
00796 $myConfig = $this->getConfig();
00797 $sMapFile = $myConfig->getOutDir() . '/' . $myConfig->getConfigParam( "sTheme" ) .'/' . oxLang::getInstance()->getLanguageAbbr( $iLang ) . '/map.php';
00798 if ( $sMapFile ) {
00799 if ( file_exists( $sMapFile ) && is_readable( $sMapFile ) ) {
00800 include $sMapFile;
00801 $this->_aLangMap[$iLang] = $aMap;
00802 }
00803 }
00804 }
00805
00806 return $this->_aLangMap[$iLang];
00807 }
00808
00817 protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00818 {
00819 $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00820 if ( !isset( $iLang ) ) {
00821 $iLang = $this->getBaseLanguage();
00822 if ( !isset( $iLang ) ) {
00823 $iLang = 0;
00824 }
00825 }
00826
00827 return (int) $iLang;
00828 }
00829
00839 protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00840 {
00841 startProfile("_getLangTranslationArray");
00842
00843 $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00844 $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
00845 $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00846
00847 if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00848 $this->_aLangCache[$sCacheName] = array();
00849 }
00850 if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00851
00852 $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00853 }
00854
00855 stopProfile("_getLangTranslationArray");
00856
00857
00858 return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00859 }
00860
00872 protected function _readTranslateStrFromTextFile( $sStringToTranslate, $iLang = null, $blIsAdmin = null )
00873 {
00874 $blIsAdmin = isset( $blIsAdmin ) ? $blIsAdmin : $this->isAdmin();
00875 $iLang = ( $iLang === null && $blIsAdmin)?$this->getTplLanguage():$iLang;
00876 if ( !isset( $iLang ) ) {
00877 $iLang = (int) $this->getBaseLanguage();
00878 }
00879
00880 $sFileName = $this->getConfig()->getLanguagePath('lang.txt', $blIsAdmin, $iLang);
00881 if ( is_file ( $sFileName ) && is_readable( $sFileName ) ) {
00882
00883 static $aUserLangCache = array();
00884
00885 if ( !isset( $aUserLangCache[$sFileName] ) ) {
00886 $handle = @fopen( $sFileName, "r" );
00887 if ( $handle === false ) {
00888 return $sStringToTranslate;
00889 }
00890
00891 $contents = fread( $handle, filesize ( $sFileName ) );
00892 fclose( $handle );
00893 $fileArray = explode( "\n", $contents );
00894 $aUserLangCache[$sFileName] = array();
00895 $aLang = &$aUserLangCache[$sFileName];
00896 $oStr = getStr();
00897
00898 while ( list( $nr,$line ) = each( $fileArray ) ) {
00899 $line = ltrim( $line );
00900 if ( $line[0]!="#" && $oStr->strpos( $line, "=" ) > 0 ) {
00901 $index = trim( $oStr->substr( $line, 0, $oStr->strpos($line, "=" ) ) );
00902 $value = trim( $oStr->substr( $line, $oStr->strpos( $line, "=" ) + 1, $oStr->strlen( $line ) ) );
00903 $aLang[trim($index)] = trim($value);
00904 }
00905 }
00906 }
00907
00908 if ( !isset( $aLang ) && isset( $aUserLangCache[$sFileName] ) ) {
00909 $aLang = &$aUserLangCache[$sFileName];
00910 }
00911
00912 if ( isset( $aLang[$sStringToTranslate] ) ) {
00913 $sStringToTranslate = $aLang[$sStringToTranslate];
00914 }
00915 }
00916
00917 return $sStringToTranslate;
00918 }
00919
00928 protected function _sortLanguagesCallback( $a1, $a2 )
00929 {
00930 return ($a1->sort > $a2->sort);
00931 }
00932
00938 public function getName()
00939 {
00940 return $this->_sName;
00941 }
00942
00948 public function getFormLang()
00949 {
00950 $sLang = null;
00951 if ( !$this->isAdmin()) {
00952 $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\">";
00953 }
00954 return $sLang;
00955 }
00956
00964 public function getUrlLang( $iLang = null )
00965 {
00966 $sLang = null;
00967 if ( !$this->isAdmin()) {
00968 $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00969 $sLang = $this->getName()."=". $iLang;
00970 }
00971 return $sLang;
00972 }
00973
00986 public function processUrl( $sUrl, $iLang = null )
00987 {
00988 $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
00989 $oStr = getStr();
00990
00991 if ( !$this->isAdmin() ) {
00992 $sParam = $this->getUrlLang( $iLang );
00993 if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) && ($iLang != oxConfig::getInstance()->getConfigParam( 'sDefaultLang' ))) {
00994 if ( $sUrl ) {
00995 if ($oStr->strpos( $sUrl, '?') === false) {
00996 $sUrl .= "?";
00997 } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
00998 $sUrl .= "&";
00999 }
01000 }
01001 $sUrl .= $sParam."&";
01002 } else {
01003 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01004 }
01005 }
01006
01007 return $sUrl;
01008 }
01009
01016 public function detectLanguageByBrowser()
01017 {
01018 $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01019
01020 if ( !$sBrowserLang ) {
01021 return;
01022 }
01023
01024 $aLangs = $this->getLanguageArray(null, true );
01025
01026 foreach ( $aLangs as $oLang ) {
01027 if ( $oLang->abbr == $sBrowserLang ) {
01028 return (int) $oLang->id;
01029 }
01030 }
01031 }
01032 }