Problemas con la línea de traducción en PHP

  • Autor Autor shadowhck
  • Fecha de inicio Fecha de inicio
Estado

🔒 Este tema está cerrado para nuevas respuestas.

⏰ Solo el creador del tema puede solicitar la reapertura de sus propios temas, pero únicamente dentro de los 60 días previos a la última actualización.

shadowhck

shadowhck

Lambda
Programador
tengo problemas con la linea:
PHP:
echo ".$tr->translate($message,'english','spanish').<br>\n";
diganme que esta mal, porque me marca error _:

me tira lo siguiente en vez del texto
.(,'english','spanish').
 
Última edición:
¿Te sirvió? Únete a la conversación

Con tu cuenta gratis puedes responder, escribirle al autor por mensaje privado y comprar con Saldo retenido: el pago se libera cuando recibes lo acordado.

Toma 30 segundos · vuelves a este tema al terminar
En comillas no creo que debas poner el array, eso es para cadenas.
El punto debería ir después del array para concatenar pero cerrando las comillas.
 
prueba con
PHP:
 echo $tr->translate($message,'english','spanish')."<br>\n";
 
No se como sera el codigo pero a simple vista prueba con esto:
echo $tr->translate($message,'english','spanish')."<br>\n";

O sino con esto, que es lo mismo, pero más prolijo por si luego deseas agregar más cosas:
echo "".$tr->translate($message,'english','spanish')."<br>\n";

El problema esta en que, basicamente, estabas haciendo mal la concatenación en la version que nos pasaste.
 
Última edición:
ahora uso la siguiente:
PHP:
echo "En Castellano: <br />\n" .$tr->translate($message,'english','spanish')."<br />\n";

pero ahora no se que hice mal,

me tira;:
** error : babelfish.class.php returned nothing

la clase php que uso es esta pero no se que esta mal:
PHP:
<?php
/*******************************************************************************
** Class: babelfish
** Purpose: Translate text using Altavista Babelfish
** Filename: babelfish.class.php
** Author: Vedanta Barooah
** Author Email: vedanta . barooah @ gmail . com
** Date: June 19 2005
** Last Updated: June 27 2007
** 
** Other Contributors:will@jetlabs.com,stefm34@wanadoo.fr,Nikolay Kubarelov <admin@gramophon.com>
********************************************************************************/

/* if ! PHP5 */
if (!function_exists('http_build_query')) {
   function http_build_query($formdata, $numeric_prefix = "")
   {
       $arr = array();
       foreach ($formdata as $key => $val)
         $arr[] = urlencode($numeric_prefix.$key)."=".urlencode($val);
       return implode($arr, "&");
   }
}
/* translate text using altavista babelfish */
class babelfish{
    /* array to store language names */
    var $languages      =   NULL;
    /* stores the altavista babelfish url*/
    var $babel_url      =   NULL;
    /* stores the search regex  (see readme for details) */
    var $search_regex   =   NULL;
    /* stores the data to be posted in an array (see readme for details) */
    var $post_data      =   NULL;
    /* stores the supported translation combination(s) */
    var $valid_translate   =   NULL;
    
    /* proxy support 
    **
    */
    var $useProxy           =    FALSE;
    var $proxyServer       =    NULL;
    var $proxyPort           =    NULL;
    var $timeOut           =    NULL;
        
    /* class constructor */
    function babelfish($url=NULL,$postdata=NULL,$regex=NULL){
        /* list of languages */
        $this->languages    =       array(
                                        'en'    =>    'english',
                                        'zh'    =>    'chinese',
                                        'zt'    =>    'chinese-traditional',
                                        'nl'    =>    'dutch',
                                        'fr'    =>    'french',
                                        'de'    =>    'german',
                                        'el'    =>    'greek',
                                        'it'    =>    'italian',
                                        'ja'    =>    'japanese',
                                        'ko'    =>    'korean',
                                        'pt'    =>    'portuguese',
                                        'ru'    =>    'russian',
                                        'es'    =>    'spanish'
                                );
        /* list of valid translations */
        $this->valid_translate=array(
            'zt_en','en_zh','en_zt','en_nl','en_fr',
            'en_de','en_el','en_it','en_ja','en_ko',
            'en_pt','en_ru','en_es','nl_en','nl_fr',
            'fr_en','fr_de','fr_el','fr_it','fr_pt',
            'fr_nl','fr_es','de_en','de_fr','el_en',
            'el_fr','it_en','it_fr','ja_en','ko_en',
            'pt_en','pt_fr','ru_en','es_en','es_fr'
        );

        /* babelfish service url */
        if($url!=NULL)
            $this->babel_url=$url;
        else
            $this->babel_url="http://babelfish.altavista.com/tr";
        /* data that is posted to the babelfish site */
        if($postdata!=NULL)
            $this->post_data=$postdata;
        else
            $this->post_data=array(
                        'doit'=>'done',
                        'intl'=>'1',
                        'tt'=>'urltext',
                        'trtext'=>NULL,
                        'lp'=>NULL
            );
        /* search for the translated text using this regex */
        if($regex!=NULL)
            $this->search_regex=$regex;
        else
             #$this->search_regex='/<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div><\/td>/';
             $this->search_regex='/<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div><\/td>/sm';
    }
    
    /* set proxy settings */
    function setProxy($proxyServer, $proxyPort, $timeOut=15){
        $this->useProxy = true;
        $this->proxyServer = $proxyServer;
        $this->proxyPort = $proxyPort;
        $this->timeOut = $timeOut;
    }
    
    /* perform babelfish translation */
    function translate($text,$from_language,$to_language,$forceUTF8=true){
        $f=array_search(strtolower($from_language),$this->languages);
        if(!$f){die("***error: source language not found");}
        $t=array_search(strtolower($to_language),$this->languages);
        if(!$t){die("***error: result language not found");}
        $l=$f.'_'.$t;
        if(!in_array($l,$this->valid_translate)){die("***error: cant translate with given combination ($l)");}
        $this->post_data['trtext']=$text;
        $this->post_data['lp']=$l;
        $query=http_build_query($this->post_data);
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, trim($this->babel_url));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (babelfish.class.php)");
        curl_setopt($ch,CURLOPT_ENCODING , "UTF-8");
        /* use proxy if required */
        if($this->useProxy){
            curl_setopt($ch,CURLOPT_PROXY,$this->proxyServer.":".$this->proxyPort);
            curl_setopt($ch,CURLOPT_TIMEOUT,$this->timeOut);    
        }
        $output = curl_exec($ch);
        curl_close($ch);
        $result=preg_match($this->search_regex,$output,$match);
        if(count($match)!=0){
            return strip_tags($match[0]);
        }else{
            return '** error : babelfish.class.php returned nothing';
        }
    }
}
/* end of class  */
?>
 
Return nothing es que la clase esa no te esta devolviendo nada, ningún valor.
 
no deberia ser asi?:

PHP:
echo $tr->translate($message,'english','spanish')."<br>\n";
 
o así

<?= $tr->translate($message, 'english', 'spanish') ?><br>
 
Estado

🔒 Este tema está cerrado para nuevas respuestas.

⏰ Solo el creador del tema puede solicitar la reapertura de sus propios temas, pero únicamente dentro de los 60 días previos a la última actualización.

Atrás
Arriba