Ver Mensaje Individual
  #9 (permalink)  
Antiguo 28/06/2014, 12:26
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: para rematar mi traductor

Ya me acorde de una solucion "general"......... la hice en el 2008

Me hizo acordar esto:

Cita:
Iniciado por Durgeoble Ver Mensaje
separa el contenido de la vista y traduce el contenido, es lo único que se me ocurre

Cita:
<i>vamos a probar</i> una vieja idea de <b><i>@Italico76</i></b> a ver si logro <b>traducir</b> esto sin perder tags <b>en el camino</b> !!!
lo deja como:

Cita:
<i>we will try</i>an old idea<b><i> @Italico76</i></b> to see if I <b>translate</b> this without losing tags <b> on the road</b>!
Código PHP:
Ver original
  1. <?php
  2. <?php
  3. include 'html_editor.class.php';
  4.  
  5. /*
  6.     Ejemplo de uso de la clase html_editor para traducir un texto sin perder los tags usando
  7.     la API de Google Translate
  8. */
  9.  
  10.  
  11. $html = '<i>vamos a probar</i> una vieja idea de <b><i>@Italico76</i></b> a ver si logro <b>traducir</b> esto sin perder tags <b>en el camino</b> !!!   ';
  12.  
  13. $texto = new htmlEditor($html);
  14.  
  15.  
  16. function fill_right($str)
  17. {
  18.     if (empty($str))
  19.         return $str;
  20.    
  21.     return $str[strlen($str)-1]==' ' ? $str : $str.' ';
  22. }
  23.  
  24. ///Procesa Google Translate
  25. function traductor($url)
  26. {
  27.     //Procesamos la URL
  28.     $contenido = explode('"',str_replace('[[["','',file_get_contents($url)));  
  29.  
  30.     $contenido = str_replace(',,','',$contenido);
  31.     $contenido = str_replace('[','',$contenido);
  32.    
  33.     return $contenido[0];
  34. }
  35.  
  36. // aca iria TU funcion traduce
  37. function traduce($descripcion,$sl = 'es',$tl= 'en',$hl= 'it')
  38. {  
  39.     //Codificamos la descripción para que Google la entienda
  40.     $descripcion = urlencode($descripcion);
  41.     //Traducimos
  42.     $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;
  43.     $traduccion = traductor($url);
  44.    
  45.     return $traduccion;
  46. }
  47. foreach ($texto as $fragemento)
  48. {
  49.     $translated = traduce($fragemento);    
  50.     $texto->setChunk(fill_right($translated));
  51. }
  52.  
  53.  
  54.        
  55. // HTML traducido  
  56. echo $texto;


y mi libreria de 2008 actualizada


Código PHP:
Ver original
  1. <?php
  2. /*
  3.     HTML EDITOR
  4.     @author: Pablo Bozzolo (2008)
  5.     minor update 2013
  6.    
  7.    
  8.     Recibe un HTML y permite modificar el texto sin preocuparse de alterar los tags
  9.  
  10.     __construct(html)  recibe el html
  11.     count()  devuelve el numero de trozos de texto desnudos
  12.     setChunk(fragmento,n) cambia esa frase (elemento del array en la clase)
  13.     getChunk(n) devuelve el fragmento (bit) que se pide
  14.     getHTML() reconstruye el HTML uniendo las "frases" (texto puro) con los tags y lo devuelve.
  15.    
  16.     Implementa Iterator asi que getChunk() y setChunk() no requieren de un (n)
  17. */
  18. class htmlEditor implements Countable, Iterator
  19. {  
  20.  
  21.     private $_html;
  22.     private $_tag;   // tags
  23.     private $_notag; // texto puro
  24.     private $_notagCant;
  25.     private $_quitarEspacio;
  26.  
  27.     function __construct($html = NULL)
  28.     {
  29.         if (!is_null($html))
  30.             $this->setHtml($html);
  31.     }  
  32.  
  33.     function count()
  34.     {
  35.         return $this->_notagCant;
  36.     }
  37.  
  38.     function setHtml($html)
  39.     {
  40.         $this->_html = $html;
  41.        
  42.         if ((strlen($this->_html)>0) and ($this->_html[0]=='<')){
  43.             $this->_html = ' '.$this->_html;
  44.             $this->_quitarEspacio=TRUE;  
  45.         }  
  46.  
  47.         $this->no_tags();      
  48.         $this->_notagCant = count ($this->_notag);
  49.     }
  50.  
  51.     function getHtml()
  52.     {      
  53.         $html = null;
  54.        
  55.         for ($i=0;$i<$this->count()-1;$i++)
  56.         {    
  57.             $a = $this->_notag[$i];
  58.             $b = $this->_tag[$i];
  59.             $html = $html.$a.$b;  
  60.         }
  61.    
  62.         $html .= $this->_notag[$i];
  63.    
  64.         //if ($this->_quitarEspacio)
  65.         //  $html =substr ($html,0,strlen($html)-1);
  66.                
  67.         return $html;
  68.     }
  69.  
  70.     function setChunk($element,$n=NULL)
  71.     {
  72.         if (is_null($n))
  73.             $n = $this->key();
  74.        
  75.         $this->_notag[$n]=$element;
  76.     }    
  77.  
  78.     function getChunk($n)
  79.     {
  80.         return $this->_notag[$n];
  81.     }
  82.  
  83.   /*
  84.      pre-condicion para $this->_tag : la cadena debe empezar con algo distinto a un tag (<)
  85.   */
  86.     private function no_tags()
  87.     {    
  88.    
  89.         preg_match_all('@(<[/]?[a-z]{1,}[^>]{0,}[/]?[\w]{0,}?>)@is', $this->_html, $matches);
  90.         $full_tags = $matches[0];
  91.    
  92.         $full_tags = array_reverse($full_tags); // tengo que sacar del principio
  93.         $cant = count($full_tags);
  94.  
  95.         $ini = 0;
  96.        
  97.         for ($i=1;$i<=$cant+1;$i++)
  98.         {
  99.             $ti = array_pop ($full_tags);  
  100.             $fin= strpos($this->_html, $ti,$ini);  
  101.            
  102.             if ($fin==NULL)
  103.                 $fin=strlen($this->_html);
  104.    
  105.             $dif    = ($fin-$ini);    
  106.             $inserto= substr ($this->_html,$ini,$dif);  
  107.             $this->_notag[] = $inserto;            
  108.             $ini    = $fin +strlen ($ti);  
  109.         }  
  110.  
  111.         $this->_tag = $matches[0];  
  112.     }  
  113.  
  114.     /* Iterator */
  115.     function rewind() {        
  116.         reset($this->_notag);
  117.     }
  118.  
  119.     public function current()
  120.     {
  121.         return current($this->_notag);
  122.     }
  123.  
  124.     public function key()
  125.     {        
  126.         return key($this->_notag);
  127.     }
  128.  
  129.     public function next()
  130.     {
  131.         next($this->_notag);        
  132.     }
  133.  
  134.     public function valid()
  135.     {  
  136.         return isset($this->_notag[$this->key()]);
  137.     }
  138.  
  139.    
  140.     public function __toString()
  141.     {      
  142.         return $this->  getHtml();     
  143.     }
  144.  
  145. }


Mi clase fue posteada en 2008 y profundamente olvidada... de hecho el post de aporte.. SE PERDIO!!!

Pero proponia un uso similar a este en 2009


HOY... lo haria MUCHO MAS SIMPLE con replace_callback() y la expresion correcta que recoja el texto fuera de los tags... esa clase es innecesaria si lo puedes hacer asi en 1 o 2 lineas de codigo
__________________
Salu2!

Última edición por Italico76; 28/06/2014 a las 18:04