linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.language.php

Documentation is available at class.language.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage system
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Language Management
  10.  */
  11. include_once('afiles/class.afiles.php');
  12.  
  13. class language {
  14.   /*
  15.    * @parameters
  16.    */
  17.   var $a_file;
  18.   var $languages=array();
  19.  
  20.   /**
  21.    * config_file::__construct()
  22.    * constructeur de classe
  23.    * @access public
  24.    * @return void 
  25.    */
  26.   function __construct()
  27.   {
  28.     $this->a_file=new afiles;
  29.     $this->getAllLang();
  30.   }
  31.   
  32.   /**
  33.    * PHP 4 compatibility
  34.    * as __construct()
  35.    * @return 
  36.    */
  37.   
  38.   function config_file()
  39.   {
  40.     $this->a_file=new afiles;
  41.     $this->getAllLang();
  42.   }
  43.   /**
  44.    * language::replace()
  45.    * modifie l'ensemble des fichiers portant l'extension $extension dans le $path
  46.    * de façon récursive
  47.    * @access public
  48.    * @param string $path 
  49.    * @param array $extension 
  50.    * @return void 
  51.    */
  52.   function replace($path$extension="php")
  53.   {
  54.     $files_path=$this->a_file->lsr(SITE_PATH.$path$extensionarray());
  55.     foreach ($files_path as $key => $file_path)
  56.     {
  57.       $file=new afiles($file_path);
  58.       $content_file=$file->read_file(false,false);
  59.       $new_file=$this->_replace($content_file);
  60.       if($new_file <> $content_file$this->a_file->mkfile($new_file$file_path);
  61.     }
  62.   }
  63. /**
  64.    * language::scan()
  65.    * scan l'ensemble des fichiers portant l'extension $extension dans le $path
  66.    * de façon récursive
  67.    * @access public
  68.    * @param string $path 
  69.    * @param array $extension 
  70.    * @return $ret 
  71.    */
  72.   function scan ($path$extension="php")
  73.   {
  74.     $ret="";
  75.     if($this->a_file->type(SITE_PATH<> false)
  76.     
  77.     $files_path=$this->a_file->lsr(SITE_PATH.$path$extensionarray());
  78.     foreach ($files_path as $key => $file_path)
  79.     {
  80.       $file=new afiles($file_path);
  81.       $content_file=$file->read_file(false,false);
  82.       $ret.=$this->_parse($content_file$file_path);
  83.     }
  84.     }
  85.     else $ret=_t('tools','language_site_path');
  86.     
  87.     if($ret == ""$ret=_t('tools','language_scan_no_result');
  88.     return $ret;
  89.   }
  90.   /**
  91.    * language::_parse()
  92.    * scan l'ensemble des fichiers de langue et compare avec un fichier donné
  93.    * affiche une erreur si l'item n'est pas trouvé dans le fichier de langue
  94.    * de façon récursive
  95.    * @access public
  96.    * @param string $content_file 
  97.    * @param array $file_path 
  98.    * @return $error 
  99.    */
  100.   function _parse ($content_file$file_path)
  101.   {
  102.     $file_path=str_replace(SITE_PATH,"",$file_path);
  103.     $error=false;
  104.     $pattern="_t("
  105.     $pos_start true;
  106.  
  107.     while($pos_start !== false)
  108.     {
  109.       //position du début de lang       
  110.       $pos_start strpos($content_file$pattern);
  111.  
  112.       if($pos_start !== false)
  113.       {
  114.         //@TODO regex...
  115.         $pos_start_scope=$pos_start+strlen($pattern);
  116.         $pos_end_scope=strpos($content_file","$pos_start_scope);
  117.         $length_scope=$pos_end_scope-$pos_start_scope;
  118.         $scope substr($content_file,$pos_start_scope,$length_scope);
  119.         $pos_start_item=strpos($content_file","$pos_end_scope);
  120.         $pos_end_item=strpos($content_file")"$pos_start_item);
  121.         $length_item=$pos_end_item-$pos_start_item;
  122.         $item=substr($content_file,$pos_start_item+1,$length_item-1);
  123.  
  124.  
  125.         if(strpos($item"$"=== false && strpos($scope"$"=== false)
  126.         {
  127.           $scope=trim(trim($scope" ")"'");  
  128.           $item=trim(trim($item" ")"'");
  129.                   
  130.           foreach ($this->languages as $code_lang => $lang)
  131.           {
  132.             if(!isset($lang[$scope][$item]))
  133.             $error.="<p><em>lang</em> : ".$code_lang.", <em>file</em> : ".$file_path.", <em>scope</em> :".$scope.", <em>item</em> : ".$item."</p>";
  134.           }
  135.           
  136.         }
  137.         $length_pattern=$pos_end_item-$pos_start;
  138.         $content_file=substr_replace($content_file"foobar"$pos_start,$length_pattern+1);
  139.       }
  140.     }
  141.     return $error;
  142.   }
  143.   /**
  144.    * language::_replace()
  145.    * remplace l'ensemble des item  correspondant à un pattern dans la chaine $content_file
  146.    * de façon récursive
  147.    * @access public
  148.    * @param string $content_file 
  149.    * @return $content_file 
  150.    */
  151.   function _replace ($content_file)
  152.   {
  153.     $pattern="GLOBALS['lang'][";
  154.      
  155.     $pos_start true;
  156.     while($pos_start !== false)
  157.     {
  158.       $pos_start strpos($content_file$pattern);
  159.       if($pos_start !== false)
  160.       {
  161.         //@TODO regex...
  162.         $pos_start_scope=$pos_start+strlen($pattern);
  163.         $pos_end_scope=strpos($content_file"]"$pos_start_scope);
  164.         $length_scope=$pos_end_scope-$pos_start_scope;
  165.         $scope substr($content_file,$pos_start_scope,$length_scope);
  166.         $pos_start_item=strpos($content_file"["$pos_end_scope+1);
  167.         $pos_end_item=strpos($content_file"]"$pos_start_item+1);
  168.         $length_item=$pos_end_item-$pos_start_item;
  169.         $item=substr($content_file,$pos_start_item+1,$length_item-1);
  170.  
  171.          
  172.         $new_pattern="_t(".$scope.",".$item.")";
  173.         //echo "new_pattern ".$new_pattern."<br />";
  174.          
  175.         $length_pattern=$pos_end_item-$pos_start;
  176.         $content_file=substr_replace($content_file$new_pattern$pos_start-1,$length_pattern+2);
  177.       }
  178.     }
  179.     return $content_file;
  180.   }
  181.  /**
  182.    * language::GetAllLang()
  183.    * Initialise tous les fichiers de langue dans l'attribut $languages
  184.    * @access public
  185.    * @return void 
  186.    */
  187.   function GetAllLang()
  188.   {
  189.     $langs=array();
  190.     $lang_files=$this->a_file->lsr(SITE_PATH."/languages""php"array());
  191.     //on récupère tous les fichiers de langue en rec
  192.     foreach ($lang_files as $key => $lang_file)
  193.     {
  194.       $file=str_replace(SITE_PATH."/languages/","",$lang_file);
  195.       $file_exp=explode("/"$file);
  196.       if(!isset($arr_langs[$file_exp[0]])) $arr_langs[$file_exp[0]]=array();
  197.       array_push($arr_langs[$file_exp[0]],$file_exp[1]);
  198.     }
  199.     
  200.     foreach ($arr_langs as $language => $files)
  201.     {
  202.       if(!isset($this->languages[$language])) $this->languages[$language]=array();
  203.     foreach ($files as $key => $file)
  204.     {
  205.       include('../languages/' $language '/'.$file);
  206.       if(isset($lang)) $this->languages[$language]=$lang;
  207.     }
  208.     }
  209.   }
  210. }
  211.  
  212.  
  213. ?>

Documentation generated on Fri, 16 Oct 2009 09:29:19 +0200 by phpDocumentor 1.4.1