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-2007 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.         global $gJConfig;
  40.         if ($locale === null){
  41.             $locale $gJConfig->locale;
  42.         }
  43.         if ($charset === null){
  44.             $charset $gJConfig->charset;
  45.         }
  46.         if(strpos($locale,'_'=== false){
  47.             $locale.='_'.strtoupper($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.         global $gJConfig;
  72.         if(!isset($gJConfig->_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->locale,0,2);
  80.         $generic_locale $lang.'_'.strtoupper($lang);
  81.         if($this->locale !== $generic_locale)
  82.             $locales[$generic_locale;
  83.  
  84.         foreach($locales as $locale){
  85.             // check if the locale has been overloaded
  86.             $overloadedPath jApp::varPath('overloads/'.$this->module.'/locales/'.$locale.'/'.$this->resource.$this->_suffix);
  87.             if (is_readable ($overloadedPath)){
  88.                 $this->_path = $overloadedPath;
  89.                 $this->_where = 'overloaded/';
  90.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  91.                 return;
  92.             }
  93.             // else check for the original locale file
  94.             $path $gJConfig->_modulesPathList[$this->module].'locales/'.$locale.'/'.$this->resource.$this->_suffix;
  95.             if (is_readable ($path)){
  96.                 $this->_where = 'modules/';
  97.                 $this->_path = $path;
  98.                 $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php';
  99.                 return;
  100.             }
  101.         }
  102.  
  103.         // to avoid infinite loop in a specific lang or charset, we should check if we don't
  104.         // try to retrieve the same message as the one we use for the exception below,
  105.         // and if it is this message, it means that the error message doesn't exist
  106.         // in the specific lang or charset, so we retrieve it in en_EN language and UTF-8 charset
  107.         if($this->toString(== 'jelix~errors.selector.invalid.target'){
  108.             $l 'en_EN';
  109.             $c 'UTF-8';
  110.         }
  111.         else{
  112.             $l null;
  113.             $c null;
  114.         }
  115.         throw new jExceptionSelector('jelix~errors.selector.invalid.target'array($this->toString()"locale")1$l$c);
  116.     }
  117.  
  118.     protected function _createCachePath(){
  119.         // on ne partage pas le même cache pour tous les emplacements possibles
  120.         // au cas où un overload était supprimé
  121.         $this->_cachePath = jApp::tempPath('compiled/locales/'.$this->_where.$this->module.'~'.$this->resource.$this->_cacheSuffix);
  122.     }
  123.  
  124.     public function toString($full=false){
  125.         if($full)
  126.             return $this->type.':'.$this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  127.         else
  128.             return $this->module.'~'.$this->fileKey.'.'.$this->messageKey;
  129.     }
  130. }

Documentation generated on Mon, 19 Sep 2011 14:13:34 +0200 by phpDocumentor 1.4.3