Source for file jBundle.class.php

Documentation is available at jBundle.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage core
  5. @author     Laurent Jouanneau
  6. @author     Gerald Croes
  7. @contributor Julien Issler, Yannick Le Guédart, Dominique Papin
  8. @copyright  2001-2005 CopixTeam, 2005-2010 Laurent Jouanneau
  9. *  Some parts of this file are took from Copix Framework v2.3dev20050901, CopixI18N.class.php, http://www.copix.org.
  10. *  copyrighted by CopixTeam and released under GNU Lesser General Public Licence.
  11. *  initial authors : Gerald Croes, Laurent Jouanneau.
  12. *  enhancement by Laurent Jouanneau for Jelix.
  13. @copyright 2008 Julien Issler, 2008 Yannick Le Guédart, 2008 Dominique Papin
  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. * a bundle contains all readed properties in a given language, and for all charsets
  20. @package  jelix
  21. @subpackage core
  22. */
  23. class jBundle {
  24.     public $fic;
  25.     public $locale;
  26.  
  27.     protected $_loadedCharset = array ();
  28.     protected $_strings = array();
  29.  
  30.     /**
  31.     * constructor
  32.     * @param jSelector   $file selector of a properties file
  33.     * @param string      $locale    the code lang
  34.     */
  35.     public function __construct ($file$locale){
  36.         $this->fic  = $file;
  37.         $this->locale = $locale;
  38.     }
  39.  
  40.     /**
  41.     * get the translation
  42.     * @param string $key the locale key
  43.     * @param string $charset 
  44.     * @return string the localized string
  45.     */
  46.     public function get ($key$charset null){
  47.  
  48.         if($charset == null){
  49.             $charset $GLOBALS['gJConfig']->charset;
  50.         }
  51.         if (!in_array ($charset$this->_loadedCharset)){
  52.             $this->_loadLocales ($this->locale$charset);
  53.         }
  54.  
  55.         if (isset ($this->_strings[$charset][$key])){
  56.             return $this->_strings[$charset][$key];
  57.         }else{
  58.             return null;
  59.         }
  60.     }
  61.  
  62.     /**
  63.     * Loads the resources for a given locale/charset.
  64.     * @param string $locale     the locale
  65.     * @param string $charset    the charset
  66.     */
  67.     protected function _loadLocales ($locale$charset){
  68.         global $gJConfig;
  69.         $this->_loadedCharset[$charset;
  70.  
  71.         $source $this->fic->getPath();
  72.         $cache $this->fic->getCompiledFilePath();
  73.  
  74.         // check if we have a compiled version of the ressources
  75.  
  76.         if (is_readable ($cache)){
  77.             $okcompile true;
  78.  
  79.             if ($gJConfig->compilation['force']){
  80.                $okcompile false;
  81.             }else{
  82.                 if ($gJConfig->compilation['checkCacheFiletime']){
  83.                     if (is_readable ($source&& filemtime($sourcefilemtime($cache)){
  84.                         $okcompile false;
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             if ($okcompile{
  90.                 include ($cache);
  91.                 $this->_strings[$charset$_loaded;
  92.                 return;
  93.             }
  94.         }
  95.  
  96.         $this->_loadResources ($source$charset);
  97.  
  98.         if(isset($this->_strings[$charset])){
  99.             $content '<?php $_loaded= '.var_export($this->_strings[$charset]true).' ?>';
  100.  
  101.             jFile::write($cache$content);
  102.         }
  103.     }
  104.  
  105.  
  106.     /**
  107.     * loads a given resource from its path.
  108.     */
  109.     protected function _loadResources ($fichier$charset){
  110.  
  111.         if (($f @fopen ($fichier'r')) !== false{
  112.             $utf8Mod ($charset=='UTF-8')?'u':'';
  113.             $unbreakablespace ($charset=='UTF-8')?utf8_encode(chr(160)):chr(160);
  114.             $escapedChars array('\#','\n''\w''\S''\s');
  115.             $unescape array('#',"\n"' '$unbreakablespace' ');
  116.             $multiline=false;
  117.             $linenumber=0;
  118.             $key='';
  119.             while (!feof($f)) {
  120.                 if($line=fgets($f)){
  121.                     $linenumber++;
  122.                     $line=rtrim($line);
  123.                     if($multiline){
  124.                         if(preg_match("/^\s*(.*)\s*(\\\\?)$/U".$utf8Mod$line$match)){
  125.                             $multiline($match[2=="\\");
  126.                             if (strlen ($match[1])) {
  127.                                 $sp preg_split('/(?<!\\\\)\#/'$match[1]-,PREG_SPLIT_NO_EMPTY);
  128.                                 $this->_strings[$charset][$key].=' '.str_replace($escapedChars,$unescape,trim($sp[0]));
  129.                             else {
  130.                                 $this->_strings[$charset][$key].=' ';
  131.                             }
  132.                         }else{
  133.                             throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,210);
  134.                         }
  135.                     }elseif(preg_match("/^\s*(.+)\s*=\s*(.*)\s*(\\\\?)$/U".$utf8Mod,$line$match)){
  136.                         // on a bien un cle=valeur
  137.                         $key=$match[1];
  138.                         $multiline($match[3=="\\");
  139.                         $sp preg_split('/(?<!\\\\)\#/'$match[2]-,PREG_SPLIT_NO_EMPTY);
  140.                         if(count($sp)){
  141.                             $value=trim($sp[0]);
  142.                         }else{
  143.                             $value='';
  144.                         }
  145.  
  146.                         $this->_strings[$charset][$keystr_replace($escapedChars,$unescape,$value);
  147.  
  148.                     }elseif(preg_match("/^\s*(\#.*)?$/",$line$match)){
  149.                         // ok, just a comment
  150.                     }else {
  151.                         throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,211);
  152.                     }
  153.                 }
  154.             }
  155.             fclose ($f);
  156.         }else{
  157.             throw new Exception('Cannot load the resource '.$fichier,212);
  158.         }
  159.     }
  160. }

Documentation generated on Wed, 24 Sep 2014 21:56:25 +0200 by phpDocumentor 1.4.3