Location: PHPKode > scripts > Siviglia Templating > siviglia-templates/templating/html/plugins/L.php
<?php
/*

  Siviglia Framework templating engine

  BSD License

  Copyright (c) 2012, Jose Maria Rodriguez Millan
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
  the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this list of conditions and 
    the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 
    the following disclaimer in the documentation and/or other materials provided with the distribution.
  * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or 
    promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Please, send bugs, feature requests or comments to: 
 
dashiad at hotmail.com 
 
You can find more information about this class at: 
 
http://xphperiments.blogspot.com 

*/
include_once(SIVIGLIA_PATH."/Plugin.php");
class L extends Plugin
{
    static $translations=null;
     function __construct($parentWidget,$layoutContents,$layoutManager)
     {
         $this->parentWidget=$parentWidget;         
         $this->layoutContents=$layoutContents;
         $this->layoutManager=$layoutManager;
     }

     function parse()
     {                  
        $layoutContents=& $this->layoutContents;
        $nContents=count($layoutContents);
        if($nContents==0)
            return;
        $params=$this->layoutManager->getPluginParams("L");
        $tLang=$params["lang"];
        if(!$tLang)
        {
            debug("Error: es necesario pasar el parametro 'LANG' para el plugin L en el LayoutManager");
            exit();
        }

        if($tLang==DEFAULT_LANGUAGE)
        {            
            return $this->layoutContents;
        }
        
        if(!$this->translations[$tLang])
        {
            $this->loadTranslations($tLang);
        }
               
        
        $curVar=1;
        $phpCode=array();
        $newContents=array();
        $currentNode=new \CHTMLElement("",$this->layoutContents[0]->parentWidget);
        $newContents[]=$currentNode;
        /* Supongamos una lista de layoutContents del tipo HTML - PHP - HTML : ej: a <?php echo "p";?> b
         *  Para buscar la traduccion, hay que actuar como si fuera 1 solo layoutContent de tipo a %1 b
         *  Por eso, el resultado debe ser 1 solo layoutcontent de tipo HTML, aunque dentro, contenga codigo php.
         * 
         */
        for($k=0;$k<$nContents;$k++)
        {            
                if(is_a($layoutContents[$k],'\CHTMLElement'))
                {
                    $html.=$layoutContents[$k]->preparedContents;
                }
                else
                {
                    if(is_a($layoutContents[$k],'\CPHPElement'))
                    {
                        $html.="{%".$curVar."}";
                        $phpCode[$curVar]=$layoutContents[$k]->preparedContents;
                        $curVar++;
                    }
                    else
                    {
                        // Se traduce el nodo HTML actual
                        $currentNode->preparedContents=$this->parseTranslation($tLang,$html,$phpCode);
                        // Se copia el nodo que hemos encontrado, tal cual (no es ni PHP ni HTML)
                        $newContents[]=$layoutContents[$k];
                        // Se crea el siguiente nodo HTML.
                        $currentNode=new \CHTMLElement("",$this->layoutContents[0]->parentWidget);
                        $newContents[]=$currentNode;
                        $html="";
                        $phpCode=array();
                        $curVar=1;
                    }
                    
                }
        }     
        // Al llegar aqui, nos queda el ultimo nodo HTML creado.Hay que parsearlo.
        $currentNode->preparedContents=$this->parseTranslation($tLang,$html,$phpCode);
        return $newContents;
     }
     function parseTranslation($tLang,$html,$phpCode)
     {
         
                
         $search=array();
         if( $tLang==DEFAULTLANG )
         {
             $trimmed=str_replace("_"," ",$html);
             $this->translations[$tLang][$trimmed]=$trimmed;
         }
         else
         {
            
            $found=false;
            // strtolower added for the Downloads Platform
             $trimmed=strtolower(trim($html));
        }
        foreach($phpCode as $cKey=>$cValue)$search["/{\\%".$cKey."}/"]=$cValue;
        if(is_array($this->translations[$tLang]))
        {
           
            if(array_key_exists($trimmed,$this->translations[$tLang]))                    
            {                                        
                 $found=true;
                if($this->translations[$tLang][$trimmed]) // Si isset, pero esta vacia, no se modifica nada                
                     $target=$this->translations[$tLang][$trimmed];                                           
                else
                {
                    $target=$html;
                }
            }
        }
        if($found==false)
        {
            $this->addPendingTranslation($tLang,$trimmed);
            $target=$html;
        }
        
        return preg_replace(array_keys($search),array_values($search),$target);
     }

     function loadTranslations($lang)
     {
         if($this->translations==null)
         {
             $op=@fopen(PROJECTPATH."/lang/".$lang.".txt","r");
             if(!$op)
             {
                 // No existe el fichero
                 return;
             }
             $lineNo=0;
             while($line=fgets($op))
             {
                 if(trim($line)=="")
                     continue;
                 $lineNo++;
                 $parts=explode("::=::",$line);
                 if(count($parts)!=2)
                 {
                     debug("ERROR EN FICHERO DE TRADUCCION [".$lang."] en linea $lineNo: ".$line);
                     exit();
                 }
                 $this->translations[$lang][trim($parts[0])]=trim($parts[1]);
             }
             fclose($op);                
         }
     }
     function addPendingTranslation($lang,$value)
     {
         @mkdir(PROJECTPATH."/lang/",0777,true);
         $op=fopen(PROJECTPATH."/lang/".$lang.".txt","a");
         fwrite($op,$value."::=::\n");
         fclose($op);
         $this->translations[$lang][$value]="";
     }
}
Return current item: Siviglia Templating