Source for file jSelectorLoc.class.php

Documentation is available at jSelectorLoc.class.php

  1. <?php
  2. /**
  3. * see jISelector.iface.php for documentation about selectors.
  4. @package     jelix
  5. @subpackage  core_selector
  6. @author      Laurent Jouanneau
  7. @contributor Rahal
  8. @contributor Julien Issler
  9. @contributor Baptiste Toinot
  10. @copyright   2005-2013 Laurent Jouanneau
  11. @copyright   2007 Rahal
  12. @copyright   2008 Julien Issler
  13. @copyright   2008 Baptiste Toinot
  14. @link        http://www.jelix.org
  15. @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  16. */
  17.  
  18. /**
  19.  * selector for localisation string
  20.  *
  21.  * localisation string are stored in file properties.
  22.  * syntax : "module~prefixFile.keyString".
  23.  * Corresponding file : locales/xx_XX/prefixFile.CCC.properties.
  24.  * xx_XX and CCC are lang and charset set in the configuration
  25.  *
  26.  * @package    jelix
  27.  * @subpackage core_selector
  28.  */
  29. class jSelectorLoc extends jSelectorModule {
  30.     protected $type = 'loc';
  31.     public $fileKey = '';
  32.     public $messageKey = '';
  33.     public $locale ='';
  34.     public $charset='';
  35.     public $_compiler = 'jLocalesCompiler';
  36.     protected $_where;
  37.  
  38.     function __construct($sel$locale=null$charset=null){
  39.  
  40.         if ($locale === null){
  41.             $locale jApp::config()->locale;
  42.         }
  43.         if ($charset === null){
  44.             $charset jApp::config()->charset;
  45.         }
  46.         if (strpos($locale,'_'=== false{
  47.             $locale jLocale::langToLocale($locale);
  48.         }
  49.         $this->locale = $locale;
  50.         $this->charset = $charset;
  51.         $this->_suffix = '.'.$charset.'.properties';
  52.         $this->_compilerPath=JELIX_LIB_CORE_PATH.'jLocalesCompiler.class.php';
  53.  
  54.         if (jelix_scan_locale_sel($sel$this)) {
  55.             if ($this->module ==''{
  56.                 $this->module = jApp::getCurrentModule ();
  57.             }
  58.             $this->_createPath();
  59.             $this->_createCachePath();
  60.         }
  61.         else {
  62.             throw new jExceptionSelector('jelix~errors.selector.invalid.syntax'array($sel,$this->type));
  63.         }
  64.     }
  65.  
  66.     protected function _createPath(){
  67.  
  68.         if (!isset(jApp::config()->_modulesPathList[$this->module])) {
  69.             if ($this->module == 'jelix')
  70.                 throw new Exception('jelix module is not enabled !!');
  71.             throw new jExceptionSelector('jelix~errors.selector.module.unknown'$this->toString());
  72.         }
  73.  
  74.         $locales array($this->locale);
  75.         $lang substr($this->locale0strpos($this->locale,'_'));
  76.         // FIXME we should drop support of such locales 'en_EN', and supports directory with lang name 'en'
  77.         // study impact of such changes
  78.         $generic_locale $lang.'_'.strtoupper($lang);
  79.         if($this->locale !== $generic_locale)
  80.             $locales[$generic_locale;
  81.  
  82.         foreach($locales as $locale){
  83.             // check if the locale has been overloaded
  84.             $overloadedPath jApp::varPath('overloads/'.$this->module.'/locales/'.$locale.'/'.$this->resource.$this->_suffix);
  85.             if (is_readable ($overloadedPath)){
  86.                 $this->_path = $overloadedPath;
  87.                 $this->_where = 'overloaded/';
  88.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  89.                 return;
  90.             }
  91.  
  92.             // check if the locale is available in the locales directory
  93.             $localesPath jApp::varPath('locales/'.$locale.'/'.$this->module.'/locales/'.$this->resource.$this->_suffix);
  94.             if (is_readable ($localesPath)){
  95.                 $this->_path = $localesPath;
  96.                 $this->_where = 'locales/';
  97.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  98.                 return;
  99.             }
  100.  
  101.             // else check for the original locale file in the module
  102.             $path jApp::config()->_modulesPathList[$this->module].'locales/'.$locale.'/'.$this->resource.$this->_suffix;
  103.             if (is_readable ($path)){
  104.                 $this->_where = 'modules/';
  105.                 $this->_path = $path;
  106.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  107.                 return;
  108.             }
  109.         }
  110.  
  111.         // to avoid infinite loop in a specific lang or charset, we should check if we don't
  112.         // try to retrieve the same message as the one we use for the exception below,
  113.         // and if it is this message, it means that the error message doesn't exist
  114.         // in the specific lang or charset, so we retrieve it in en_EN language and UTF-8 charset
  115.         if($this->toString(== 'jelix~errors.selector.invalid.target'){
  116.             $l 'en_US';
  117.             $c 'UTF-8';
  118.         }
  119.         else{
  120.             $l null;
  121.             $c null;
  122.         }
  123.         throw new jExceptionSelector('jelix~errors.selector.invalid.target'array($this->toString()"locale")1$l$c);
  124.     }
  125.  
  126.     protected function _createCachePath(){
  127.         // don't share the same cache for all the possible dirs
  128.         // in case of overload removal
  129.         $this->_cachePath = jApp::tempPath('compiled/locales/'.$this->_where.$this->module.'~'.$this->resource.$this->_cacheSuffix);
  130.     }
  131.  
  132.     public function toString($full=false){
  133.         if($full)
  134.             return $this->type.':'.$this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  135.         else
  136.             return $this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  137.     }
  138. }

Documentation generated on Wed, 04 Jan 2017 22:56:43 +0100 by phpDocumentor 1.4.3