Wednesday, May 22, 2013

Google translator code php

5/22/2013

Use Google translator code  in your  php script, Code send a HTTP request to the Google Translate website to translate a given string between an origin language and target language. Code show the translation results




googletranslate.php

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

class translate { 


    var $translate_from; 
    var $translate_into; 
    var $debug; 




    function __construct($from , $to){ 

        $this->debug = 0; 


        ini_set("display_errors",$this->debug); 


        if(!$from){ 

            $this->translate_from = "en"; 

        }else{ 

            $this->translate_from = $from; 

        } 
         
        if(!$to){ 

            $this->translate_into = "it"; 


        }else{ 
             
            $this->translate_into = $to; 

        } 

    } 


    function TranslateUrl($word){ 


        if(!$word){ 

            die("you need to adda a translate word"); 
        } 


        $word = urlencode($word); 

        $url = "http://translate.google.com/?sl=". $this->translate_from ."&tl=". $this->translate_into ."&js=n&prev=_t&hl=it&ie=UTF-8&eotf=1&text=". $word .""; 


        return $url; 

    } 

    function get($word){ 


        $dom  = new DOMDocument(); 
         
        $html =  $this->curl_download($this->TranslateUrl($word)); 

        $dom->loadHTML($html); 

        $xpath = new DOMXPath($dom); 
         
        $tags = $xpath->query('//*[@id="result_box"]'); 

        foreach ($tags as $tag) { 
             
            $var = trim($tag->nodeValue); 

            if(!$var){ 
                $this->sendEmail(); 
            }else{ 

                return ($var); 

            } 
             
                   
        } 

    } 

   

    function curl_download($Url){ 
      
        
        if (!function_exists('curl_init')){ 
            
            if (function_exists('file_get_contents')){ 

                return file_get_contents($Url); 

            }else{ 

                die("Your server dosen't support curl or file get contents"); 

            } 

        } 
      
        
        $ch = curl_init(); 

        curl_setopt($ch, CURLOPT_URL, $Url); 
      
        
        curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
      
        curl_setopt($ch, CURLOPT_HEADER, 0); 
      
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      
        curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
      
        $output = curl_exec($ch); 
      
        curl_close($ch); 
      
        return $output; 
    } 


} 



?>
usage of script
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

require_once("googletranslate.php"); 


 $translate_from = "en"; #language from what we translate 
 $translate_into = "it"; #language in what we want to translate 

$var = new translate($translate_from,$translate_into); 
echo $var->get("hello"); // replace your text with "hello"



?>

Supported languages

Afrikaans (AF)
Albanian (SQ)
English (EN)
Arabic (AR)
Azerbaijani (AZ)
Bengali (BN)
Belarusian (BE)
Bulgarian (BG)
Croatian (HR)
Czech (CS)
Danish (DA)
Esperanto (EO)
Estonian (ET)
Filipino (TL)
Finnish (FI)
French (FR)
Galician (GL)
Greek (EL)
Georgian (KA)
Gujarati (GU)
Hebrew (IW)
Hindi (HI)
Spanish (ES)
Dutch (NL)
Indonesian (ID)
Irish (GA)
Iceland (IS)
Japanese (JA)
Yiddish (YI)
Kannada (KN)
Catalan (CA)
Khmer (KM)
Korean (KO)
Haitan (HT)
Lao (LO)
Lithuanian (LT)
Latin (LA)
Latvian (LV)
Macedonian (MK)
Malay (MS)
Maltese (MT)
German (DE)
Norwegian (NO)
Armenian (HY)
Persian (FA)
Polish (PL)
Portuguese (PT)
Russian (RU)
Romanian (RO)
Serbian (SR)
Slovak (SK)
Slovenian (SL)
Swahili (SW)
Swedish (SV)
Thai (TH)
Tamil (TA)
Telugu (TE)
Turkish (TR)
Ukrainian (UK)
Urdu (UR)
Welsh (CY)
Hungarian (HU)
Vietnamese (VI)
Italian (IT)

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top