Ver Mensaje Individual
  #10 (permalink)  
Antiguo 30/06/2014, 15:29
Sirrion
 
Fecha de Ingreso: junio-2010
Mensajes: 19
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: para rematar mi traductor

Perdon la demora, gracias por el aporte no he podido estar hasta hoy en el pc.

He queria adaptarlo para que me traduzca varios html que deje dentro de una carpeta pero solo me coge de esa carpeta 1 html los demas no me los coge son muchos html los que he de traducir y de 1 en 1 es un poco engorroso si al menos me coge de 5 en 5 o 10 en 10 de esa carpeta ya seria mas rapido. Dejo lo que he hecho a ver si alguien sabe el porque no me los coge. Gracias

Código PHP:
<?php
/*
    HTML EDITOR
    @author: Pablo Bozzolo (2008)
    minor update 2013
    
    
    Recibe un HTML y permite modificar el texto sin preocuparse de alterar los tags
 
    __construct(html)  recibe el html
    count()  devuelve el numero de trozos de texto desnudos
    setChunk(fragmento,n) cambia esa frase (elemento del array en la clase)
    getChunk(n) devuelve el fragmento (bit) que se pide 
    getHTML() reconstruye el HTML uniendo las "frases" (texto puro) con los tags y lo devuelve.
    
    Implementa Iterator asi que getChunk() y setChunk() no requieren de un (n)
*/
class htmlEditor implements CountableIterator
{  
 
    private 
$_html;
    private 
$_tag;   // tags
    
private $_notag// texto puro
    
private $_notagCant;
    private 
$_quitarEspacio;
 
    function 
__construct($html NULL)
    {
        if (!
is_null($html))
            
$this->setHtml($html);
    }  
  
    function 
count()
    { 
        return 
$this->_notagCant;
    }
  
    function 
setHtml($html)
    {
        
$this->_html $html;
        
        if ((
strlen($this->_html)>0) and ($this->_html[0]=='<')){
            
$this->_html ' '.$this->_html
            
$this->_quitarEspacio=TRUE;  
        }  
 
        
$this->no_tags();       
        
$this->_notagCant count ($this->_notag);
    }
  
    function 
getHtml()
    {       
        
$html null;
        
        for (
$i=0;$i<$this->count()-1;$i++)
        {     
            
$a $this->_notag[$i];
            
$b $this->_tag[$i];
            
$html $html.$a.$b;  
        }
    
        
$html .= $this->_notag[$i];
    
        
//if ($this->_quitarEspacio)
        //  $html =substr ($html,0,strlen($html)-1); 
                
        
return $html;
    }
 
    function 
setChunk($element,$n=NULL)
    {
        if (
is_null($n))
            
$n $this->key();
        
        
$this->_notag[$n]=$element;
    }    
  
    function 
getChunk($n)
    {
        return 
$this->_notag[$n];
    }
  
  
/*
     pre-condicion para $this->_tag : la cadena debe empezar con algo distinto a un tag (<)
  */
    
private function no_tags()
    {    
    
        
preg_match_all('@(<[/]?[a-z]{1,}[^>]{0,}[/]?[\w]{0,}?>)@is'$this->_html$matches);
        
$full_tags $matches[0];
   
        
$full_tags array_reverse($full_tags); // tengo que sacar del principio
        
$cant count($full_tags);
 
        
$ini 0
        
        for (
$i=1;$i<=$cant+1;$i++)
        {
            
$ti array_pop ($full_tags);  
            
$finstrpos($this->_html$ti,$ini);  
            
            if (
$fin==NULL)
                
$fin=strlen($this->_html);
    
            
$dif    = ($fin-$ini);     
            
$insertosubstr ($this->_html,$ini,$dif);  
            
$this->_notag[] = $inserto;            
            
$ini    $fin +strlen ($ti);  
        }  
 
        
$this->_tag $matches[0];  
    }  
  
    
/* Iterator */
    
function rewind() {        
        
reset($this->_notag);
    }
 
    public function 
current()
    {
        return 
current($this->_notag);
    }
 
    public function 
key()
    {        
        return 
key($this->_notag);
    }
 
    public function 
next()
    {
        
next($this->_notag);        
    }
 
    public function 
valid()
    {   
        return isset(
$this->_notag[$this->key()]);
    }
 
    
    public function 
__toString()
    {       
        return 
$this->  getHtml();      
    }
  
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Traductor</title>
</head>
<body>


</body>
<?php
$directorio
=opendir("./traducir"); 
while (
$archivo readdir($directorio))
{
    if (
$archivo!="." && $archivo!="..")
    {
        
$file fopen("./traducir/".$archivo"r") or exit("Unable to open file!");
        
$texto = new htmlEditor(fread($file,filesize("./traducir/".$archivo)));
        
$traducido "";
        foreach (
$texto as $fragemento)
        {
            
$traducido traduce($fragemento,"ru","en","es");     
            
$texto->setChunk(fill_right($traducido));
        }
        
fclose($file);
        
$nuevoarchivo fopen("./traducido/".$archivo"w+");
        
fwrite($nuevoarchivo,$texto);
        
fclose($nuevoarchivo);
    }
}
closedir($directorio);  
 
function 
fill_right($str)
{
    if (empty(
$str))
        return 
$str;
    
    return 
$str[strlen($str)-1]==' ' $str $str.' ';
}
 
///Procesa Google Translate
function traductor($url)
{
    
//Procesamos la URL
    
$contenido explode('"',str_replace('[[["','',file_get_contents($url)));   
 
    
$contenido str_replace(',,','',$contenido);
    
$contenido str_replace('[','',$contenido);
    
    return 
$contenido[0];
}
 
// aca iria TU funcion traduce
function traduce($descripcion,$sl 'es',$tl'en',$hl'it')
{   
    
//Codificamos la descripción para que Google la entienda
    
$descripcion urlencode($descripcion);
    
//Traducimos
    
$url "https://translate.google.es/translate_a/single?client=t&sl=$sl&tl=$tl&hl=$hl&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&oc=1&otf=1&trs=1&inputm=1&srcrom=1&ssel=0&tsel=0&q=".$descripcion;
    
$traduccion traductor($url);
    
    return 
$traduccion;
}
?>
</html>