OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxlang.php
Go to the documentation of this file.
1 <?php
2 
6 class oxLang extends oxSuperCfg
7 {
13  private static $_instance = null;
14 
20  protected $_sName = 'lang';
21 
27  protected $_iBaseLanguageId = null;
28 
34  protected $_iTplLanguageId = null;
35 
41  protected $_iEditLanguageId = null;
42 
48  protected $_aLangCache = array();
49 
55  protected $_aAdminTplLanguageArray = null;
56 
62  protected $_aLangAbbr = null;
63 
69  protected $_aAdditionalLangFiles = array();
70 
76  protected $_aLangMap = array();
77 
83  protected $_aActiveModuleInfo = null;
84 
90  protected $_aDisabledModuleInfo = null;
91 
99  public static function getInstance()
100  {
101  return oxRegistry::getLang();
102  }
103 
110  public function resetBaseLanguage()
111  {
112  $this->_iBaseLanguageId = null;
113  }
114 
120  public function getBaseLanguage()
121  {
122  if ( $this->_iBaseLanguageId === null ) {
123 
124  $myConfig = $this->getConfig();
125  $blAdmin = $this->isAdmin();
126 
127  // languages and search engines
128  if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
129  $this->_iBaseLanguageId = $iSeLang;
130  }
131 
132  if ( is_null( $this->_iBaseLanguageId ) ) {
133  $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
134  }
135 
136  //or determining by domain
137  $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
138 
139  if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
140  foreach ( $aLanguageUrls as $iId => $sUrl ) {
141  if ( $sUrl && $myConfig->isCurrentUrl( $sUrl ) ) {
142  $this->_iBaseLanguageId = $iId;
143  break;
144  }
145  }
146  }
147 
148  if ( is_null( $this->_iBaseLanguageId ) ) {
149  $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
150  if (!isset($this->_iBaseLanguageId)) {
151  $this->_iBaseLanguageId = oxSession::getVar('language');
152  }
153  }
154 
155  // if language still not set and not search engine browsing,
156  // getting language from browser
157  if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxRegistry::getUtils()->isSearchEngine() ) {
158 
159  // getting from cookie
160  $this->_iBaseLanguageId = oxRegistry::get("oxUtilsServer")->getOxCookie( 'language' );
161 
162  // getting from browser
163  if ( is_null( $this->_iBaseLanguageId ) ) {
164  $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
165  }
166  }
167 
168  if ( is_null( $this->_iBaseLanguageId ) ) {
169  $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
170  }
171 
172  $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
173 
174  // validating language
175  $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
176 
177  oxRegistry::get("oxUtilsServer")->setOxCookie( 'language', $this->_iBaseLanguageId );
178  }
179 
181  }
182 
188  public function getObjectTplLanguage()
189  {
190  if ( $this->_iObjectTplLanguageId === null ) {
191  $this->_iObjectTplLanguageId = $this->getTplLanguage();
193  if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
194  $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
195  $this->_iObjectTplLanguageId = key( $aLanguages );
196  }
197  }
198  return $this->_iObjectTplLanguageId;
199  }
200 
208  public function getTplLanguage()
209  {
210  if ( $this->_iTplLanguageId === null ) {
211  $iSessLang = oxSession::getVar( 'tpllanguage' );
212  $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
213  }
214  return $this->_iTplLanguageId;
215  }
216 
222  public function getEditLanguage()
223  {
224  if ( $this->_iEditLanguageId === null ) {
225 
226  if ( !$this->isAdmin() ) {
227  $this->_iEditLanguageId = $this->getBaseLanguage();
228  } else {
229 
230  $iLang = null;
231  // choosing language ident
232  // check if we really need to set the new language
233  if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
234  $iLang = oxConfig::getParameter( "new_lang");
235  }
236  $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
237  $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
238  $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
239 
240  // validating language
241  $this->_iEditLanguageId = $this->validateLanguage( $iLang );
242 
243  // writing to session
244  oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
245  }
246  }
248  }
249 
259  public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
260  {
261  $myConfig = $this->getConfig();
262 
263  if ( is_null($iLanguage) ) {
264  $iLanguage = $this->_iBaseLanguageId;
265  }
266 
267  $aLanguages = array();
268  $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
269  $aLangParams = $myConfig->getConfigParam( 'aLanguageParams' );
270 
271  if ( is_array( $aConfLanguages ) ) {
272  $i = 0;
273  reset( $aConfLanguages );
274  while ( list( $key, $val ) = each( $aConfLanguages ) ) {
275 
276  if ( $blOnlyActive && is_array($aLangParams) ) {
277  //skipping non active languages
278  if ( !$aLangParams[$key]['active'] ) {
279  $i++;
280  continue;
281  }
282  }
283 
284  if ( $val ) {
285  $oLang = new stdClass();
286  $oLang->id = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
287  $oLang->oxid = $key;
288  $oLang->abbr = $key;
289  $oLang->name = $val;
290 
291  if ( is_array( $aLangParams ) ) {
292  $oLang->active = $aLangParams[$key]['active'];
293  $oLang->sort = $aLangParams[$key]['sort'];
294  }
295 
296  $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
297  $aLanguages[$oLang->id] = $oLang;
298  }
299  ++$i;
300  }
301  }
302 
303  if ( $blSort && is_array($aLangParams) ) {
304  uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
305  }
306  return $aLanguages;
307  }
308 
314  public function getAdminTplLanguageArray()
315  {
316  if ( $this->_aAdminTplLanguageArray === null ) {
317  $myConfig = $this->getConfig();
318 
319  $aLangArray = $this->getLanguageArray();
320  $this->_aAdminTplLanguageArray = array();
321 
322  $sSourceDir = $myConfig->getAppDir() . 'views/admin/';
323  foreach ( $aLangArray as $iLangKey => $oLang ) {
324  $sFilePath = "{$sSourceDir}{$oLang->abbr}/lang.php";
325  if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
326  $this->_aAdminTplLanguageArray[$iLangKey] = $oLang;
327  }
328  }
329  }
330 
331  // moving pointer to beginning
332  reset( $this->_aAdminTplLanguageArray );
334  }
335 
343  public function getLanguageAbbr( $iLanguage = null )
344  {
345  if ( $this->_aLangAbbr === null ) {
346  $this->_aLangAbbr = $this->getLanguageIds();
347  }
348 
349  $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
350  if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
351  $iLanguage = $this->_aLangAbbr[$iLanguage];
352  }
353 
354  return $iLanguage;
355  }
356 
363  public function getLanguageNames()
364  {
365  $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
366  $aLangIds = $this->getLanguageIds();
367  $aLanguages = array();
368  foreach ( $aLangIds as $iId => $sValue ) {
369  $aLanguages[$iId] = $aConfLanguages[$sValue];
370  }
371  return $aLanguages;
372  }
373 
386  public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
387  {
388  // checking if in cache exist
389  $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
390  if ( isset( $aLang[$sStringToTranslate] ) ) {
391  return $aLang[$sStringToTranslate];
392  }
393 
394  // checking if in map exist
395  $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
396  if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
397  return $aLang[$aMap[$sStringToTranslate]];
398  }
399 
400  // checking if in theme options exist
401  if (count($this->_aAdditionalLangFiles)) {
402  $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
403  if (isset( $aLang[$sStringToTranslate] )) {
404  return $aLang[$sStringToTranslate];
405  }
406  }
407 
408  return $sStringToTranslate;
409  }
410 
421  protected function _collectSimilar( $aData, $sKey, $aCollection = array() )
422  {
423  foreach ( $aData as $sValKey => $sValue ) {
424  if ( strpos( $sValKey, $sKey ) === 0 ) {
425  $aCollection[$sValKey] = $sValue;
426  }
427  }
428 
429  return $aCollection;
430  }
431 
442  public function getSimilarByKey( $sKey, $iLang = null, $blAdmin = null )
443  {
444  startProfile("getSimilarByKey");
445 
446  $iLang = isset( $iLang ) ? $iLang : $this->getTplLanguage();
447  $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
448 
449  // checking if exists in cache
450  $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin );
451  $aSimilarConst = $this->_collectSimilar( $aLang, $sKey );
452 
453  // checking if in map exist
454  $aMap = $this->_getLanguageMap( $iLang, $blAdmin );
455  $aSimilarConst = $this->_collectSimilar( $aMap, $sKey, $aSimilarConst );
456 
457  // checking if in theme options exist
458  if ( count( $this->_aAdditionalLangFiles ) ) {
459  $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin, $this->_aAdditionalLangFiles);
460  $aSimilarConst = $this->_collectSimilar( $aLang, $sKey, $aSimilarConst );
461  }
462 
463  stopProfile("getSimilarByKey");
464 
465  return $aSimilarConst;
466  }
467 
476  public function formatCurrency( $dValue, $oActCur = null )
477  {
478  if ( !$oActCur ) {
479  $oActCur = $this->getConfig()->getActShopCurrencyObject();
480  }
481  $sValue = oxRegistry::getUtils()->fRound( $dValue, $oActCur );
482  return number_format( (double)$sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
483  }
484 
493  public function formatVat( $dValue, $oActCur = null )
494  {
495  $iDecPos = 0;
496  $sValue = ( string ) $dValue;
497  $oStr = getStr();
498  if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
499  $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
500  }
501 
502  $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
503  $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
504  return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
505  }
506 
514  public function getLanguageTag( $iLanguage = null)
515  {
516  if ( !isset( $iLanguage ) ) {
517  $iLanguage = $this->getBaseLanguage();
518  }
519 
520  $iLanguage = (int) $iLanguage;
521 
522  return ( ( $iLanguage )?"_$iLanguage":"" );
523  }
524 
532  public function validateLanguage( $iLang = null )
533  {
534  $iLang = (int) $iLang;
535 
536  // checking if this language is valid
537  $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
538  if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
539  $oLang = current( $aLanguages );
540  if (isset($oLang->id)) {
541  $iLang = $oLang->id;
542  }
543  }
544 
545  return $iLang;
546  }
547 
555  public function setBaseLanguage( $iLang = null )
556  {
557  if ( is_null($iLang) ) {
558  $iLang = $this->getBaseLanguage();
559  } else {
560  $this->_iBaseLanguageId = (int) $iLang;
561  }
562 
563  if ( defined( 'OXID_PHP_UNIT' ) ) {
565  }
566 
567  oxSession::setVar( 'language', $iLang );
568  }
569 
577  public function setTplLanguage( $iLang = null )
578  {
579  $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
580  if ( $this->isAdmin() ) {
582  if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
583  $this->_iTplLanguageId = key( $aLanguages );
584  }
585  }
586 
587  if ( defined( 'OXID_PHP_UNIT' ) ) {
589  }
590 
591  oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
592  return $this->_iTplLanguageId;
593  }
594 
604  protected function _recodeLangArray( $aLangArray, $sCharset, $blRecodeKeys = false )
605  {
606  $aLangs = array();
607  foreach ( $aLangArray as $sKey => $sValue ) {
608  $sItemKey = $sKey;
609  if ($blRecodeKeys === true) {
610  $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
611  }
612 
613  $aLangs[$sItemKey] = iconv( $sCharset, 'UTF-8', $sValue );
614  unset($aLangArray[$sKey]);
615  }
616 
617  return $aLangs;
618  }
619 
627  protected function _getLangFilesPathArray( $iLang )
628  {
629  $oConfig = $this->getConfig();
630  $aLangFiles = array();
631 
632  $sAppDir = $oConfig->getAppDir();
633  $sLang = oxRegistry::getLang()->getLanguageAbbr( $iLang );
634  $sTheme = $oConfig->getConfigParam( "sTheme" );
635  $sCustomTheme = $oConfig->getConfigParam( "sCustomTheme" );
636  $sShopId = $oConfig->getShopId();
637  $aModulePaths = $this->_getActiveModuleInfo();
638 
639  //get generic lang files
640  $sGenericPath = $sAppDir . 'translations/' . $sLang;
641  if ( $sGenericPath ) {
642  $aLangFiles[] = $sGenericPath . "/lang.php";
643  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sGenericPath );
644  }
645 
646  //get theme lang files
647  if ( $sTheme ) {
648  $sThemePath = $sAppDir . 'views/' . $sTheme .'/' . $sLang;
649  $aLangFiles[] = $sThemePath . "/lang.php";
650  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath );
651  }
652 
653  //get custom theme lang files
654  if ( $sCustomTheme ) {
655  $sCustPath = $sAppDir . 'views/' . $sCustomTheme .'/' . $sLang;
656  $aLangFiles[] = $sCustPath . "/lang.php";
657  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sCustPath );
658  }
659 
660 
661  // custom theme shop languages
662 
663  // modules language files
664  $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang );
665 
666  // custom language files
667  $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang );
668 
669  return count( $aLangFiles ) ? $aLangFiles : false;
670  }
671 
679  protected function _getAdminLangFilesPathArray( $iLang )
680  {
681  $oConfig = $this->getConfig();
682  $aLangFiles = array();
683 
684  $sAppDir = $oConfig->getAppDir();
685  $sLang = oxRegistry::getLang()->getLanguageAbbr( $iLang );
686 
687  $aModulePaths = array();
688  $aModulePaths = array_merge( $aModulePaths, $this->_getActiveModuleInfo() );
689  $aModulePaths = array_merge( $aModulePaths, $this->_getDisabledModuleInfo() );
690 
691  // admin lang files
692  $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
693  $aLangFiles[] = $sAdminPath . "/lang.php";
694  $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
695  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sAdminPath );
696 
697  // themes options lang files
698  $sThemePath = $sAppDir . 'views/*/' . $sLang;
699  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath, "options" );
700 
701  // module language files
702  $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, true );
703 
704  // custom language files
705  $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang, true );
706 
707  return count( $aLangFiles ) ? $aLangFiles : false;
708  }
709 
719  protected function _appendLangFile( $aLangFiles, $sFullPath, $sFilePattern = "lang" )
720  {
721  $aFiles = glob( $sFullPath . "/*_{$sFilePattern}.php" );
722  if ( is_array( $aFiles ) && count( $aFiles ) ) {
723  foreach ( $aFiles as $sFile ) {
724  if ( !strpos( $sFile, 'cust_lang.php' ) ) {
725  $aLangFiles[] = $sFile;
726  }
727  }
728  }
729  return $aLangFiles;
730  }
731 
741  protected function _appendCustomLangFiles( $aLangFiles, $sLang, $blForAdmin = false )
742  {
743  $oConfig = $this->getConfig();
744  $sAppDir = $oConfig->getAppDir();
745  $sTheme = $oConfig->getConfigParam( "sTheme" );
746  $sCustomTheme = $oConfig->getConfigParam( "sCustomTheme" );
747 
748  if ( $blForAdmin ) {
749  $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
750  } else {
751  if ( $sTheme ) {
752  $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
753  }
754  if ( $sCustomTheme ) {
755  $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
756  }
757  }
758 
759  return $aLangFiles;
760  }
761 
772  protected function _appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, $blForAdmin = false )
773  {
774  if ( is_array( $aModulePaths ) ) {
775 
776  $oConfig = $this->getConfig();
777 
778  foreach ( $aModulePaths as $sPath ) {
779  $sFullPath = $oConfig->getModulesDir() . $sPath;
780  $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
781  $sFullPath .= $sLang;
782  //@deprecated since v4.7.1/5.0.1 (2012-11-20); for < 4.6 modules, since 4.7/5.0 translation files should be in modules/modulepath/views/admin/ dir
783  if ( !is_dir( $sFullPath ) ) {
784  $sFullPath = $oConfig->getModulesDir() . $sPath;
785  $sFullPath .= ($blForAdmin) ? '/out/admin/' : '/out/lang/';
786  $sFullPath .= $sLang;
787  }
788  // END deprecated
789  $aLangFiles = $this->_appendLangFile( $aLangFiles, $sFullPath );
790  //load admin modules options lang files
791  if ( $blForAdmin ) {
792  $aLangFiles[] = $sFullPath . '/module_options.php';
793  }
794  }
795  }
796 
797  return $aLangFiles;
798  }
799 
809  protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
810  {
811  $myConfig = $this->getConfig();
812  $sLangFilesIdent = '_default';
813  if (is_array($aLangFiles) && $aLangFiles) {
814  $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
815  }
816  return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
817  }
818 
828  protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
829  {
830  $myConfig = $this->getConfig();
831  $myUtils = oxRegistry::getUtils();
832 
833  $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
834  $aLangCache = $myUtils->getLangCache( $sCacheName );
835  if ( !$aLangCache && $aLangFiles === null ) {
836  if ( $blAdmin ) {
837  $aLangFiles = $this->_getAdminLangFilesPathArray( $iLang );
838  } else {
839  $aLangFiles = $this->_getLangFilesPathArray( $iLang );
840  }
841  }
842  if ( !$aLangCache && $aLangFiles ) {
843  $aLangCache = array();
844  $sBaseCharset = false;
845  $aLang = array();
846  $aLangSeoReplaceChars = array();
847  foreach ( $aLangFiles as $sLangFile ) {
848 
849  if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
850  $aSeoReplaceChars = array();
851  include $sLangFile;
852 
853  // including only (!) those, which has charset defined
854  if ( isset( $aLang['charset'] ) ) {
855 
856  // recoding only in utf
857  if ( $myConfig->isUtf() ) {
858  $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
859 
860  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
861  $aSeoReplaceChars = $this->_recodeLangArray( $aSeoReplaceChars, $aLang['charset'], true );
862  }
863 
864  // overriding charset
865  $aLang['charset'] = 'UTF-8';
866  }
867 
868  if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
869  $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
870  }
871 
872  if ( !$sBaseCharset ) {
873  $sBaseCharset = $aLang['charset'];
874  }
875 
876  $aLangCache = array_merge( $aLangCache, $aLang );
877  }
878  }
879  }
880 
881  // setting base charset
882  if ( $sBaseCharset ) {
883  $aLangCache['charset'] = $sBaseCharset;
884  }
885 
886  // special character replacement list
887  $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
888 
889  //save to cache
890  $myUtils->setLangCache( $sCacheName, $aLangCache );
891  }
892 
893  return $aLangCache;
894  }
895 
904  protected function _getLanguageMap( $iLang, $blAdmin = null )
905  {
906  $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
907  $sKey = $iLang . ( (int) $blAdmin );
908  if ( !isset( $this->_aLangMap[$sKey] ) ) {
909  $this->_aLangMap[$sKey] = array();
910  $myConfig = $this->getConfig();
911 
912  $sMapFile = '';
913  $sParentMapFile = $myConfig->getAppDir() . '/views/' . ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
914  $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' . ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sCustomTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
915 
916  if ( file_exists( $sCustomThemeMapFile ) && is_readable( $sCustomThemeMapFile ) ) {
917  $sMapFile = $sCustomThemeMapFile;
918  } elseif ( file_exists( $sParentMapFile ) && is_readable( $sParentMapFile ) ) {
919  $sMapFile = $sParentMapFile;
920  }
921 
922  if ( $sMapFile ) {
923  include $sMapFile;
924  $this->_aLangMap[$sKey] = $aMap;
925  }
926 
927  }
928 
929  return $this->_aLangMap[$sKey];
930  }
931 
940  protected function _getCacheLanguageId( $blAdmin, $iLang = null )
941  {
942  $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
943  if ( !isset( $iLang ) ) {
944  $iLang = $this->getBaseLanguage();
945  if ( !isset( $iLang ) ) {
946  $iLang = 0;
947  }
948  }
949 
950  return (int) $iLang;
951  }
952 
962  protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
963  {
964  startProfile("_getLangTranslationArray");
965 
966  $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
967  $iLang = $this->_getCacheLanguageId( $blAdmin, $iLang );
968  $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
969 
970  if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
971  $this->_aLangCache[$sCacheName] = array();
972  }
973  if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
974  // loading main lang files data
975  $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
976  }
977 
978  stopProfile("_getLangTranslationArray");
979 
980  // if language array exists ..
981  return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
982  }
983 
992  protected function _sortLanguagesCallback( $a1, $a2 )
993  {
994  return ($a1->sort > $a2->sort);
995  }
996 
1002  public function getName()
1003  {
1004  return $this->_sName;
1005  }
1006 
1012  public function getFormLang()
1013  {
1014  $sLang = null;
1015  if ( !$this->isAdmin()) {
1016  $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\" />";
1017  }
1018  return $sLang;
1019  }
1020 
1028  public function getUrlLang( $iLang = null )
1029  {
1030  $sLang = null;
1031  if ( !$this->isAdmin()) {
1032  $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
1033  $sLang = $this->getName()."=". $iLang;
1034  }
1035  return $sLang;
1036  }
1037 
1050  public function processUrl( $sUrl, $iLang = null )
1051  {
1052  $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
1053  $oStr = getStr();
1054 
1055  if ( !$this->isAdmin() ) {
1056  $sParam = $this->getUrlLang( $iLang );
1057  if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) && ($iLang != oxRegistry::getConfig()->getConfigParam( 'sDefaultLang' ))) {
1058  if ( $sUrl ) {
1059  if ($oStr->strpos( $sUrl, '?') === false) {
1060  $sUrl .= "?";
1061  } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
1062  $sUrl .= "&amp;";
1063  }
1064  }
1065  $sUrl .= $sParam."&amp;";
1066  } else {
1067  $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
1068  }
1069  }
1070 
1071  return $sUrl;
1072  }
1073 
1080  public function detectLanguageByBrowser()
1081  {
1082  $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
1083 
1084  if ( !$sBrowserLang ) {
1085  return;
1086  }
1087 
1088  $aLangs = $this->getLanguageArray(null, true );
1089 
1090  foreach ( $aLangs as $oLang ) {
1091  if ( $oLang->abbr == $sBrowserLang ) {
1092  return (int) $oLang->id;
1093  }
1094  }
1095  }
1096 
1102  public function getMultiLangTables()
1103  {
1104  $aTables = array( "oxarticles", "oxartextends", "oxattribute",
1105  "oxcategories", "oxcontents", "oxcountry",
1106  "oxdelivery", "oxdiscount", "oxgroups",
1107  "oxlinks", "oxnews", "oxobject2attribute",
1108  "oxpayments", "oxselectlist", "oxshops",
1109  "oxactions", "oxwrapping", "oxdeliveryset",
1110  "oxvendor", "oxmanufacturers", "oxmediaurls",
1111  "oxstates" );
1112 
1113 
1114  $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
1115 
1116  if ( is_array( $aMultiLangTables ) ) {
1117  $aTables = array_merge($aTables, $aMultiLangTables);
1118  }
1119 
1120  return $aTables;
1121  }
1122 
1130  public function getSeoReplaceChars($iLang)
1131  {
1132  // get language replace chars
1133  $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
1134  if (!is_array($aSeoReplaceChars)) {
1135  $aSeoReplaceChars = array();
1136  }
1137 
1138  return $aSeoReplaceChars;
1139  }
1140 
1146  protected function _getActiveModuleInfo()
1147  {
1148  if ($this->_aActiveModuleInfo === null) {
1149  $oModuleList = oxNew('oxModuleList');
1150  $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
1151  }
1153  }
1154 
1160  protected function _getDisabledModuleInfo()
1161  {
1162  if ($this->_aDisabledModuleInfo === null) {
1163  $oModuleList = oxNew('oxModuleList');
1164  $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
1165  }
1167  }
1168 
1174  public function getAllShopLanguageIds()
1175  {
1177 
1178  return $aLanguages;
1179  }
1180 
1188  public function getLanguageIds( $iShopId = null )
1189  {
1190  if ( empty( $iShopId ) || $iShopId == $this->getConfig()->getShopId() ) {
1192  } else {
1193  $aLanguages = $this->_getLanguageIdsFromDatabase( $iShopId );
1194  }
1195 
1196  return $aLanguages;
1197  }
1198 
1204  public function getActiveShopLanguageIds()
1205  {
1206  $oConfig = $this->getConfig();
1207 
1208  //if exists language parameters array, extract lang id's from there
1209  $aLangParams = $oConfig->getConfigParam( 'aLanguageParams' );
1210  if ( is_array( $aLangParams ) ) {
1211  $aIds = $this->_getLanguageIdsFromLanguageParamsArray( $aLangParams );
1212  } else {
1213  $aIds = $this->_getLanguageIdsFromLanguagesArray( $oConfig->getConfigParam( 'aLanguages' ) );
1214  }
1215 
1216  return $aIds;
1217  }
1218 
1226  protected function _getLanguageIdsFromDatabase( $iShopId = null )
1227  {
1228  $aLanguages = $this->getLanguageIds();
1229 
1230 
1231  return $aLanguages;
1232  }
1233 
1242  protected function _getConfigLanguageValues( $sLanguageParameterName, $iShopId = null )
1243  {
1244  $aConfigDecodedValues = array();
1245  $aConfigValues = $this->_selectLanguageParamValues( $sLanguageParameterName, $iShopId );
1246 
1247  foreach ( $aConfigValues as $sConfigValue )
1248  {
1249  $aConfigLanguages = unserialize( $sConfigValue['oxvarvalue'] );
1250 
1251  $aLanguages = array();
1252  if ( $sLanguageParameterName == 'aLanguageParams' ) {
1253  $aLanguages = $this->_getLanguageIdsFromLanguageParamsArray( $aConfigLanguages );
1254  } elseif ( $sLanguageParameterName == 'aLanguages' ) {
1255  $aLanguages = $this->_getLanguageIdsFromLanguagesArray( $aConfigLanguages );
1256  }
1257 
1258  $aConfigDecodedValues = array_unique( array_merge( $aConfigDecodedValues, $aLanguages ) );
1259  }
1260 
1261  return $aConfigDecodedValues;
1262  }
1263 
1272  protected function _selectLanguageParamValues( $sParamName, $sShopId = null )
1273  {
1275  $oConfig = oxRegistry::getConfig();
1276 
1277  $sQuery = "
1278  select ".$oConfig->getDecodeValueQuery()." as oxvarvalue
1279  from oxconfig
1280  where oxvarname = '{$sParamName}' ";
1281 
1282  if ( !empty( $sShopId ) ) {
1283  $sQuery .= " and oxshopid = '{$sShopId}' limit 1";
1284  }
1285 
1286  return $oDb->getArray( $sQuery );
1287  }
1288 
1289 
1297  protected function _getLanguageIdsFromLanguageParamsArray( $aLanguageParams )
1298  {
1299  $aLanguages = array();
1300  foreach ( $aLanguageParams as $sAbbr => $aValue ) {
1301  $iBaseId = (int) $aValue[ 'baseId' ];
1302  $aLanguages[ $iBaseId ] = $sAbbr;
1303  }
1304 
1305  return $aLanguages;
1306  }
1307 
1316  {
1317  return array_keys( $aLanguages );
1318  }
1319 
1320 }