• 22-05-2013, 18:52:40
    #1
    İyi günler arkadaşlar.
    Sizlere bugün google translate class'ını paylaşmak istedim.
    Yararı olursa ne mutlu.

    class_translate.php
    <?php
    class translate {
    
    
    	///language from what we translate
    	var $translate_from;
    
    
    	////language in what we whant to translate
    	var $translate_into;
    
    	///debug the code
    	var $debug;
    
    
    
    
    	function __construct($from , $to){
    
    		/*
    
    		this function is for debuging code
    
    		*/
    
    		$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");
    		}
    		///we need to encode the word that we want to translate
    
    		$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){
    				die("Problem with Google translate Word");
    			}else{
    
    				return ($var);
    
    			}
    		    
    		  		
    		}
    
    	}
    
    	/*
    		function for downloading the gooogle page content for translating 
    	*/
    
    	function curl_download($Url){
    	 
    	    // is cURL installed yet?
    	    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");
    
    	        }
    
    	    }
    	 
    	    // OK cool - then let's create a new cURL resource handle
    	    $ch = curl_init();
    	    // Now set some options (most are optional)
    	    // Set URL to download
    	    curl_setopt($ch, CURLOPT_URL, $Url);
    	 
    	    // Set a referer
    	 
    	    // User agent
    	    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    	 
    	    // Include header in result? (0 = yes, 1 = no)
    	    curl_setopt($ch, CURLOPT_HEADER, 0);
    	 
    	    // Should cURL return or print out the data? (true = return, false = print)
    	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	 
    	    // Timeout in seconds
    	    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    	 
    	    // Download the given URL, and return output
    	    $output = curl_exec($ch);
    	 
    	    // Close the cURL resource, and free system resources
    	    curl_close($ch);
    	 
    	    return $output;
    	}
    
    
    }
    
    ?>
    index.php
    <?php
    require_once("class_translate.php");
    
    $var = new translate("en","tr");
    
    echo $var->get("hello world");
    ?>
    İyi forumlar.
  • 22-05-2013, 19:07:04
    #2
    Güzel ve işe yarayan bir class.
  • 22-05-2013, 19:30:07
    #3
    Teşekkür ederim yararı olur
  • 22-05-2013, 21:31:44
    #4
    Kimlik doğrulama veya yönetimden onay bekliyor.
    Sağolun, hemen class arşivime atıyorum.
  • 22-05-2013, 21:40:53
    #5
    eline sağlık dostum arşive aldım
  • 22-05-2013, 23:15:29
    #6
    Rica ederim dostlar..