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-2012 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(preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_]+)\.([a-zA-Z0-9_\.]+)$/"$sel$m)){
  55.             if($m[1]!='' && $m[2]!=''){
  56.                 $this->module = $m[2];
  57.             }else{
  58.                 $this->module = jContext::get ();
  59.             }
  60.             $this->resource = $m[3];
  61.             $this->fileKey = $m[3];
  62.             $this->messageKey = $m[4];
  63.             $this->_createPath();
  64.             $this->_createCachePath();
  65.         }else{
  66.             throw new jExceptionSelector('jelix~errors.selector.invalid.syntax'array($sel,$this->type));
  67.         }
  68.     }
  69.  
  70.     protected function _createPath(){
  71.  
  72.         if (!isset(jApp::config()->_modulesPathList[$this->module])) {
  73.             if ($this->module == 'jelix')
  74.                 throw new Exception('jelix module is not enabled !!');
  75.             throw new jExceptionSelector('jelix~errors.selector.module.unknown'$this->toString());
  76.         }
  77.  
  78.         $locales array($this->locale);
  79.         $lang substr($this->locale0strpos($this->locale,'_'));
  80.         // FIXME we should drop support of such locales 'en_EN', and supports directory with lang name 'en'
  81.         // study impact of such changes
  82.         $generic_locale $lang.'_'.strtoupper($lang);
  83.         if($this->locale !== $generic_locale)
  84.             $locales[$generic_locale;
  85.  
  86.         foreach($locales as $locale){
  87.             // check if the locale has been overloaded
  88.             $overloadedPath jApp::varPath('overloads/'.$this->module.'/locales/'.$locale.'/'.$this->resource.$this->_suffix);
  89.             if (is_readable ($overloadedPath)){
  90.                 $this->_path = $overloadedPath;
  91.                 $this->_where = 'overloaded/';
  92.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  93.                 return;
  94.             }
  95.             // else check for the original locale file
  96.             $path jApp::config()->_modulesPathList[$this->module].'locales/'.$locale.'/'.$this->resource.$this->_suffix;
  97.             if (is_readable ($path)){
  98.                 $this->_where = 'modules/';
  99.                 $this->_path = $path;
  100.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  101.                 return;
  102.             }
  103.         }
  104.  
  105.         // to avoid infinite loop in a specific lang or charset, we should check if we don't
  106.         // try to retrieve the same message as the one we use for the exception below,
  107.         // and if it is this message, it means that the error message doesn't exist
  108.         // in the specific lang or charset, so we retrieve it in en_EN language and UTF-8 charset
  109.         if($this->toString(== 'jelix~errors.selector.invalid.target'){
  110.             $l 'en_US';
  111.             $c 'UTF-8';
  112.         }
  113.         else{
  114.             $l null;
  115.             $c null;
  116.         }
  117.         throw new jExceptionSelector('jelix~errors.selector.invalid.target'array($this->toString()"locale")1$l$c);
  118.     }
  119.  
  120.     protected function _createCachePath(){
  121.         // don't share the same cache for all the possible dirs
  122.         // in case of overload removal
  123.         $this->_cachePath = jApp::tempPath('compiled/locales/'.$this->_where.$this->module.'~'.$this->resource.$this->_cacheSuffix);
  124.     }
  125.  
  126.     public function toString($full=false){
  127.         if($full)
  128.             return $this->type.':'.$this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  129.         else
  130.             return $this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  131.     }
  132. }

Documentation generated on Mon, 26 Oct 2015 21:56:09 +0100 by phpDocumentor 1.4.3