mzubairsaleem / gtranslate-api-php

Automatically exported from code.google.com/p/gtranslate-api-php
GNU General Public License v3.0
0 stars 0 forks source link

Fatal error #20

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Fatal error: Uncaught GTranslateException: [0]: Unable to perform
Translation:invalid text thrown in
/home/user/public_html/rosescc/gtranslate/GTranslate.php on line 231

someone can help me?

Thank

Original issue reported on code.google.com by mikamou...@gmail.com on 17 May 2010 at 1:21

GoogleCodeExporter commented 8 years ago
Hello,

Can you paste your code?

Original comment by josedasilva on 18 May 2010 at 11:59

GoogleCodeExporter commented 8 years ago
I, THIS IS MY INDEX.PHP

<?php
require("gtranslate/GTranslate.php");
$languages = parse_ini_file("gtranslate/languages.ini");

// Put the original directory here
$path_from = "lang/en/";
// Set the destination directory here
$path_to = "lang/fr/";
// Set the desired language here
$lang_to = "french";
$lang_to_iso = $languages[strtoupper($lang_to)];

// It retrieves the list of files contained in the original directory
$rep = dir($path_from);
$files_to_translate = array();
while ($nametmp = $rep->read()) {
    if (is_file($path_from.$nametmp) && $nametmp!="." && $nametmp!=".." &&
$nametmp!="Thumbs.db") $files_to_translate[] = $nametmp;
}
$rep->close();

$gt = new Gtranslate;
foreach($files_to_translate as $filename){
    $translation = "";
    // We treat the contents of the file line by line
    $filecontent = file($path_from.$filename);
    foreach($filecontent as $line){
        // If the line contains a PHP variable, so we translated
        if(strstr($line,'$')){
            // On extrait le nom et la valeur de la variable
            list($name,$value) = explode("=",$line);
            $value = str_replace('";','',$value);
            $value = str_replace(' "','',$value);
            $value = htmlentities($value);
            // We rewrite the line with the translation of the value of the variable
            $translation .= $name.'= "'.$gt->{"english_to_".$lang_to}($value).'";';
            $translation .= "\n";
        }
        // If the line does not contain variable, then it copies the line as it
        else {
            $translation .= $line;
        }
    }

    // Writing the new language file
    if(!is_dir($path_to)) mkdir($path_to);
    $newfilename = str_replace("_fr","_".$lang_to_iso,$filename);
    $fp = fopen($path_to.$newfilename, "w");
    fwrite($fp,$translation);
    fclose($fp);
}

AND THIS IS MY GTRANSLATE.PHP

<?php

/**
* GTranslate - A class to comunicate with Google Translate(TM) Service
*               Google Translate(TM) API Wrapper
*               More info about Google(TM) service can be found on
http://code.google.com/apis/ajaxlanguage/documentation/reference.html
*       This code has o affiliation with Google (TM) , its a PHP Library that 
allows to
comunicate with public a API
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @author Jose da Silva <jose@josedasilva.net>
* @since 2009/01/11
* @version 0.7.1
* @licence LGPL v3
*
* <code>
* <?
* require_once("GTranslate.php");
* try{
*   $gt = new Gtranslate;
*   echo $gt->english_to_german("hello world");
* } catch (GTranslateException $ge)
* {
*   echo $ge->getMessage();
* }
* ?>
* </code>
*/

/**
* Exception class for GTranslated Exceptions
*/

class GTranslateException extends Exception
{
    public function __construct($string) {
        parent::__construct($string, 0);
    }

    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }
}

class GTranslate
{
    /**
    * Google Translate(TM) Api endpoint
    * @access private
    * @var String 
    */
    private $url = "http://ajax.googleapis.com/ajax/services/language/translate";

        /**
        * Google Translate (TM) Api Version
        * @access private
        * @var String 
        */  
    private $api_version = "1.0";

        /**
        * Comunication Transport Method
    * Available: http / curl
        * @access private
        * @var String 
        */
    private $request_type = "http";

        /**
        * Path to available languages file
        * @access private
        * @var String 
        */
    private $available_languages_file   = "languages.ini";

        /**
        * Holder to the parse of the ini file
        * @access private
        * @var Array
        */
    private $available_languages = array();

        /**
        * Constructor sets up {@link $available_languages}
        */
    public function __construct()
    {
        $this->available_languages = parse_ini_file("languages.ini");
    }

        /**
        * URL Formater to use on request
        * @access private
        * @param array $lang_pair
    * @param array $string
    * "returns String $url
        */

    private function urlFormat($lang_pair,$string)
    {
        $parameters = array(
            "v" => $this->api_version,
            "q" => $string,
            "langpair"=> implode("|",$lang_pair)
        );

        $url  = $this->url."?";

        foreach($parameters as $k=>$p)
        {
            $url    .=  $k."=".urlencode($p)."&";
        }
        return $url;
    }

        /**
        * Query the Google(TM) endpoint 
        * @access private
        * @param array $lang_pair
        * @param array $string
        * returns String $response
        */

    public function query($lang_pair,$string)
    {
        $query_url = $this->urlFormat($lang_pair,$string);
        $response = $this->{"request".ucwords($this->request_type)}($query_url);
        return $response;
    }

        /**
        * Query Wrapper for Http Transport 
        * @access private
        * @param String $url
        * returns String $response
        */

    private function requestHttp($url)
    {
        return GTranslate::evalResponse(json_decode(file_get_contents($url)));
    }

        /**     
        * Query Wrapper for Curl Transport 
        * @access private
        * @param String $url
        * returns String $response
        */

    private function requestCurl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER["HTTP_REFERER"]);
        $body = curl_exec($ch);
        curl_close($ch);        
        return GTranslate::evalResponse(json_decode($body));
    }

        /**     
        * Response Evaluator, validates the response
    * Throws an exception on error 
        * @access private
        * @param String $json_response
        * returns String $response
        */

    private function evalResponse($json_response)
    {
        switch($json_response->responseStatus)
        {
            case 200:
                return $json_response->responseData->translatedText;
                break;
            default:
                throw new GTranslateException("Unable to perform
Translation:".$json_response->responseDetails);
            break;
        }
    }

        /**     
        * Validates if the language pair is valid
        * Throws an exception on error 
        * @access private
        * @param Array $languages
        * returns Array $response Array with formated languages pair
        */

    private function isValidLanguage($languages)
    {
        $language_list  = $this->available_languages;

        $languages      =   array_map( "strtolower", $languages );
        $language_list_v    =   array_map( "strtolower", array_values($language_list) );
        $language_list_k    =   array_map( "strtolower", array_keys($language_list) );
        $valid_languages    =   false;
        if( TRUE == in_array($languages[0],$language_list_v) AND TRUE ==
in_array($languages[1],$language_list_v) )
        {
            $valid_languages    =   true;   
        }

        if( FALSE === $valid_languages AND TRUE == in_array($languages[0],$language_list_k)
AND TRUE == in_array($languages[1],$language_list_k) )
        {
            $languages  = 
array($language_list[strtoupper($languages[0])],$language_list[strtoupper($langu
ages[1])]);
            $valid_languages        =       true;
        }

        if( FALSE === $valid_languages )
        {
            throw new GTranslateException("Unsupported languages
(".$languages[0].",".$languages[1].")");
        }

        return $languages;
    }

        /**     
        * Magic method to understande translation comman
    * Evaluates methods like language_to_language
        * @access public
    * @param String $name
        * @param Array $args
        * returns String $response Translated Text
        */

    public function __call($name,$args)
    {
        $languages_list     =   explode("_to_",strtolower($name));
        $languages = $this->isValidLanguage($languages_list);

        $string     =   $args[0];
        return $this->query($languages,$string);
    }
}

?>

IF YOU WANT TO VIEW MY SCRIPT, YOU CAN DOWNLOAD THE ATTACH FILE

THANK YOU VERY MUCH

Original comment by mikamou...@gmail.com on 18 May 2010 at 4:43

Attachments:

GoogleCodeExporter commented 8 years ago
I'm also getting this error. It resolves itself after a while then it happens 
again.

24-May-2010 00:00:24] PHP Fatal error:  Uncaught GTranslateException: [0]: 
Unable to 
perform Translation:invalid result data

  thrown in /path/google-translate/GTranslate.php on line 231

Original comment by enkay16@gmail.com on 24 May 2010 at 4:11

GoogleCodeExporter commented 8 years ago
it is very simple yar.
just create new case for 400 and return the same value as case 200 return 
just past the following code at line 231

case 400: return $json_response->responseData->translatedText;

Enjoy

Original comment by nidost...@gmail.com on 21 Jun 2010 at 6:15

GoogleCodeExporter commented 8 years ago
Thank it's work

Original comment by mikamou...@gmail.com on 23 Jun 2010 at 12:37

GoogleCodeExporter commented 8 years ago
Hello nidostyle,

the error isn't show. but it does not translate languages. how to make it work?

Original comment by excheaps...@gmail.com on 10 Jul 2010 at 11:31

GoogleCodeExporter commented 8 years ago
test to translate a single file (files by files) and space translations of 
several hours

Original comment by mikamou...@gmail.com on 10 Jul 2010 at 5:07

GoogleCodeExporter commented 8 years ago
Please note that Google advices to avoid too much server side interactions, 
being in danger of being blocked. The api is intended to be used on real time 
systems!

Original comment by josedasilva on 23 Jul 2010 at 6:19

GoogleCodeExporter commented 8 years ago
Issue 21 has been merged into this issue.

Original comment by josedasilva on 23 Jul 2010 at 6:30

GoogleCodeExporter commented 8 years ago
Please verify if using userIp, ip address for the user using your site that 
needs translation solves this.

Original comment by josedasilva on 23 Jul 2010 at 7:04